~ 2 min read

Ansible role testing with multiple Linux distros, using GitLab Container Registry

Written by Brie Carranza

Here's how I did it.

What are we trying to accomplish?

Molecule is an excellent tool for building and testing Ansible roles! It frees you from writing boilerplate and helps you get a solid Ansible role in place quickly!

There is something about Molecule that I wanted to change:

By default, a CentOS container is defined for testing. What if I want to test against multiple operating systems? What if I want to bring my own Docker container? Follow along to see how I answer these questions.

Overview

The solution that I came up with has a few parts:

  • Build a Docker container
  • Upload container to GitLab Container Registry
  • Configure Molecule to use containers from GitLab Container Registry

Build a Docker container

This is fairly straightforward but here’s an overview. You could put a few lines like this in Dockerfile for a minimal example:

FROM ubuntu:focal

RUN apt-get -y update && apt-get -y  upgrade && apt-get -y install python3

See the Dockerfile best practices for more guidance on building your own Docker containers.

Upload your container to GitLab Container Registry

My GitLab account has 2FA enabled so I used a GitLab personal access token to authenticate. First, I generated a Personal Access Token in the GitLab Web interface. Then I set $GITLAB_PERSONAL_ACCESS_TOKEN to contain my token and authenticate to the GitLab container registry like so:

GITLAB_PERSONAL_ACCESS_TOKEN=abcdef012345
docker login -u brie -p $GITLAB_PERSONAL_ACCESS_TOKEN registry.gitlab.com

At this point, you can iterate on the container recipe until the built image works the way that you want. To push that image to the container registry, run this command in the directory that contains Dockerfile:

docker build -t registry.gitlab.com/brie/ubuntu-container-with-python .

Configure Molecule to test your container

The Molecule quick start guide is a great place to get started. I will assume that you have already installed Molecule and initialized a role. You should be able to run molecule create without errors. The images that the roles is tested against are defined in molecule/default/molecule.yml. Extending the platforms section is how you add additional operating systems. By default, the platforms section will look like this:

platforms:
  - name: instance
    image: docker.io/pycontribs/centos:7
    pre_build_image: true

I wanted to test against CentOS 8 and the container created above. To do this, I extended the platforms section to look like this instead:

platforms:
  - name: cent7
    image: docker.io/pycontribs/centos:7
    pre_build_image: true
  - name: cent8
    image: docker.io/pycontribs/centos:8
    pre_build_image: true
  - name: focal-gitlab
    image: registry.gitlab.com/brie/ubuntu-container-with-python
    pre_build_image: true

Put it all together!

Now, I will run molecule create and watch all three of my instances get spun up!