A command that works in bash scripts and in the Amazon Linux 2 terminal is breaking when I call it from within an Ansible playbook. What specifically do I need to change in order to get this command to work in the Ansible playbook?
The command working in the terminal of the target server is as follows:
[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
And then I put the same command into an Ansible playbook as follows:
- 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
But the output of the Ansible run shows that Ansible is parsing the command and producing errors as a result as follows:
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 is a follow up to this other question.
So what changes need to be made to the Ansible Playbook in order to avoid this error?