Testing Ansible Playbook in a Docker Container

I am considerably new with Ansible and wanted to verify the changes I have made to an existing Ansible project. I found a nifty solution to my problem. I am now running the Ansible playbook inside a docker container. I got inspiration from couple of places that I should give credit to:

So what I have done is, I am creating a Docker container with the open-ssh server, sudo and python installed. Then I am adding an inventory for the docker container. Finally I am executing a bash script where I am building my image, running the container and then running the playbook on the container. I will show you just how I did it.

My Ansible project structure looks like this:

The Dockerfile looks like this:

The content of my local_docker file looks like this:

The ansible-test is the name of my container that I will create in my container-start-and-playbook-run.sh script. ansible_connection=docker is the part that tell Ansible that it needs to run the playbook inside the ansible-test container.

Now the content of myplaybook will need to refer to this local inventory that I have created. Like so:

Finally the content of the bash script that puts everything together is:

This has been greatly helpful for me, as I have zero confidence in my Ansible skills. Hope someone finds it helpful.

I am a software developer, who is easily intrigued by anything tech and is always thriving to make software development more inclusive and empathetic.