-1

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?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Separately, I hope you're aware that setting an environment variable in a `shell:` block like that won't do what you expect, just as running `bash -c "HELLO=world"` won't set one in your current shell – mdaniel Mar 07 '19 at 02:52

1 Answers1

1

As is very clearly stated in the fine manual, command: is not shell commands, it is essentially fed to exec. For what you are doing, you'll want shell:

mdaniel
  • 31,240
  • 5
  • 55
  • 58