1. I use a base docker image that already has ansible installed.
2. In your Dockerfile, use the COPY command to copy your ansible files
(playbook yaml, files, etc.) into the container. Something like:
COPY ansible/ /tmp/ansible/
3. After the copy, in your Dockerfile, run ansible-playbook on the
files you copied into the container in the previous step:
RUN ansible-playbook /tmp/ansible/foo.yml -i /tmp/ansible/hosts
In this case, /tmp/ansible/hosts just contains:
[local]
127.0.0.1
And in the playbook, I specify:
- hosts: local
connection: local
4. After the playbook completes, remove the ansible files you copied
in during step 2.
I hadn’t thought of using Ansible to create Docker images before, but this is an interesting way to do this, instead of using a complicated Dockerfile to manage each variety of service.
I might want to figure out a way to be able to build an image for each group in your hosts file.