Skip to content

Instantly share code, notes, and snippets.

@neilus
Last active June 27, 2017 17:40
Show Gist options
  • Save neilus/04d57598c1d64368dd829738d88621dd to your computer and use it in GitHub Desktop.
Save neilus/04d57598c1d64368dd829738d88621dd to your computer and use it in GitHub Desktop.
Sample moleculeV2 scenario
tests/
.molecule/

Sample Molecule v2 Scenario

to spin this up clone this folder under the molecule dir in your role:

(my-role) $ git clone https://gist.github.com/04d57598c1d64368dd829738d88621dd.git molecule/sample
(my-role) $ molecule list
---
- hosts: localhost
connection: local
gather_facts: no
vars:
molecule_file: "{{ lookup('env','MOLECULE_FILE') }}"
molecule_scenario_directory: "{{ lookup('env','MOLECULE_SCENARIO_DIRECTORY') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
tasks:
- name: Build an Ansible compatible image
docker_image:
path: "{{ molecule_scenario_directory }}"
name: "{{ item.image }}"
dockerfile: "{{ item.dockerfile }}"
with_items: "{{ molecule_yml.platforms | molecule_dict_at_index(0) }}"
- name: Create molecule instance(s)
docker_container:
name: "{{ item.name }}-{{ molecule_yml.scenario.name }}"
hostname: "{{ item.name }}"
image: "{{ item.image }}"
state: started
recreate: no
log_driver: syslog
command: "{{ item.command }}"
with_items: "{{ molecule_yml.platforms }}"
---
- hosts: localhost
connection: local
gather_facts: no
vars:
molecule_file: "{{ lookup('env','MOLECULE_FILE') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
tasks:
- name: Destroy molecule instance(s)
docker_container:
name: "{{ item.name }}-{{ molecule_yml.scenario.name }}"
state: absent
with_items: "{{ molecule_yml.platforms }}"
FROM ubuntu:trusty
RUN 'apt-get update -qq && apt-get install -qqy python sudo bash'

Install

This set of playbooks have specific dependencies on Ansible due to the modules being used.

Requirements

  • Ansible 2.2
  • Docker Engine
  • docker-py

Install OS dependencies on CentOS 7

$ sudo yum install -y epel-release
$ sudo yum install -y gcc python-pip python-devel openssl-devel
# If installing Molecule from source.
$ sudo yum install libffi-devel git

Install OS dependencies on Ubuntu 16.x

$ sudo apt-get update
$ sudo apt-get install -y python-pip libssl-dev docker-engine
# If installing Molecule from source.
$ sudo apt-get install -y libffi-dev git

Install OS dependencies on Mac OS

$ brew install python
$ brew install git

Install using pip:

$ sudo pip install ansible
$ sudo pip install docker-py
$ sudo pip install molecule --pre
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: ansible-lint
platforms:
- name: whatewer
image: molecule_local/ubuntu:trusty
command: sleep infinity
dockerfile: Dockerfile
groups:
- molecule
- docker
provisioner:
name: ansible
config_options:
defaults:
roles_path: roles:../../roles:../../../roles
options:
diff: True
scenario:
name: sample
verifier:
name: testinfra
---
- name: Sample Playbook
hosts: all
tasks:
- name: Say Hi
debug:
msg: echo Hello world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment