将外部变量与Ansible传递给剧本

时间:2019-02-06 19:29:22

标签: ansible

在剧本中没有定义通过命令行传递的Ansible变量。

我正在寻找通过命令行将外部变量传递给一个有趣的剧本的方法。使用-e时,它无法按预期工作,这是基于ansible文档调用外部变量的。

 ansible-playbook  /opt/playbooks/shutdown.yaml -f 10 -i /opt/inventory/hosts -e 'logPath=/my/log/path logName=shutdown.log logDir=shutdown'



---
- name: Transfer and execute a script.
  hosts: all
  remote_user: ansible
  sudo: yes
  tasks:
     - name: Transfer the script
       copy: src=/opt/files/shutdown.sh dest=/tmp/ mode=0777

     - name: Execute the script
       command: sh /tmp/shutdown.sh  logPath  logName logDir

     - name: cat log output
       command: cat logDir
       register: myoutput


     - name: get stout of execution of script
       debug: msg={{ myoutput.stdout_lines }}

这是我的输出,我期望使用key:value对将LogPath定义为变量

: FAILED! => {"changed": true, "cmd": ["cat", "logPath"], "delta": "0:00:00.005258", "end": "2019-02-06 13:30:03.551631", "failed": true, "rc": 1, "start": "2019-02-06 13:30:03.546373", "stderr": "cat: logPath: No such file or directory", "stderr_lines": ["cat: logPath: No such file or directory"], "stdout": "", "stdout_lines": []}
        to retry, use: --limit @/opt/playbooks/shutdown.retry

2 个答案:

答案 0 :(得分:1)

您的command任务似乎是错误的,您需要使用大括号将ansible视为包含的字符串作为变量(并将其替换为其值)。尝试使用以下语法:

 - name: Execute the script
   command: sh /tmp/shutdown.sh  {{ logPath }} {{ logName }} {{ logDir }}

希望有帮助

答案 1 :(得分:0)

这些应该以{{1​​}}表示法传递,这将支持传递字符串以外的其他数据类型:

<template>
  <v-app>
    <v-container dark grid-list-md text-xs-center>
      <v-layout row wrap>
        <v-flex xs12 v-resize="onResize">
          <v-card color="primary"> <svg></svg> </v-card>
        </v-flex>
      </v-layout>
      mounted x: {{ windowSize.x }} mounted y: {{ windowSize.y }}
    </v-container>
  </v-app>
</template>

<script>
export default {
  name: "Test",
  data: () => ({ windowSize: { x: 0, y: 0, mx: 0, my: 0 } }),
  methods: {
    onResize() {
      this.windowSize = { x: window.innerWidth, y: window.innerHeight };
    }
  },
  mounted() {
    this.onResize();
  }
};
</script>

,然后进行相应替换:

JSON

snake-case而不是驼峰式大小写是变量名的默认设置。

请参见documentation