Test Ansible Roles with Molecule 3 and Red Hat Docker Images on Linux Mint

Here are instructions on how to set up Molecule 3 to test your Ansible roles using Docker images in Mint 20.2.

You can find some additional working code for this in my Github repos like abrt and CloudBees; these use official Red Hat images from the Red Hat Container Registry.

root@mint:/# apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common lsb_release

Most of these will already be installed, but this ensures you have everything you need.

root@mint:/# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Add the Docker repo:

root@mint:/# echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

If lsb_release returns something like ‘uma’ that has no Ubuntu equivalent, edit /etc/apt/sources.list.d/docker.list and change ‘uma stable’ to the Ubuntu parent release. e.g.:

deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu   focal stable 

Install the Docker packages and python-venv:

root@mint:/# apt update; apt install -y docker-ce docker-ce-cli containerd.io ansible python3.8-venv

At this point you might also want to edit /etc/group and add your non-root user account to the docker group.

Finally, try to run a Docker container.

root@mint:/# docker run hello-world
Hello World!

Now, switch to your non-root user account and go into your home directory. Set up a Python3 virtual environment:

me@mint:~$ python3 -m venv docker_env
me@mint:~$ source docker_env/bin/activate

Install all the Ansible and Molecule stuff in your venv:

(docker_env) me@mint:~$ python3 pip install wheel molecule-docker molecule docker ansible

Set up an Ansible role:

(docker_env) me@mint:~/github$ molecule init role ansible-test -d docker
INFO     Initializing new role ansible-test...
Using /etc/ansible/ansible.cfg as config file
- Role ansible-test was created successfully

Finally, try running one of the usual Molecule commands like `molecule create` to test the role. If that works, then you can try building out some full Ansible roles.


Leave a Reply

Your email address will not be published. Required fields are marked *