Ansible:如何向“添加变量”;“命令”;或;壳牌公司;
是否可以在Ansible:如何向“添加变量”;“命令”;或;壳牌公司;,ansible,ansible-2.x,Ansible,Ansible 2.x,是否可以在命令或外壳模块上使用变量? 我有以下代码,我想使用变量文件提供一些配置: 我想从变量文件中读取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
命令
或外壳
模块上使用变量?
我有以下代码,我想使用变量文件提供一些配置:
我想从变量文件中读取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
正确的语法是什么?在
命令
参数中引用完整的字符串:
- name: Iniciar zkfc
command: "{{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc"
语法错误是因为您以
{
开头一个值。如果您以这样的变量开头一个值:
命令:{{my_var}}
然后你必须引用整行:
命令:“{{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
如果它在整行没有被引用导致它无法解释变量格式而不是只是默默地失败时抛出某种错误或警告,那就太好了。