How can I run ansible-playbook on docker container to execute commands on an AWS EC2 Ubuntu instance?

  amazon-ec2, amazon-web-services, ansible, docker, ubuntu

I am trying to use ansible-playbook from a docker container to an Ubuntu AWS EC2 instance (hostname myubuntu).
I have the ssh key to allow me to ssh onto myubuntu as the ubuntu user like so:

bash-5.1# ssh -i my-key-file.pem [email protected]
...
[email protected]:~$ exit

But when I attempt to run a very simple anisble playbook like so:

bash-5.1# ansible-playbook -i inventories/dev-eu-central-1.yaml --private-key muy-key-file.pem 
--become-user ubuntu playbooks/hello-world.yaml

I get this output:

PLAY [Hello World] ************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************fatal: [ec2-XX-XX-XX-XX.eu-central-1.compute.amazonaws.com]: UNREACHABLE! => {"changed": false,
"msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory.
Consider changing the remote tmp path in ansible.cfg to a path rooted in "/tmp", for more error information use -vvv.
Failed command was: ( umask 77 && mkdir -p "` echo Please login as the user "ubuntu" rather than the user "root"./.ansible/tmp `"&&
                      mkdir "` echo Please login as the user "ubuntu" rather than the user "root"./.ansible/tmp/ansible-tmp-1614280818.9033527-8950-254411587258517 `" &&
                      echo ansible-tmp-1614280818.9033527-8950-254411587258517="` echo Please login as the user "ubuntu" rather than the user "root"./.ansible/tmp/ansible-tmp-1614280818.9033527-8950-254411587258517 `" )

PLAY RECAP ************************************************************************************************
ec2-XX-XX-XX-XX.eu-central-1.compute.amazonaws.com : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

What am I doing wrong? I thought specifing --become-user ubuntu to ansible-playbook would have taken care of this. to ansible-playbook.
I suspect I am not only person that has ever tried to do this so I hope the SO can help me out.

Source: Docker Questions

LEAVE A COMMENT