Testing Ansible Roles with Molecule
Testing Ansible Roles with Molecule
May 26, 2022
Molecule
A test framework for Ansible roles maintained by ansible-community. Molecule supports testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks, and test scenarios.
Installation
Installing with pip can tangle the dependencies of the system Python, so creating a virtual environment with Virtualenv or using a dependency manager like Pipenv or Poetry is recommended.
| |
Writing Tests
- Create the
/path/to/role/molecule/defaultdirectory. (default is the default scenario. Scenarios with other names can be added.) - Create
/path/to/role/molecule/default/molecule.ymland enter the following.
| |
- Create
/path/to/role/molecule/default/converge.ymland add the provisioning code
Insert the code that runs the Role to set up the environment after the docker container is created. (Deploying a web server, etc.)
| |
- Create
/path/to/role/molecule/default/verify.ymland add the verification code
Insert the code that verifies the environment after it’s set up (this is where the actual testing happens)
| |
Running
| |