Use Case

On my current project we are extracting data from Kubernetes (Openshift) to determine the cost of running a cluster. We utilize Operator Metering which pulls metrics from Prometheus to glean usage information. We constructed a project, Korekuta, which utilizes Ansible to perform two tasks:

(1) setup the operator metering reports and store configuration data

(2) collect data from the operator metering API and send it to cloud storage for processing

Ansible Playbook for Korekuta

In order to accomplish this, two roles (setup and collect) were created and tagging is used in the playbook to trigger each role.

Molecule Setup

As stated in the introductory article, Molecule works with a variety of drivers from Docker to Vagrant and Virtualbox and more. The beauty of the setup process is that it uses Ansible, something you are clearly familiar with, to prepare the test system. As you construct a role for testing you can add a prepare.yml that acts as a playbook for the test system setup. The prepare.yml sits in the <role>/molecule/default/ directory and is triggered by the Prepare step of the Molecule test process.

For the Korekuta setup role the Prepare step does the following:

Installs the Openshift client tools using an existing role from Ansible Galaxy Creating the presence of a command line tool Creating a file in a known location to be used as an extra variables input Creating a simple web server to respond with expected payloads

Molecule’s integration with Ansible Galaxy allows you to piggyback on all the great roles others have previously constructed. Unfortunately, I found the documentation around this feature to be lacking so for an example you can see the usage of the role below:

Executing a role from Ansible Galaxy

In order to utilize this role you must specify your role’s dependency on it by adding a requirements.yml to the <role>/molecule/default/ directory. For Korekuta the requirements.yml is as follows:

Requirements to utilize an Ansible Galaxy role.

With these small steps you can take advantage of the vibrant eco-system of Ansible Galaxy to make test system setup a breeze.

Molecule Test Execution

Molecule again leverages the domain the user is familiar with by having the “test runner” for a role be a playbook. In the section above I mentioned the <role>/molecule/default/ directory, however the default directory is just one test case, so to generalize its actually <role>/molecule/<test_case>/; and each of these has a playbook.yml that will be executed in the Converge step. With this construction you can setup a variety of test flows for your role along with specialized setup as mentioned in the previous section.

The playbook.yml for Korekuta’s setup role is fairly simple, however, its “runtime” execution depends on the user supplying extra variables to the command line using the “-e VARIABLE=VALUE” mechanism of Ansible. For test purposes we can use the vars option in a playbook to provide these input values and then have multiple test cases to vary the inputs with different playbooks, as seen in the example.

As mentioned in the intro article, Molecule allows you to test expected outcomes of the test case and setup a continuous integration flow. Since these are covered clearly enough between the article and documentation I’ll just link to these items in Korekuta for completeness.

Synopsis

In this article we’ve covered, how Ansible roles can be tested using Molecule along with some high-level resources. Next we dove into a project utilizing Molecule and discussed how test setup and execution work along with including tips for employing Ansible Galaxy roles and driving extra-vars flows with a multiple of test cases. Hopefully the resource associated with this practical application of testing an Ansible role provide value to you.

Happy Automating!