Giter Site home page Giter Site logo

ansible-molecule-example's Introduction

Testing Ansible with Molecule

This repository is an example of how to test Ansible roles with Molecule You will find a couple of ansible roles that are already initialized with molecule and are ready for experimenting. Not all molecule features are covered with this example project but you will be able to test your role against different OS versions and see the Unit tests in action.

The role ossec provides you with two example molecule.yml files one for a docker driver and a second one (molecule.yml.vagrant) for testing against a vagrant box. Just rename them to activate your desired configuration. By default the docker version is active.

Prerequisites

To use this repo on your local machine you need the following environment

  • Python incl. pip
  • Ansible
  • Docker
  • Vagrant

Install the following python modules with pip

pip install molecule docker python-vagrant

TL;DR - Molecule in Action

Clone the repo

git clone [email protected]:MadZimbo/ansible-molecule-example.git  && cd ansible-molecule-example.git 

cd into a role, for example check_mk

cd roles/check_mk

Run molecule

molecule test

Molecule

Molecule is a testing framework for Ansible that helps you to write better playbooks.

Molecule has different ways of helping you to write the best playbooks in the world

  • Syntax checking for your yaml files and Ansible code
  • Initialize new roles and generate the directory layout
  • Test your playbooks against different OS versions
  • Test different Ansible versions
  • Unit testing

HowTo

Create a new role or 'moleculeize' an existing role

Create a new Ansible role with molecule

molecule init role --role-name foo

Will create a new role directory layout for the role foo

If you already have a role, you can add a molecule scenario with

cd foo ; molecule init scenario  --role-name foo

In both cases you will find a directory molecule where the configuration for your role resides

Molecule Configuration

The interesting part happens in <yourRole>/molecule/default/tests/molecule.yml

Here you can define which platform driver to use for example docker or vagrant. There are a lot of other drivers available like EC2, GCE, LXE, Azure... Example:

driver:
  name: docker

You can configure against which OS versions you want to test

platforms:
  - name: Ubuntu-Latest
    image: ubuntu:latest
  - name: CentOS-7
    image: centos:7

You can define which steps to execute in your testplan when executing

molecule test

By default molecule would run all steps

└── default
    ├── lint
    ├── destroy
    ├── dependency
    ├── syntax
    ├── create
    ├── prepare
    ├── converge
    ├── idempotence
    ├── side_effect
    ├── verify
    └── destroy

If you want to skip, lets say the idempotence test change the molecule.yml part to

scenario:
  name: default
  test_sequence:
    - lint
    - destroy
    - dependency
    - syntax
    - create
    - prepare
    - converge
    - side_effect
    - verify
    - destroy

Molecule would then skip the idempotence task and does not try to discover if a playbook is idempotent

Unit Test

Molecule supports unit testing with testinfra. When you create or initialize a role with molecule it creates a tests directory inside the molecule directory of your role. A default test is generated as well, check the test_default.py file

You can find more tests in the provided roles, check out the <role>/molecule/default/tests directory

Files inside the tests directory that are prefixed with test_ are considered to be tests and are executed by molecule

Runing a unit test

Gitlab CI

Pitfalls

In most cases you test either against a docker container or a Vagrant box A Vagrant box is a full Virtual Machine a docker container is just isolation at the OS level.

This can lead to some "problems" if you test roles that rely on things like a init system In a default docker container there is normaly no init system available. Tasks that restart a service would fail. You can get around those problems with special docker images that are include a init system or you use a vagrant box, which is a complete virtual machine.

ansible-molecule-example's People

Contributors

madzimbo avatar zimboniker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.