Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the docker connection-plugin of ansible?

Tags:

docker

ansible

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?

like image 582
Thorbijoern Avatar asked May 05 '17 14:05

Thorbijoern


People also ask

How can you use Docker module in Ansible?

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.

How does Ansible module connect to Docker API?

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.

How does Ansible and Docker work together?

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.

Can Ansible be used with Docker?

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.

What is connection plugin Ansible?

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.


2 Answers

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.

like image 76
Konstantin Suvorov Avatar answered Sep 20 '22 10:09

Konstantin Suvorov


2020 TLDR: run a minimal Python container

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

Classic solution

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

Recommended Docker image

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/

like image 39
johntellsall Avatar answered Sep 21 '22 10:09

johntellsall