Páginas

Thursday, 3 September 2020

Testing Ansible Roles with molecule



Molecule is a complete testing framework that helps you develop and test Ansible roles. Without it, you would have to provision and maintain a testing environment separately.

Molecule uses drivers to provision testing instances using different technologies, including Linux containers, virtual machines and cloud providers. By default, it comes with three drivers pre-installed: Docker and Podman drivers to manage containers, and Delegated that allows you to customize your integration.

We will use the Podman driver to develop and test a new role using Linux containers. Podman is a lightweight container engine for Linux that does not require a running daemon, and allows execution of containers in “rootless” mode for increased security.


Install the container-tools module, which pulls in the full set of container software packages:

# yum module install -y container-tools
Install the podman-docker package. That package installs a link that replaces the docker command-line interface with the matching podman commands instead.

# yum install -y podman-docker


Try a podman command. Set the http_proxy variable if needed



Create a dedicated Python environment for our Molecule installation:

$ mkdir molecule-blog 
$ cd molecule-blog 
$ python3 -m venv molecule-venv 
$ source molecule-venv/bin/activate

(molecule-venv) $ pip3 install --proxy=http//10.90.11.18:8080 "molecule[lint]"



Initializing a New Ansible Role

Use molecule to initialize the new role



Molecule created the infraestructure for the new role in a directory named "mywebapp"





Molecule adds a single scenario named default and a configuration file is created under that directory. You can create more scenarios different to the default one.
Include the lint configuration at the end of the file




Run molecule lint and fix the errors that shows up in the file meta/main.yml


The role is initialized and the molecule configuration is in place.


Setting up instances

Modify the molecule/default/molecule.yml configuraction file and add the instance for RedHat.
We are mounting temporary filesystems /tmp and /run as well as the cgroup volume.
We are also enabling the "SYS_ADMIN" capability (required to run a container with systemd)




Modify parameters for provisioning by modifying the provisioner dictionary



Load the environment


Go to the directory


Create the instance by running molecule create

If by any chance, the instance is not created properly you can delete it by running the command molecule destroy

When it is finished , you can check the instance, running molecule list



Verify that the container is running




Testing with molecule

Add a task to your tasks/main.yml file


Test it against the instance by running molecule converge



Another example. Install a java package

Add the java.yml file into the tasks/main.yml 

Create java.yml file



Take into account that if you are behind a proxy, you need to configure the proxy in the instance to be able to install a package.
Run molecule login and edit the following file to add the proxy configuration




Run molecule converge




Run molecule verify