Five years ago, I had developed an Ansible role that facilitates the
installation and basic configuration of Plesk for Linux. Plesk is a commercial web hosting control
panel and it is widely adopted in various shared web hosting companies.
According to Plesk documentation, the installation process is straightforward; Download and run an installation script locally, that would guide you through the installation process.
By passing some parameters to the installation script, installation is performed unattended.

The goal of this Ansible role was to perform an unattended installation for pre-determined Plesk version and also perform some post-installation tasks (such as run some initial configuration steps for Plesk or install a Plesk license).
The role, named ansible-plesk is available in Ansible Galaxy, and its source code is available in
Github. I made this role just for experimentation in order to learn Ansible.

In order to test the role, I used to spin a virtual machine (usually CentOS 7) and run the role against this machine.
During its installation, Plesk performs numerous changes on the underlying host (install services such as dovecot, httpd, mysql etc.) hence the only option to ensure that the role works as expected, was using a full-blown virtual machine.
This method was cumbersome and also required some resources.
In particular, the virtual machine should be created, destroyed, recreated across tests.
Not to mention the possibility of using a different OS version (CentOS 6) or even a different linux distribution.
In other words I would seek for a solution to test Ansible roles, where the testing environment would be as close as a virtual machine but without all the maintenance hassle.

Recently, I came across a blog post by Jeff Geerling, with regards to test his Ansible roles with Molecule.
Jeff has developed a variety of Ansible roles available on Ansible Galaxy as well as is the author of the “Ansible for DevOps” book. As I wanted to give molecule a try, I thought that the ansible-plesk role is a good candidate.
Besides, the code had been left untouched for years, so it was a perfect opportunity to perform a refactoring, if needed. Following instructions omit lots of details for brevity.

I started with installing molecule on my laptop using pip

$ pip install molecule molecule[docker]

Then I created the molecule/default directory in the role. molecule directory contains all tests (called “scenarios”) for that role.
Molecule expects that each role has a default scenario, hence the default directory. Core configuration exists under molecule.yml.

  name: docker
  - name: instance
    image: geerlingguy/docker-centos7-ansible:latest
    command: ""
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    privileged: true
    pre_build_image: true
  name: ansible
    name: ansible-lint

Molecule can use Docker containers as test platforms. In a nutshell, you can use a pre-built Docker container (e.g a Centos container with systemd) where your role would run. Jeff also maintains some base Docker images tailored for the purpose.
Setting privileged: true is inevitable when our role is performing changes on systemd units (which in our case, it does).

The next step was to create a playbook that invokes the role. Molecule expects a specific playbook called converge.yml to be present.
This playbook is invoked by molecule and is run against the instance defined on molecule.yml.
It could be as simple as the following

- name: Converge
  hosts: all
    - ansible-plesk

We are now ready to run our Ansible role with molecule. Simply run

molecule test

This would create the testing environment, invoke converge.yml to run the role, re-invoke converge.yml to test whether our role is idempotent or not and finally destroy the testing environment.
testalso performs other tasks as well (such as linting).

By incorporating these changes to ansible-plesk, I managed to successfully migrate my role testing to Molecule. Moreover, I can now test my changes locally with minimal effort.
Incorporating molecule with an existing role was also really simple, if you wish some basic testing to your role.
I even found an issue when the role was running on Ansible 2.9 and pushed the relevant fix to upstream.

All in all, molecule combined with pre-built Docker containers by Jeff Geerling is a compelling option when it comes to test Ansible roles.
I would suggest reading the blog post for a more detailed approach on how to use molecule.