This project gives a simple Ansible Docker image to run Ansible locally in a Docker container. From Docker side, it provides the Dockerfile to build the image and a Docker Compose to run the container. As examples, an inventory file is provided, with only one local execution node, and a Hello World! playbook is also furnished within this repository.
More information can be found here:
To build the image, run the following command:
docker build -t local/ansible .
The build will download ubuntu:22.04
and install the required packages to run Ansible. It will also create the
user ansible
and all the directories to mount local playbooks and inventory. Finally, it adds the following aliases to
make playbook run easily:
ansible-playbook='ansible-playbook -i /home/ansible/inventory.yml'
to use the local inventory as the default oneap='ansible-playbook
to quickly run playbooks
The image tag is local/ansible
.
To run the container, simply use the provided docker-composer configuration file:
docker-compose up -d ansible
To connect to the container, run the following command:
docker-compose exec ansible bash
Once run, you will be logged as ansible
user in the /home/ansible
directory.
With the legacy command:
cd playbooks
ansible-playbook hello-world.yml
As you notice, there is no need to provide the inventory location as an argument, as the ansible-playbook
is an alias
of the full command ansible-playbook -i /home/ansible/inventory/inventory.yml
With the short alias:
cd playbooks
ap hello-world.yml
Once run you might have an output like this:
PLAY [Hello World] ***************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [localhost]
TASK [Log Hello World!] **********************************************************************************************
ok: [localhost] => {
"msg": "Hello World!"
}
PLAY RECAP ***********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0