In the era of Big Data many popular tools are built to scale out by spliting the workload over multiple hosts. Tools like Hadoop, Spark, Zookeeper, Solr, and MongoDB can be deployed in standalone and clustered modes. Writing and testing Ansible roles for these tools is difficult, especially if you are writing and testing the Ansible roles on your personal desktop or laptop with limited resources.
In order to write and test Ansible roles for multiple hosts/clusters, it is important to use tools that are efficient and automated, otherwise you will spend your time manually creating and destroying your development and test environments. Luckily, the Molecule project for Ansible is the perfect tool for this use case because it automates every part of the development and testing lifecycle and it uses lightweight Docker containers to quickly create and destroy the test environment. Unfortunately, the documentation for Molecule isn’t clear about how you can go about setting up multiple development and test scenarios (standalone & cluster) or how to setup multiple hosts and configure the host groups and host variables.
In this example, I will show how I used Molecule for creating and testing an Ansible Role for Apache Zookeeper that will deploy Zookeeper in either standalone or clustered mode. The full Ansible role is at https://github.com/kevincoakley/ansible-role-zookeeper .
First, I will show how to create one scenario for standalone mode and one scenario for clustered mode. We will keep the default scenario for standalone mode and create a second scenario for clustered mode. Simply copy the molecule/default
directory and all of its files to molecule/cluster
.
.
└── molecule
├── cluster
│ ├── converge.yml
│ ├── molecule.yml
│ └── verify.yml
├── default
│ ├── converge.yml
│ ├── molecule.yml
│ └── verify.yml
└── yaml-lint.yml
Once that is done, edit molecule/cluster/molecule.yml
and update the name
variable underscenario
YAML node to cluster
, like so:
scenario:
name: cluster
That is it! If you want to test the default scenario you can run molecule test -s default
, run the cluster scenario with molecule test -s cluster
or run both with molecule test --all
.
#cluster #ansible #ansible-roles #multi-host #molecule #testing