Page
Test a sample Ansible role using Molecule
Now that you have started a workspace, it's time to test a sample Ansible role using Molecule.
Ansible Molecule is a project that aids in the development and testing of Ansible roles. Molecule provisions and manages test infrastructure that the automation can be tested against. In this example, we'll be testing automation for backing up files and using pods as the test infrastructure.
An example role is located here: collections/ansible_collections/sample_namespace/sample_collection/roles/backup_file
. This role generates a backup copy of a file and places it in an alternative directory (Figure 8).
A detailed description of the backup role can be found in the backup_file README.md. Let's use the Molecule project to test the backup_file
role. To do that in your workspace, select Terminal -> Run Task -> devfile -> 1.Molecule: start a test pod for the automation (Figure 9).
Doing this will start a test pod for the automation to run against, as defined in collections/ansible_collections/sample_namespace/sample_collection/extensions/molecule/default/molecule.yml
. (Figure 10).
You can use the following Kubernetes extension to check that the test pod was created: Clusters → Workloads → Pods → molecule-ubi8-init1 (Figure 11).
If you don’t see the installed extensions, refresh the workspace web page or execute another workspace command. To do that in your workspace, select Terminal -> Run Task -> devfile -> 2.Molecule: view the test instance that was created (Figure 12).
You can also access the OpenShift console directly from the workspace to inspect the test pod. To do this, select VS Code - Open Source Web then choose Dev Spaces: Open OpenShift Console (Figure 13).
The list of pods will contain your Molecule test pod (Figure 14).
The next step is running the verification against the test pod. To do that, press Terminal -> Run Task -> devfile -> 3.Molecule: run the verification against the test pod (Figure 15).
Running your verification will execute the molecule verify
command line under the hood. As a result, you will see failures like backup destination directory doesn't exist, which means you didn't create your backup file, and the Molecule tests failed.
To fix that, run the backup_file role against the instance provisioned by Molecule. Select Terminal -> Run Task -> devfile -> 4.Molecule: apply the role to the pod, which will run the molecule converge
command line under the hood and a backup file will be created in the test pod (Figure 16).
Check again to see if you created the backup file. Use the Kubernetes extension that has access to your namespace, then select the Terminal button on the test pod to make sure that the file exists.
In the terminal, execute cd/tmp/backups/
and check files in the folder ls -a
(Figure 17). You can also see the content of the hosts.bak file, cat hosts.bak
.
Now, when you apply the backup_file role to the test pod, run the 3.Molecule: run the verification against the test pod command one more time and your tests should pass (Figure 18).
If you’d like to run the full Molecule test without stepping through each stage, you can execute the 5.Molecule: run the full molecule test workspace command. Executing that command will automatically execute all of your Molecule test sequences (dependency, syntax, create, converge, idempotence, and verify) one by one.
Imagine integrating this workflow into the development of your own Ansible automation. You can easily create or enhance your roles and playbooks, and simply test them against lightweight, ephemeral infrastructure provided by OpenShift.
Congratulations! You've tested an Ansible role. Now it's time to explore Ansible Navigator.