I’ve been meaning to set up a baseline Docker image for creating a complete Ansible and AWS environment to kick start your use of Ansible.

As with all things, I won’t be the first, or even the last, but this should be some use if you want to just build an up-to-date Ansible image, use Ansible to run a container and fire off Ansible playbooks etc. and get on with your life.

Here goes. If you want to go to the Docker Hub image directly, click here.

Set some basic parameters

First, we’re going to set some parameters, so, in Bash, set the following variables. The registry shown is for Docker Hub, but you can use any registry you like. The repo name is the name of the repo you want to use for the image. The tag is the version of the image you want to use.

ANSIBLE_VER='6.7.0'
REGISTRY="cloudguyinbroadstone"
REPO="ansible-${ANSIBLE_VER}"
TAG='1.3'

Clone and build

Now, clone the repo for the Dockerfiles and requirements.txt

git clone git@github.com:clicktechnology/ansible4docker.git

..and cd into the cloned repo and build the docker image..

cd ansible4docker
docker build --pull --no-cache -t ${REPO}:${TAG} -f ${REPO}/Dockerfile ${REPO}

OK, so if that built properly, then we can test it by running the command..

docker run -it --rm -v .:/ansible ${REPO}:${TAG} /bin/bash

which should mount your current working directory on your host into the /ansible folder in the container and give a container prompt like this..

root@2305eb23752b:/ansible#

When you exit the container using exit, the container is automagically removed.

Add in AWS Credentials

So far so good. If (unusually) you haven’t installed the AWS CLI just yet, here’s the installation instructions and the setup details so get that done now.

Once done, add in your AWS credentials into the container by mounting a volume (-v) so the AWS credentials file in your host home directory under .aws appear in the container under /root/.aws.

docker run -it --rm -v ~/.aws/:/root/.aws/ -v .:/ansible ${REPO}:${TAG} /bin/bash

Now you can run your standard playbooks from inside the container just using all the normal commands, for example..

ansible-playbook hello_world.yml

Tag and push

You’re basically already finished. If however, you want to push the image to a repo, here’s how.

All you need to do is tag the image, login into your repo and then push the image to your repo. I’m using Docker Hub.

Login to your repo

To log into your repo, follow the repo instructions. For Dockerhub, here’s a one liner from the documentation.

cat ~/my_password.txt | docker login --username cloudguyinbroadstone --password-stdin

Tag image and push to repo

Tagging and pushing is a simple matter. Obviously, you’ll want to use your own repo for the Docker image, so just set the repo name in the original parameters at the start of this post. Then use the commands below to tag the image with a tag indicating a version number and latest.

docker tag ${REPO}:${TAG} ${REGISTRY}/${REPO}:${TAG}
docker tag ${REPO}:${TAG} ${REGISTRY}/${REPO}:latest

Now push the image to your repo.

docker push ${REGISTRY}/${REPO}:${TAG}
docker push ${REGISTRY}/${REPO}:latest

OK, now your image is available for use in pipelines and building images using a known standard for Ansible. The most useful usage is a an overlay to any directory where your playbooks are and you can start the image and then process a playbook directly and consistently.

Using the image

Simple. Running the command below will mount your current working directory on your host into the /ansible folder in the container and give a container prompt like this root@2305eb23752b:/ansible#. It also mounts your AWS credentials into the container so you can use the AWS CLI and Ansible to run playbooks etc.

docker run -it --rm -v ~/.aws/:/root/.aws/ -v .:/ansible ${REPO}:${TAG} /bin/bash

Docker Hub Image

So, you want to just pull and run?

docker pull cloudguyinbroadstone/ansible-6.7.0:latest
docker run -it --rm -v ~/.aws/:/root/.aws/ -v .:/ansible cloudguyinbroadstone/ansible-6.7.0:latest /bin/bash

Docker Hub