I want to create and edit docker containers automated using ansible and I found a connection plugin in the ansible GitHub repository, which uses docker exec instead of ssh to run commands etc. inside the container. I can't find any documentation about this plugin and can't exactly figure out how to use it?
docker (dynamic inventory) Dynamically builds an inventory of all the available containers from a set of one or more Docker hosts. Ansible 2.1. 0 includes major updates to the Docker modules, marking the start of a project to create a complete and integrated set of tools for orchestrating containers.
Environment Variables Control how the modules connect to the Docker API by setting the following variables in the environment of the host running Ansible: DOCKER_HOST. The URL or Unix socket path used to connect to the Docker API. DOCKER_API_VERSION.
Ansible is the easiest way to automate Docker in your development environment. Ansible allows you to automate your Docker container build and deployment method in means that you're likely doing manually. Ansible seamlessly automates Docker and operationalizes the process of building and deploying containers.
Ansible offers the following modules for orchestrating Docker containers: docker_service. Use your existing Docker compose files to orchestrate containers on a single Docker daemon or on Swarm. Supports compose versions 1 and 2.
Connection plugins allow Ansible to connect to the target hosts so it can execute tasks on them. Ansible ships with many connection plugins, but only one can be used per host at a time. By default, Ansible ships with several connection plugins.
It's simple: set connection: docker
and use container names as inventory hosts.
Example:
# docker run -d --name=mycontainer -e FOO=bar alpine:latest sleep 600
fde1a28914174c53e8f186f2b8ea312c0bda9c895fc6c956f3f1315788f0bf20
# ansible all -i 'mycontainer,' -c docker -m raw -a 'echo $FOO'
mycontainer | SUCCESS | rc=0 >>
bar
Just keep in mind, that most of Ansible modules require Python, but usually you have minimal amount of libraries inside your containers, and Python is not among them.
In 2020, the above solution (running a minimal Alpine container) doesn't work -- Python is not installed.
Building on Konstantin Suvorov's answer, to make Ansible happy, give it a slim Python container:
docker run -d --name=mycontainer python:3.8-slim-buster sleep 600
Check:
ansible all -i 'mycontainer,' -c docker -m setup
The solution above no longer works, Python is not discoverable by Ansible:
docker run -d --name=bogus alpine:latest sleep 600
ansible all -i 'bogus,' -c docker -m setup
[WARNING]: No python interpreters found for host bogus (tried ['/usr/bin/python',
'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6',
'/usr/libexec/platform-python', '/usr/bin/python3', 'python'])
To make Ansible happy, give it a slim Python container:
docker run -d --name=mycontainer python:3.8-slim-buster sleep 600
Check:
ansible all -i 'mycontainer,' -c docker -m setup
Itamar Turner-Trauring[1] recommended base Python image = python:3.8-slim-buster. The Alpine image, although nice and tiny, causes a lots of problems with Python! The above image is Debian-based, small enough, and totally solid.
[1] from https://pythonspeed.com/articles/base-image-python-docker-images/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With