With some luck you can find official documentation online for Docker integration into Ansible:
but I lack of description connection
docker. Ansible v2.9 supports
$ ansible-doc -t connection -l | grep docker docker Run tasks in docker containers
Documentation suggests to read output of:
$ ansible-doc -t connection docker Run commands or put/fetch files to an existing docker container. ...
Lets start simple Docker container, install software and remove container afterwards.
We will use
localhost as the only inventory host:
- hosts: localhost connection: local become: false gather_facts: no
and dynamically add a Docker managed host after creating it:
tasks: # https://docs.ansible.com/ansible/latest/collections/community/general/docker_container_module.html # check that container is running by: # docker exec alpine-x ps - docker_container: name: alpine-x image: alpine:latest # command: sh -c "while sleep 1; do :; done" command: tail -f /dev/null auto_remove: yes cleanup: yes oom_killer: yes state: started # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html - add_host: name: alpine-x # To avoid: [WARNING]: # Platform linux on host alpine-x is using the discovered Python interpreter at /usr/bin/python3, # but future installation of another Python interpreter could change this. ansible_python_interpreter: /usr/bin/python3
command: tail -f /dev/null is used to keep container running infinity.
add_host we introduce host dynamically of the same name as Docker container.
Python 3 Support.
we can define Python interpreter only:
To explicitly configure a Python 3 interpreter, set the
ansible_python_interpreterinventory variable at a group or host level to the location of a Python 3 interpreter, such as /usr/bin/python3. The default interpreter path may also be set in
That is why we see
On other hand we can avoid setting
- add_host: name: alpine-x ansible_connection: docker ansible_ssh_user: root
add_host level because those settings are supported at plays'
- hosts: alpine-x connection: docker user: root become: false gather_facts: no # vars: # ansible_python_interpreter: /usr/bin/python3 tasks: # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/raw_module.html - raw: apk add python3 # https://docs.ansible.com/ansible/latest/collections/community/general/apk_module.html - apk: name: mc state: present
ansible_python_interpreter could be defined at
We have to install Python 3 with module
row as Alpine images don't have them bundled. Then we
can use Ansible modules as usual (
apk is an example).
Managed nodes. There are only minimal requirements for the clients. For every system you want to manage, you need to have the client's SSH key in the authorized_keys file of the management system and Python.
Ansible APK Module.
Finally we can destroy running container (remember
tail -f /dev/null blocks termination!):
- hosts: localhost connection: local become: false gather_facts: no tasks: # https://docs.ansible.com/ansible/latest/collections/community/general/docker_container_module.html # check that container is stopped: # docker ps - docker_container: name: alpine-x image: alpine:latest state: absent
Check full example play-fun-with-docker.yml.
Run it as:
Of cause you need to have Ansible & Docker installed and Docker supervisor running ))
You can communicate with Docker directly (here
raw module is used, don't forget to start
$ id=$(docker run -d --name alpine alpine tail -f /dev/null) $ ansible all -i "alpine," -c docker -m raw -a 'echo OK' $ ansible all -i "$id," -c docker -m raw -a 'echo OK' $ docker stop alpine $ docker stop $id