Let’s do a test of Ansible Role with Molecule in OpenStack
In this article, I introduce how to a role test for Ansible in OpenStack with the molecule.
The molecule is a framework for Ansible Role unit test.
It has some drivers(vagrant, ec2, and so on) for test environment preparation.
The OpenStack driver is one of them.
Test Environment Information
This time, I used the following version in my lab.
OpenStack
is 4.0.1(Kolla)ansible
is 2.9.15molecule
is 3.2.1openstack-driver
is 0.3Python
is 3.6.8
I created the tenant network the following.

The test environment has the following images.
This time will use the CentOS7 image.
$ glance image-list
+--------------------------------------+---------+
| ID | Name |
+--------------------------------------+---------+
| f44c58da-da37-4c9c-a157-975d3baf047e | CentOS7 |
| 705c4b76-8280-476c-982d-c9ba0a625310 | cirros |
+--------------------------------------+---------+
How to prepare an environment of the molecule
Install dependence python libraries
The following procedure will install the required libraries.
(venv)$ pip install ansible==2.9.15 molecule molecule-openstack openstacksdk
Role creation for a test
Next, will create a role for using a test in the molecule.
I created a role example for the following.
(venv)$ mkdir -p roles/example/tasks
(venv)$ vi roles/example/tasks/main.yml
---
- name: Execute ping
ping:
Prepare molecule scenario
When using the molecule, needs to create a test scenario.
To create it, will give init scenario
option to the module command after the move to the role directory.
And please specify the OpenStack driver.
(venv)$ cd roles/example/
(venv)$ molecule init scenario -d openstack
(venv)$ ls
molecule tasks
(venv)$ ls molecule
default
In a standard, the default directory is created.
Change the molecule configuration
Open the molecule configuration file and change the contents based on your environment.
The following is an example.
(venv)$ vi molecule/default/molecule.yml
---
dependency:
name: galaxy
driver:
name: openstack
platforms:
- name: instance1
image: CentOS7
flavor: sample
network: 192.168.200.0/24
fip_pool: external
ssh_user: centos
provisioner:
name: ansible
env:
MOLECULE_EPHEMERAL_DIRECTORY: /root/dev/roles/example/molecule/default
verifier:
name: ansible
The driver specifies the openstack
.
The platforms will set for the test instance parameters.
name
is instance nameflavor
is flavor name to usenetwork
is network name to be attache of an instancefip_poo
is network name for using floating IPssh_user
is a user name when using SSH connection
Create an authentication file for OpenStack
Create an authentication file in $HOME/.config/openstack
for OpenStack to be used during testing
(venv)$ mkdir ~/.config/openstack
(venv)$ vi ~/.config/openstack/clouds.yaml
clouds:
devstack:
auth:
auth_url: http://ctrl-server01:5000
username: myuser
password: secret
project_name: myproject
project_domain_id: default
user_domain_id: default
auth_url
is keystone urlusername
is a tenant username to log inpassword
is a tenant user password to log inproject_name
is a project name which the user belongsproject_domain_id
is a domain name to which the project belongsuser_domain_id
is a domain name which the user belongs
Execute the role test
Execute the molecule test in the role directory.
(venv)$ ls
molecule tasks
(venv)$ molecule test
The key pairs is automatically created using an instance name in test instance creation tasks.
When the executed test hasn’t a problem, it will be connected via SSH to a test instance.
In this way, can do easily for the role test in the OpenStack environment with the molecule by using the OpenStack driver.
That is all, Happy automation :^)