Skip to content

Instantly share code, notes, and snippets.

@mjuenema
Last active November 28, 2016 03:05
Show Gist options
  • Select an option

  • Save mjuenema/9ac6262167af2d02ff168be39bb7d6d7 to your computer and use it in GitHub Desktop.

Select an option

Save mjuenema/9ac6262167af2d02ff168be39bb7d6d7 to your computer and use it in GitHub Desktop.
Use Molecule and Docker to test Ansible roles

Use Molecule and Docker to test Ansible roles

Install pre-requisites.

$ which yum && sudo yum install libffi-devel python-devel python-virtualenvwrapper
$ which apt-get && sudo apt-get libffi-dev python-dev python-virtualenvwrapper

Install Docker.

$ TODO

Create a virtualenv with needed packages.

$ source /usr/bin/virtualenvwrapper.sh
$ workon molecule || mkvirtualenv molecule
(molecule)$ pip install --upgrade pip
(molecule)$ pip install --upgrade jinja2-cli docker-py molecule ansible ansible-lint ansible-MORE
  • Create a .travis.yml template file for Travis-CI.

  • Create the molecule.yml.j2.

  • Create a Makefile and use it.

    make ubuntu_1610 make all

---
# .travis.yml
#
# https://molecule.readthedocs.io/en/latest/usage.html#travis-ci
sudo: required
language: python
env:
matrix:
- TARGET=config_ubuntu_1610
- TARGET=config_ubuntu_1604
- TARGET=config_ubuntu_1404
- TARGET=config_centos_6
- TARGET=config_centos_7
- TARGET=config_debian_7
- TARGET=config_debian_8
services:
- docker
before_install:
- sudo apt-get -qq update
- sudo apt-get install -o Dpkg::Options::="--force-confold" --force-yes -y docker-engine make
install:
- pip install jinja2-cli
- pip install testinfra
- pip install docker-py
- pip install ansible
- pip install molecule
- pip install ansible-lint
before_script:
- make ${TARGET}
script:
- molecule syntax
- molecule create
- molecule status
- molecule converge
after_success:
#- molecule idempotence
- molecule verify
molecule_steps := syntax create status converge verify
all: ubuntu_1610 ubuntu_1604 ubuntu_1404 centos_6 centos_7 debian_7 debian_8
ubuntu_1610: config_ubuntu_1610 $(molecule_steps)
ubuntu_1604: config_ubuntu_1604 $(molecule_steps)
ubuntu_1404: config_ubuntu_1404 $(molecule_steps)
centos_6: config_centos_6 $(molecule_steps)
centos_7: config_centos_7 $(molecule_steps)
debian_7: config_debian_7 $(molecule_steps)
debian_8: config_debian_8 $(molecule_steps)
config_ubuntu_1610:
echo '{}' | jinja2 -DIMAGE_NAME=ubuntu -DIMAGE_VERSION=16.10 molecule.yml.j2 > molecule.yml
config_ubuntu_1604:
echo '{}' | jinja2 -DIMAGE_NAME=ubuntu -DIMAGE_VERSION=16.04 molecule.yml.j2 > molecule.yml
config_ubuntu_1404:
echo '{}' | jinja2 -DIMAGE_NAME=ubuntu -DIMAGE_VERSION=14.04 molecule.yml.j2 > molecule.yml
config_centos_6:
echo '{}' | jinja2 -DIMAGE_NAME=centos -DIMAGE_VERSION=6 molecule.yml.j2 > molecule.yml
config_centos_7:
echo '{}' | jinja2 -DIMAGE_NAME=mjuenema/centos7 -DIMAGE_VERSION=latest molecule.yml.j2 > molecule.yml
config_debian_7:
echo '{}' | jinja2 -DIMAGE_NAME=debian -DIMAGE_VERSION=7 molecule.yml.j2 > molecule.yml
config_debian_8:
echo '{}' | jinja2 -DIMAGE_NAME=debian -DIMAGE_VERSION=8 molecule.yml.j2 > molecule.yml
$(molecule_steps):
molecule $@
---
# molecule.yml.j2
#
# This file will be converted to molecule.yml in .travis.yml.
ansible:
verbose: 'vvvv'
driver:
name: docker
docker:
containers:
# IMAGE_NAME and IMAGE_VERSION
- name: ansible-role-postinstall
image: {{IMAGE_NAME}}
image_version: '{{IMAGE_VERSION}}'
{% if IMAGE_NAME in ['mjuenema/centos7'] -%}
privileged: true
volume_mounts:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
cap_add:
- SYS_ADMIN
{% endif -%}
verifier:
name: testinfra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment