为什么这些管道命令使Ansible窒息?

时间:2019-03-06 01:28:39

标签: bash ansible yaml

当我从Ansible剧本中调用该命令时,在bash脚本和Amazon Linux 2终端中可用的命令中断。为了使此命令在Ansible剧本中起作用,我需要具体更改什么?

在目标服务器的终端中运行的命令如下:

[lnxcfg@ip-10-0-0-99 ~]$ MY_CALICO_CIDR=$( sudo cat /home/lnxcfg/calico.yaml | grep -A 1 CALICO_IPV4POOL_CIDR | grep value | cut -d ':' -f2 | tr -d ' "')
[lnxcfg@ip-10-0-0-99 ~]$ echo $MY_CALICO_CIDR
192.168.0.0/16

然后我将相同的命令放入Ansible剧本中,如下所示:

 - name: Put cidr for overlay network into environment variable
   command: MY_CALICO_CIDR=$( cat /home/lnxcfg/calico.yaml | grep -A 1 CALICO_IPV4POOL_CIDR | grep value | cut -d ':' -f2 | tr -d ' "')
   become: yes
   become_user: root

但是Ansible运行的输出显示Ansible正在解析命令并产生错误,结果如下:

TASK [Put cidr for overlay network into environment variable] **********************************************************************
fatal: [10.0.0.6]: FAILED! => {
    "changed": false, "cmd": "'MY_CALICO_CIDR=$(' cat /home/lnxcfg/calico.yaml '|' grep -A 1 CALICO_IPV4POOL_CIDR '|' grep value '|' cut -d : -f2 '|' tr -d ' \")'", "msg": "[Errno 2] No such file or directory", "rc": 2
}
 [WARNING]: Could not create retry file '/home/lnxcfg/playbooks/initializeKubernetesMaster.retry'.         [Errno 13] Permission
denied: u'/home/lnxcfg/playbooks/initializeKubernetesMaster.retry'

这是对this other question的跟进。

那么为了避免此错误,需要对Ansible Playbook进行哪些更改?

1 个答案:

答案 0 :(得分:1)

the fine manual中非常明确地指出,command:不是shell命令,它实际上被馈送到exec。对于您正在做的事情,您需要shell: