Open Source For You — December 2017

(Steven Felgate) #1

Admin How To


34 | DECEMBER 2017 | OPEN SOURCE FOR YOU | http://www.OpenSourceForU.com

Installing and working with Molecule is simple. Follow
the steps shown below. First, get the OS updated as follows:

sudo apt-get update && sudo apt-get -y upgrade

Next, install Docker:

sudo apt install docker.io

Now install Molecule with the help of Python Pip:

sudo apt-get install python-pip python-dev build-essential
sudo pip install --upgrade pip
sudo pip install molecule

After the install, do a version check of Molecule. If the
molecule version is not the latest, upgrade it as follows:

sudo pip install --upgrade molecule

It is always good to work with the latest version of
Molecule as there are significant changes compared to an
earlier version. Enabling or disabling modules in the latest
version of Molecule is more effective. For example, a common
problem faced is the forced audit errors that make Molecule
fail. When starting to test with Molecule, at times audit errors
can pose a roadblock. Disabling the Lint module during the
initial phase can give you some speed to concentrate on writing
tests rather than trying to fix the audit errors.
Here are a few features of Molecule explained, though the
full toolset offers more.


  1. Create: This creates a virtualised provider, which in our
    case is the Docker container.

  2. Converge: Uses the provisioner and runs the Ansible
    scripts to the target running Docker containers.

  3. Idempotency: Uses the provisioner to check the
    idempotency of Ansible scripts.

  4. Lint: This does code audits of Ansible scripts, test code,
    test scripts, etc.

  5. Verify: Runs the test scripts written.

  6. Test: Runs the full steps needed for Molecule, i.e., create,
    converge, Lint, verify and destroy.
    The roles need to be initialised by the following command:


molecule init role –role-name abc

This will create all the folders needed to create the role,
and is similar to the following command:

ansible-galaxy init abc

There are certain differences in the directory structure,
and some amount of manual folder creation may be
required.

Table 1: A comparison of folders created by Ansible and Molecule
Folders creat-
ed by Ansible-
galaxy init

Folders
created by
Molecule init

Remarks

Default Default No changes are
needed and folder
Handler Handler structure is identical
Meta Meta
README.md README.md
Tasks Tasks
Vars Vars
Files Files Folders needs to be
created manually
to take advantage
of file and template
module of ansible

templates templates

molecule All Molecule related
scripts and test
scripts are placed in
this folder

The Molecule folder has files in which one can put
in a pre-created Molecule playbook and test scripts. The
environment that is created is named default. This can be
changed as per the project’s requirements.
One file that will be of interest is molecule.yml, which
is placed at:

./molecule/default/molecule.yml
Another file which describes the playbook for the role is
playbook.yml placed at:

./molecule/default/playbook.yml

Note: Molecule initialises the environment called
default but engineers can use a name as per the
environment used in the project.

Figure 1: molecule.xml sample file
Free download pdf