Using Molecule for testing an Ansible role to install a CommVault Commserve
Introduction
Molecule is a tool designed for testing Ansible roles. Molecule provides support for testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks and testing scenarios.
In this post we will learn how to use Molecule together with Vagrant, VirtualBox and Testinfra, to test an Ansible role used for installing a CommVault Comserve in Windows Server 2016. You can learn more about how Molecule works and how to set it up and use it in your own computer in the following guide:
Setting up Molecule for testing Ansible roles with Vagrant and Testinfra
Prerequisites
Molecule and the rest of tools and packages have to be installed:
- VirtualBox.
- Vagrant.
- Molecule.
- Molecule Vagrant plugin.
- Testinfra.
Once everything is installed and working we have to clone the CV_Automation repository.
How it works
In the project we can find the role install_commserve, used for installing a Commserve. Inside the role there is a molecule directory with the necessary files for the execution and configuration of Molecule:
- molecule/default/molecule.yml: Contains the definition of the platform driver (Vagrant) and the provider (VirtualBox), the VM OS settings, the provisioner (Ansible), testing tool used (Testinfra), the execution sequence scenario, etc.
---
dependency:
name: galaxy
driver:
name: vagrant
provider:
name: virtualbox
platforms:
- name: cvwindows
box: acastillo/winserver2016
box_version: 1.0.0
memory: 8192
cpus: 4
interfaces:
- network_name: private_network
ip: 192.168.50.20
config_options:
vm.communicator: "'winrm'"
provisioner:
name: ansible
inventory:
host_vars:
cvwindows:
ansible_become: False
ansible_user: vagrant
ansible_password: vagrant
ansible_connection: psrp
ansible_psrp_protocol: http
ansible_psrp_cert_validation: ignore
ansible_psrp_read_timeout: 140
ansible_port: 55985
disk: C
sw_path: /home/acastillo/Downloads/cv_11_sp18.zip
sw_dest_path: C:\tmp\
install_type: full
lint: |
set -e
yamllint .
ansible-lint
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy
verifier:
name: testinfra
options:
sudo: true
v: 1
This file is used to configure the automatic deployment of a VM using Vagrant and VirtualBox. The VM deployed is based on the Vagrant box acastillo/winserver2016, that has been created using a Windows Server 2016 image modified to have WinRM (communication plugin used by Ansible on Windows hosts) and uploaded to Vagrant Cloud to make it available to everyone who uses this project. The VM is going to have 8 GB of RAM, 4 CPU cores and a network interface with the IP specified.
The provisioner for the VM is Ansible and the inventory variables used for the role are defined in this file so Molecule can generate it.
The linting of the repository code is configured using YAMLlint and ansible-lint.
Besides, we can control the test sequence that Molecule will carry out when executing the molecule test
command, in this case we are using the default.
Finally, the verifier used is Testinfra, used to test the role functionalities in the VM by executing the Python tests defined in the tests directory.
- molecule/default/prepare.yml: Playbook for setting up the VM.
- molecule/default/converge.yml: Playbook for executing the role in the VM.
Molecule execution
To execute Molecule we have to move to the molecule directory inside the install_commserve role.
cd roles/install_commserve/molecule/
Executing the next command, the testing scenario test_sequence defined in molecule.yml will be executed:
molecule test
This process follows the next steps:
- Linting the code. Using YAMLlint and ansible-lint to check the syntax of the playbooks. The execution will stop if there are linting errors.
- Destroy the instance. If there is a VM already running with the same name, it will be destroyed.
- Create the instance. Creation of the Windows Server 2016 VM in VirtualBox using Vagrant.
- Prepare the instance by execution the prepare.yml playbook. In this case the playbook is empty, but we can use it to execute any task to prepare the VM for the role execution.
- Execute the converge.yml playbook. This playbook executes the install_commserve role, that will carry out the installation of the Commserve in the created VM.
---
- name: Converge
hosts: all
tasks:
- name: "Include install_commserve"
include_role:
name: "install_commserve"
- After the role execution, Testinfra tests are executed to verify that everything is working as expected. These tests are defined in the test_default.py file inside the tests directory and are used to check some things like: Commserve services are running and admin and web consoles are up and reachable.
import testinfra
hostname = "cvwindows"
def test_get_hostname(host):
global hostname
hostname = host.ansible("win_command", "hostname", check=False)["stdout_lines"][0]
def test_services(host):
required_services = ['GxMONGO', 'GXMMM', 'GXMLM', 'GxApM', 'GxTomcatInst', 'GxQSDK', 'GxCVD', 'GxClMgrS', 'GxJobMgr', 'GxFWD', 'GxEvMgrS']
result_services = host.ansible("win_shell", "Get-Service 'gx*' | Sort-Object status*", check=False)["stdout"]
result = all(service in result_services for service in required_services)
assert result == True
def test_admin_console(host):
admin_console = host.ansible("win_uri", f"url=https://{hostname}/adminconsole validate_certs=False timeout=120", check=False)["status_code"]
assert admin_console == 200
def test_web_console(host):
web_console = host.ansible("win_uri", f"url=https://{hostname}:443/webconsole validate_certs=False timeout=120", check=False)["status_code"]
assert web_console == 200
def test_web_server_port(host):
connection_result = host.ansible("win_shell", f"Test-NetConnection {hostname} -Port 81 |Format-List | Out-String -stream | Select-String 'TcpTestSucceeded'", check=False)["stdout"]
assert "True" in connection_result
- Destroy the VM. After testing that the role has been executed successfully and the tests have passed, the VM is destroyed. Besides, if something goes wrong during the execution of the test sequence, Molecule will automatically stop the execution and destroy the VM.
Summary
During this guide we have configured and executed Molecule to test the installation of a CommVault Commserve in a Windows Server 2016 host. As we have seen, Molecule offers the possibility to test an Ansible role in a testing environment to make sure it is working as expected before moving to a production environment.
Moreover, Molecule makes this process simple but quite powerful as we don't have to worry about going step by step because Molecule do everything automatically using Vagrant to orchestrate the creation and destruction of the VM executed on VirtualBox. Vagrant Cloud gives us the possibility to upload custom boxes containing the modified operating system we need to use in our testing and make them available for everyone, so any developer working in the project can use the box and download it in runtime.
In the next posts, we can find more detailed information about how to execute Molecule for testing two Ansible roles used to install a NetBackup Master Server in RHEL/CentOS/Azure and to execute a Disaster Recovery process on the server.
Using Molecule to testing the deployment of a Veritas Netbackup Master Server with DR Process