Ansible Molecule Config Reference


Percy Grunwald's Profile Picture

Written by Percy Grunwald

— Last Updated February 22, 2019

molecule.yml

The default/molecule.yml is the entry point for Molecule and contains the configuration for the default scenario:

# molecule/default/molecule.yml
---
dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: instance
    image: centos:7
provisioner:
  name: ansible
  lint:
    name: ansible-lint
scenario:
  name: default
verifier:
  name: testinfra
  lint:
    name: flake8

Understanding this file reveals a lot about how Molecule works, so it’s a good idea to step through it and give a short explanation of each item.

dependency

If your role has any dependencies, you can add a molecule/default/requirements.yml and Molecule will automatically run ansible-galaxy to download them on the test instance. If there is no requirements file, Molecule will simply skip this step.

dependency:
  name: galaxy

driver

The driver for your infrastructure such as docker, vagrant or ec2. docker is the default option (and best, in my opinion). This option determines how Molecule will create and connect to your instances.

driver:
  name: docker

lint

Molecule will automatically run a “linter” on your role files to make sure the YAML is valid, with yamllint being the default.

lint:
  name: yamllint

platforms

Molecule will create an instance for each platform in the list and test your role on each one. The configuration keys available for each item in the list will depend on the driver chosen. By default, Molecule will create and test on a single Centos 7 instance.

platforms:
  - name: instance
    image: centos:7

provisioner

Controls how Molecule provisions each instance with your role (ansible is the only option) and provides options for an additional layer of linting for Ansible best practices with ansible-lint.

provisioner:
  name: ansible
  lint:
    name: ansible-lint

scenario

Allows you to name and configure the test sequences for multiple scenarios. I won’t elaborate on scenarios now since they aren’t important for this tutorial, but know that every role will have a default scenario.

scenario:
  name: default

verifier

Configures the test runner that Molecule uses to verify the instances after the Ansible role has been applied. testinfra is a Python infrastructure unit testing tool and is the default test runner. There’s another lint configuration in this section as well, which by default uses flake8 to lint the Python test file at molecule/default/test/test_default.py.

verifier:
  name: testinfra
  lint:
    name: flake8