Ansible:如何将变量添加到 "command" 或 "shell"
Ansible: How to add variables to "command" or "shell"
是否可以在 command
或 shell
模块上使用变量?
我有以下代码,我想使用变量文件来提供一些配置:
我想从我的变量文件中读取 Hadoop 版本。在 ansible 的其他模块上,我可以使用 {{ansible_version}}
,但是使用命令或 shell 它不起作用。
- name: start ZooKeeper HA
command: hadoop-2.7.1/bin/hdfs zkfc -formatZK -nonInteractive
- name: start zkfc
shell: hadoop-2.7.1/sbin/hadoop-daemon.sh start zkfc
我想转换成以下内容:
- name: Iniciar zkfc
command: {{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc
因为如果我 运行 使用这种语法它会抛出这个错误:
- name: inicializar estado ZooKeeper HA
command: {{hadoop_version}}/bin/hdfs zkfc -formatZK -nonInteractive
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
我已经尝试使用,但同样的问题:
- name: Iniciar zkfc
command: "{{ hadoop_version }}"/sbin/hadoop-daemon.sh start zkfc
正确的语法是什么?
在 command
参数中引用完整的字符串:
- name: Iniciar zkfc
command: "{{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc"
语法错误是因为您用 {
开始了一个值。如果您以这样的变量开始一个值:
command: {{ my_var }}
那么你必须引用整行:
command: "{{ my_var }}"
这是因为解析器无法区分 YAML 的字典语法和变量插值。
我已经完成以下步骤来替换 ansible shell 模块
中的清单主机名
- name: 'Task-10 Modif the splunk input.conf file with Actual hostname'
become: yes
become_method: sudo
become_user: root
shell: |
/splunk/splunkforwarder/bin/splunk enable boot-start --accept-license --answer-yes --no-prompt -user splunk
declare wfqdn
wfqdn=$(uname -n)
sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf
tags:
- always
主要玩法是:
sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf
是否可以在 command
或 shell
模块上使用变量?
我有以下代码,我想使用变量文件来提供一些配置:
我想从我的变量文件中读取 Hadoop 版本。在 ansible 的其他模块上,我可以使用 {{ansible_version}}
,但是使用命令或 shell 它不起作用。
- name: start ZooKeeper HA
command: hadoop-2.7.1/bin/hdfs zkfc -formatZK -nonInteractive
- name: start zkfc
shell: hadoop-2.7.1/sbin/hadoop-daemon.sh start zkfc
我想转换成以下内容:
- name: Iniciar zkfc
command: {{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc
因为如果我 运行 使用这种语法它会抛出这个错误:
- name: inicializar estado ZooKeeper HA
command: {{hadoop_version}}/bin/hdfs zkfc -formatZK -nonInteractive
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
我已经尝试使用,但同样的问题:
- name: Iniciar zkfc
command: "{{ hadoop_version }}"/sbin/hadoop-daemon.sh start zkfc
正确的语法是什么?
在 command
参数中引用完整的字符串:
- name: Iniciar zkfc
command: "{{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc"
语法错误是因为您用 {
开始了一个值。如果您以这样的变量开始一个值:
command: {{ my_var }}
那么你必须引用整行:
command: "{{ my_var }}"
这是因为解析器无法区分 YAML 的字典语法和变量插值。
我已经完成以下步骤来替换 ansible shell 模块
中的清单主机名- name: 'Task-10 Modif the splunk input.conf file with Actual hostname'
become: yes
become_method: sudo
become_user: root
shell: |
/splunk/splunkforwarder/bin/splunk enable boot-start --accept-license --answer-yes --no-prompt -user splunk
declare wfqdn
wfqdn=$(uname -n)
sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf
tags:
- always
主要玩法是:
sed -i "s/$wfqdn/"{{ inventory_hostname }}"/g" /splunk/splunkforwarder/etc/system/local/inputs.conf