What is molecule?
No one is perfect. We all make mistakes but
we can avoid many of them with careful planning.
Molecule project is designed to aid in the
development and testing of Ansible roles.
Molecule provides support for testing with
multiple instances, operating systems and distributions, virtualization
providers, test frameworks and testing scenarios.
Using Molecule is much simpler. We can create multiple scenarios for testing
configuration variances and we can use Docker images for all the common
Operating Systems and versions ensuring they are all thoroughly tested.ng they are all thoroughly tested.
Installation
Molecule is written in python and distributed as a pip. In
most cases it can be easily installed with the pip command. I am using Docker to test my roles and I
therefore need to install the Molecule package with Docker support.
pip install
molecule
pip install
'molecule[docker]'
If you have trouble with the installation process check the
very well documented steps here.
I’m going to
make this section quite practical so you can follow through the steps and set
up your own roles with Molecule.
First thing
is to initialise the role. There are two different ways depending on whether
you’re updating an existing role or creating a brand new role.
How to create New Role
The Molecule init command creates not
just the Molecule directory but also the
Ansible one.
Update Existing
Let’s first of all examine the contents of
the molecule directory:
The first thing you’ll notice is there is a
default directory. This defines your test scenario. For instance, it allows you
to test the same role using different configurations. Inside the default
directory there are several files. We’re interested in just the molecule.yml and playbook.yml
Let’s look first at molecule.yml
---
# use Ansible Galaxy
to get other modules
dependency:
name: galaxy
# we'll be running
our tests inside a docker container
driver:
name: docker
# linter to check
file systax; try "molecule lint" to test it
lint:
name: yamllint
# docker images to
use, you can have as many as you'd like
platforms:
- name: instance
image: centos:7
# run ansible in the
docker containers
provisioner:
name: ansible
lint:
name: ansible-lint
# Molecule handles
role testing by invoking configurable verifiers.
verifier:
name: testinfra
lint:
name: flake8
And this is playbook.yml which
is a standard Ansible playbook to invoke your role.
---
- name: Converge
hosts: all
roles:
- role: nginx
First test
You can test your initial setup simply by running molecule
create. This command creates the docker images where Ansible will be running
your playbooks. Another handy command is ‘molecule login’ to open a shell in to
the running docker container for you to
perform debugging for example
First role
I’m going to assume you are creating a new
role for this lab. I’m doing a simple nginx installation. My
first change is to make the role install nginx when it runs,
adding the following to tasks/main.yml
You may have noticed the default image on
molecule is centos7. Feel free to change it to whatever image you prefer but I’m
sticking to defaults for the moment and I’ll be showing how to test for
multiple distributions shortly.
- name:
Install EPEL
package:
name: epel-release
state: present
when: ansible_os_family == "RedHat"
- name:
Install nginx
package:
name: nginx
state: present
Now you can simply run ‘molecule
converge’ and the default scenario will run to test your role.
As you can see the role completed
successfully!
You can use the command ‘molecuel test’ instead which would run through every single
step in molecule such as linting, converging, clean up, destroy, etc. But for
our tests converge alone is much faster and lint is unlikely to succeed as we
haven’t edited the meta/* configurations.
Testing for multiple Operating System
The above role has only been tested for
CentOS version 7. But this role may be used on Ubuntu or Debian and we should
therefore test them as well. We do this by adding the different images to the
platforms section in the molecule.yml file config
platforms:
- name: CentOS-7
image: centos:7
- name: Ubuntu-18
image:
ubuntu:18.04
As simple as that the’molecule converge’
command will now create two docker images, one for CentOS 7 and one for Ubuntu
18.08 and run Ansible with our brand new role in both.
You will need to execute molecule
destroy after you change the molecule.yml for
Molecule to pick up the new configurations.
Conclusion
Molecule can be of great help to ensure your
roles are up to the best of standards before you tag them for use. It will help
you ensuring quality code is used and it works (at least in isolation). It
doesn’t mean it will be all perfect, you will rarely be running a single role
on your playbook but it should simplify your debugging when problems occur.