Let’s do a test of Ansible Role with Molecule in OpenStack

sky_joker@Ansible x VMware
3 min readJan 6, 2021

--

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.15
  • molecule is 3.2.1
  • openstack-driver is 0.3
  • Python 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 name
  • flavor is flavor name to use
  • network is network name to be attache of an instance
  • fip_poo is network name for using floating IP
  • ssh_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 url
  • username is a tenant username to log in
  • password is a tenant user password to log in
  • project_name is a project name which the user belongs
  • project_domain_id is a domain name to which the project belongs
  • user_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 :^)

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

sky_joker@Ansible x VMware
sky_joker@Ansible x VMware

No responses yet

Write a response