SlideShare a Scribd company logo
Serena Lorenzini, PhD (www.biodec.com)
serena@biodec.com
PloneConf2019
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 1 / 38
Test-driven infrastructure development with Ansible & Molecule
Test-driven infrastructure development with Ansible &
Molecule
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 2 / 38
Test-driven infrastructure development with Ansible & Molecule
What is Ansible?
“Working in IT, you’re likely doing the same tasks over and over. What if
you could solve problems once and then automate your solutions going
forward? Ansible is here to help.”
Ansible is a simple provisioning tool that allows to automate tasks like:
server provisioning
software deployment
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 3 / 38
Test-driven infrastructure development with Ansible & Molecule
Why Ansible?
It is agentless. Everything is done via ssh.
Its YAML configuration files use structure and syntax that will be
familiar to Python programmers.
Ansible’s documentation is vast.
Ansible is easily extended by roles. Roles are Ansibles’ reusable code.
Many roles are available and evaluated by the community (stars,
downloads, comments)
It encourages to discipline yourself to never changing configuration
on your target machines via login.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 4 / 38
Test-driven infrastructure development with Ansible & Molecule
Why Ansible for Plone?
Here I found the documentation of an Ansible role to provision a Plone
infrastructure.
Installing Plone for production, particularly for a busy or complex site,
is hard and requires you learn about a variety of moving parts:
ZEO server
ZEO clients
Process-control
Load balancing
Reverse-proxy caching
URL rewriting and HTTPS support including certificate management
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 5 / 38
Test-driven infrastructure development with Ansible & Molecule
Provisioning a Plone server
HUGE disclaimer: I don’t know Plone and I have never installed one.
But I like to jump into new things using Ansible when possible.
So, let’s pretend that I had to provision a Plone infrastructure. What
would I do and how would I test my deploy strategy?
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 6 / 38
Test-driven infrastructure development with Ansible & Molecule
Install Ansible
Ansible is a Python package. So, install Ansible using virtualenv and
pip – not your OS package manager
$ apt install virtualenv
$ virtualenv -p /usr/bin/python3.6 py36ansible281
$ source py36ansible281/bin/activate
$ (py36ansible281)$ pip install ansible==2.8.1
Check that Ansible works
$ echo "localhost" > inventory
$ ansible all -m ping -i inventory
$ localhost | success >> {
"changed": false,
"ping": "pong" }
Yes, it is that simple.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 7 / 38
Test-driven infrastructure development with Ansible & Molecule
The ansible-playbook command
Ping your instance using the ansible-playbook command.
A playbook.yml file:
hosts: all
tasks:
- name: Ping your instance
action: ping
ansible-playbook -i inventory playbook.yml
TASK [Ping] *****************************************
ok: [localhost] => {"changed": false, "ping": "pong"}
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 8 / 38
Test-driven infrastructure development with Ansible & Molecule
Do Ansible the right way
from the beginning: use roles.
Roles provide a directory structure for breaking a (huge) playbook
into multiple files.
This simplifies writing complex playbooks, and it makes them easier
to reuse.
Plus, it makes them easier to be used by others.
Roles can be created by using the ansible-galaxy init command.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 9 / 38
Test-driven infrastructure development with Ansible & Molecule
Molecule
Instead of using ansible-galaxy I will use Molecule. Molecule is
designed to aid in the development and testing of Ansible roles.
Molecule was adopted by the Ansible project on September 26 2018.
Molecule supports any provider that Ansible supports. Providers can be
bare-metal, virtual, cloud or containers. If Ansible can use it, Molecule
can test it. Molecule simply leverages Ansible’s module system to
manage instances.
So, let’s create a role using Molecule.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 10 / 38
Test-driven infrastructure development with Ansible & Molecule
Install Molecule
Molecule is a python package, let’s manage it using virtualenv and pip, as
usual.
(py36ansible281)$ pip install molecule==2.22
(py36ansible281)$ pip install testinfra==3.2.0
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 11 / 38
Test-driven infrastructure development with Ansible & Molecule
Develop a role with Molecule
Create a role named example:
$ molecule init role -r example -d docker
--> Initializing new role example...
Initialized role in
/home/serena/Work/test/example successfully.
This command uses ansible-galaxy behind the scenes to generate a new
Ansible role, then it injects a molecule directory in the role, and sets it up
to run builds and test runs in a docker environment. Inside the Molecule
directory is a default directory, indicating the default test scenario.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 12 / 38
Test-driven infrastructure development with Ansible & Molecule
Develop a role with Molecule
Now, what’s inside this role scaffold? The usual Ansible directory structure
(tasks, defaults, handlers…), plus a molecule folder.
$ tree -L 1
 defaults
 handlers
 meta
 molecule
 README.md
 tasks
 vars
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 13 / 38
Test-driven infrastructure development with Ansible  Molecule
Develop a role with Molecule
In Ansible everything starts from the tasks/main.yml file:
$ cd example
$ cat tasks/main.yml
There is nothing here. Let’s run some code!
- name: A simple hello
command: echo hello
changed_when: False
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 14 / 38
Test-driven infrastructure development with Ansible  Molecule
Develop a role with Molecule
Molecule has also created a playbook to run the role,
molecule/default/playbook.yml:
$ ansible-playbook molecule/default/playbook.yml -v
TASK [/home/serena/Work/test/example : A simple hello] ***
ok: [localhost] = {
changed: false,
cmd: [echo hello]],
}
STDOUT: hello
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 15 / 38
Test-driven infrastructure development with Ansible  Molecule
What about tests?
So far we have seen how to:
install Ansible
install Molecule
create the simplest Ansible role following the best practices.
Now, what about tests?
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 16 / 38
Test-driven infrastructure development with Ansible  Molecule
Test using Molecule
In the molecule folder there is a sub folder called default. This is a
Scenario.
A scenario is a test suite for your newly created role.
You can have as many scenarios as you like and Molecule will run one
after the other.
The molecule default Scenario is called “default”
$ cd molecule/default/
$ tree -L 1
.
 Dockerfile.j2
 INSTALL.rst
 molecule.yml
 playbook.yml
 tests
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 17 / 38
Test-driven infrastructure development with Ansible  Molecule
Test using Molecule
The molecule.yml is for configuring Molecule. It is a YAML file whose
keys represent the high level components that Molecule provides. The
default Molecule Scenario uses Docker.
$ cat molecule.yml
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: centos:7
The tests will run in a container, named instance and based onto
the image variable. You can add multiple containers and dedicate parts
of your role to a specific container.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 18 / 38
Test-driven infrastructure development with Ansible  Molecule
A Molecule test
molecule test --help
Usage: molecule test [OPTIONS]
Test (lint, cleanup, destroy, dependency,
syntax, create, prepare, converge, idempotence,
side_effect, verify, cleanup, destroy).
The linting phases are quite difficult to pass, so, honestly, I usually disable
them. I can re-apply the linting rules once the development phase is
concluded.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 19 / 38
Test-driven infrastructure development with Ansible  Molecule
A Molecule test
My final molecule.yml:
$ cat molecule/default/molecule.yml
---
lint:
name: yamllint
enabled: False
provisioner:
name: ansible
lint:
name: ansible-lint
enabled: False
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 20 / 38
Test-driven infrastructure development with Ansible  Molecule
A simple Molecule test
The test matrix (not complete):
$ molecule test
-- Validating schema .
Validation completed successfully.
-- Test matrix
 default
 lint
 create
 converge
 idempotence
 destroy
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 21 / 38
Test-driven infrastructure development with Ansible  Molecule
A simple Molecule test
Converge:
-- Scenario: 'default'
-- Action: 'converge'
PLAY [Converge] ************************
TASK [Gathering Facts] ****************
ok: [instance]
TASK [example : A simple hello] *******
ok: [instance]
PLAY RECAP ****************************
instance : ok=2 changed=0
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 22 / 38
Test-driven infrastructure development with Ansible  Molecule
A simple Molecule test
Idempotence:
-- Action: 'idempotence'
Idempotence completed successfully.
Destroy:
-- Action: 'destroy'
PLAY [Destroy] ************************************
TASK [Destroy molecule instance(s)] ****************
changed: [localhost] = (item=instance)
TASK [Delete docker network(s)] ****************
PLAY RECAP *********************************
localhost : ok=2 changed=2serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 23 / 38
Test-driven infrastructure development with Ansible  Molecule
The Plone Ansible role
Following the Plone documentation there is an Ansible role for provisioning
a Plone infrastructure. Let’s take a look.
$ git clone https://github.com/plone/ansible-playbook.git
$ cd ansible-playbook
$ make all
$ ansible-galaxy -r requirements.yml -p roles install
What’s inside this directory?
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 24 / 38
Test-driven infrastructure development with Ansible  Molecule
The Plone Ansible role
There are a lot of directories here, including roles and a few playbooks:
$ tree -L 1
....
 README.rst
 requirements.yml
 roles
 sample-medium.yml
 sample-multiserver.yml
 sample-small.yml
 sample-very-small.yml
...
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 25 / 38
Test-driven infrastructure development with Ansible  Molecule
The Plone Ansible role
Following the instructions here configure the playbook to include these
roles:
$ cat playbook.yml
...
- name: Include vars from local-configure.yml if found
include_vars: {{ item }}
with_first_found:
- local-configure.yml
...
$ cp sample-very-small.yml local-configure.yml
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 26 / 38
Test-driven infrastructure development with Ansible  Molecule
The Plone Ansible role
Following the README I did:
disable munin
configure the admin credentials
run make all to download some other Ansible roles
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 27 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
Now I am ready to inject a molecule test here!
I will use a non default scenario. I will test the Plone deployment using the
ec2 (AWS EC2) driver. (Docker doesn’t work in this case. Services bother
regular Docker containers).
$ molecule init scenario -d ec2
-- Initializing new scenario default...
Initialized scenario in
ansible-playbook/molecule/default successfully.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 28 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
Using the ec2 driver the default scenario layout looks a bit different:
$ tree -L 1 molecule/default/
molecule/default/
 create.yml
 destroy.yml
 INSTALL.rst
 molecule.yml
 playbook.yml
 prepare.yml
 tests
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 29 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
Using the ec2 driver the molecule.yml file looks like this:
driver:
name: ec2
platforms:
- name: instance
image: ami-11bb0e7e
instance_type: t2.micro
...
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 30 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
There is a create.yml file instead of a Dockerfile. This playbook:
creates a security group
creates a keypair and puts it in
.cache/molecule/ansible-playbook/ec2/ssh_key
expects to find the env variable EC2_REGION
expects to find the aws credentials in ~/.aws file
starts the aws instance
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 31 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
The create.yml file:
---
- name: Create
hosts: localhost
connection: local
vars:
ssh_user: admin
ssh_port: 22
security_group_name: molecule
security_group_description: Security group
security_group_rules:
- proto: tcp
from_port: {{ ssh_port }}
to_port: {{ ssh_port }}
cidr_ip: '0.0.0.0/0'
....serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 32 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
Now change the main playbook that molecule will use:
$ cat molecule/default/playbook.yml
- name: Plone
include: /home/serena/Work/test/ansible-playbook/playbook.ym
To test the Plone Ansible role, which is a collection of ~13 roles.
$ tree -L 1 roles/
roles/
 audit
 default_config
....
 timezone
 varnish
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 33 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
I will dare to say that this is becoming a test-driven cloud deployment
with Molecule and Ansible.
Now, that’s a statement!
However, we are getting close to it. Let’s do this.
$ molecule converge
And about ~15 minutes of Ansible later …
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 34 / 38
Test-driven infrastructure development with Ansible  Molecule
The Plone web interface
Figure 1: Plone website
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 35 / 38
Test-driven infrastructure development with Ansible  Molecule
Test the Plone Ansible role
Remeber to destroy this instance:
$ molecule destroy
The molecule test command does the full
converge/idempotence/destroy workflow, but, for keeping things alive for a
bit, I run the molecule converge and molecule destroy commands
one after the other.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 36 / 38
Test-driven infrastructure development with Ansible  Molecule
Summary
Ansible is easy
Molecule really helps in developing roles and
allows to have many pre-configured test scenarios
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 37 / 38
Test-driven infrastructure development with Ansible  Molecule
The end
Thank you for your attention.
serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 38 / 38

More Related Content

What's hot

Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
Andres Almiray
 
Intro To Spring Python
Intro To Spring PythonIntro To Spring Python
Intro To Spring Python
gturnquist
 
Troubleshooting Puppet
Troubleshooting PuppetTroubleshooting Puppet
Troubleshooting Puppet
Thomas Howard Uphill
 
The JavaFX Ecosystem
The JavaFX EcosystemThe JavaFX Ecosystem
The JavaFX Ecosystem
Andres Almiray
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
Andres Almiray
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
Ivan Ivanov
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
Brian Gesiak
 
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Puppet
 
Scala, Functional Programming and Team Productivity
Scala, Functional Programming and Team ProductivityScala, Functional Programming and Team Productivity
Scala, Functional Programming and Team Productivity
7mind
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
Andres Almiray
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
Antonio Goncalves
 
Making the most of your gradle build - Gr8Conf 2017
Making the most of your gradle build - Gr8Conf 2017Making the most of your gradle build - Gr8Conf 2017
Making the most of your gradle build - Gr8Conf 2017
Andres Almiray
 
Mastering Maven 2.0 In 1 Hour V1.3
Mastering Maven 2.0 In 1 Hour V1.3Mastering Maven 2.0 In 1 Hour V1.3
Mastering Maven 2.0 In 1 Hour V1.3
Matthew McCullough
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8
Marcelo de Castro
 
Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017
Andres Almiray
 
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
7mind
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverless
David Przybilla
 

What's hot (19)

Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
Intro To Spring Python
Intro To Spring PythonIntro To Spring Python
Intro To Spring Python
 
Troubleshooting Puppet
Troubleshooting PuppetTroubleshooting Puppet
Troubleshooting Puppet
 
The JavaFX Ecosystem
The JavaFX EcosystemThe JavaFX Ecosystem
The JavaFX Ecosystem
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
Puppet Camp Düsseldorf 2014: Continuously Deliver Your Puppet Code with Jenki...
 
Scala, Functional Programming and Team Productivity
Scala, Functional Programming and Team ProductivityScala, Functional Programming and Team Productivity
Scala, Functional Programming and Team Productivity
 
Testing Java Code Effectively
Testing Java Code EffectivelyTesting Java Code Effectively
Testing Java Code Effectively
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
 
Making the most of your gradle build - Gr8Conf 2017
Making the most of your gradle build - Gr8Conf 2017Making the most of your gradle build - Gr8Conf 2017
Making the most of your gradle build - Gr8Conf 2017
 
Mastering Maven 2.0 In 1 Hour V1.3
Mastering Maven 2.0 In 1 Hour V1.3Mastering Maven 2.0 In 1 Hour V1.3
Mastering Maven 2.0 In 1 Hour V1.3
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8
 
Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017Making the most of your gradle build - Greach 2017
Making the most of your gradle build - Greach 2017
 
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverless
 

Similar to Test driven Infrastructure development with Ansible and Molecule

Ansible top 10 - 2018
Ansible top 10 -  2018Ansible top 10 -  2018
Ansible top 10 - 2018
Viresh Doshi
 
Learn you some Ansible for great good!
Learn you some Ansible for great good!Learn you some Ansible for great good!
Learn you some Ansible for great good!
David Lapsley
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in Django
Kevin Harvey
 
Maven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable ResultsMaven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable Results
Steve Keener
 
Maven and ANT
Maven and ANTMaven and ANT
Maven and ANT
Sun Technlogies
 
Integrating Maven with Eclipse
Integrating Maven with EclipseIntegrating Maven with Eclipse
Integrating Maven with Eclipse
Nikhil Bharati
 
Cinfony - Bring cheminformatics toolkits into tune
Cinfony - Bring cheminformatics toolkits into tuneCinfony - Bring cheminformatics toolkits into tune
Cinfony - Bring cheminformatics toolkits into tune
baoilleach
 
Openstack Summit Container Day Keynote
Openstack Summit Container Day KeynoteOpenstack Summit Container Day Keynote
Openstack Summit Container Day Keynote
Boyd Hemphill
 
Corwin on Containers
Corwin on ContainersCorwin on Containers
Corwin on Containers
Corwin Brown
 
Getting to push_button_deploys
Getting to push_button_deploysGetting to push_button_deploys
Getting to push_button_deploys
Christian Mague
 
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
NETWAYS
 
30 days or less: New Features to Production
30 days or less: New Features to Production30 days or less: New Features to Production
30 days or less: New Features to Production
Karthik Gaekwad
 
Creating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with TestcontainersCreating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with Testcontainers
Paul Balogh
 
Complementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an ExampleComplementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an Example
PVS-Studio
 
Proper Null handling with modern java techniques
Proper Null handling with modern java techniquesProper Null handling with modern java techniques
Proper Null handling with modern java techniques
Nikola Petrov
 
How to Achieve Canary Deployment on Kubernetes
How to Achieve Canary Deployment on KubernetesHow to Achieve Canary Deployment on Kubernetes
How to Achieve Canary Deployment on Kubernetes
HanLing Shen
 
Testing Ansible
Testing AnsibleTesting Ansible
Testing Ansible
Anth Courtney
 
Devops - Continuous Integration And Continuous Development
Devops - Continuous Integration And Continuous DevelopmentDevops - Continuous Integration And Continuous Development
Devops - Continuous Integration And Continuous Development
SandyJohn5
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenv
amenasse
 
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
Daniel Krook
 

Similar to Test driven Infrastructure development with Ansible and Molecule (20)

Ansible top 10 - 2018
Ansible top 10 -  2018Ansible top 10 -  2018
Ansible top 10 - 2018
 
Learn you some Ansible for great good!
Learn you some Ansible for great good!Learn you some Ansible for great good!
Learn you some Ansible for great good!
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in Django
 
Maven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable ResultsMaven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable Results
 
Maven and ANT
Maven and ANTMaven and ANT
Maven and ANT
 
Integrating Maven with Eclipse
Integrating Maven with EclipseIntegrating Maven with Eclipse
Integrating Maven with Eclipse
 
Cinfony - Bring cheminformatics toolkits into tune
Cinfony - Bring cheminformatics toolkits into tuneCinfony - Bring cheminformatics toolkits into tune
Cinfony - Bring cheminformatics toolkits into tune
 
Openstack Summit Container Day Keynote
Openstack Summit Container Day KeynoteOpenstack Summit Container Day Keynote
Openstack Summit Container Day Keynote
 
Corwin on Containers
Corwin on ContainersCorwin on Containers
Corwin on Containers
 
Getting to push_button_deploys
Getting to push_button_deploysGetting to push_button_deploys
Getting to push_button_deploys
 
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
Puppet Camp Duesseldorf 2014: Toni Schmidbauer - Continuously deliver your pu...
 
30 days or less: New Features to Production
30 days or less: New Features to Production30 days or less: New Features to Production
30 days or less: New Features to Production
 
Creating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with TestcontainersCreating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with Testcontainers
 
Complementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an ExampleComplementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an Example
 
Proper Null handling with modern java techniques
Proper Null handling with modern java techniquesProper Null handling with modern java techniques
Proper Null handling with modern java techniques
 
How to Achieve Canary Deployment on Kubernetes
How to Achieve Canary Deployment on KubernetesHow to Achieve Canary Deployment on Kubernetes
How to Achieve Canary Deployment on Kubernetes
 
Testing Ansible
Testing AnsibleTesting Ansible
Testing Ansible
 
Devops - Continuous Integration And Continuous Development
Devops - Continuous Integration And Continuous DevelopmentDevops - Continuous Integration And Continuous Development
Devops - Continuous Integration And Continuous Development
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenv
 
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
 

Recently uploaded

Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
kkirkland2
 
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
OECD Directorate for Financial and Enterprise Affairs
 
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Dutch Power
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Dutch Power
 
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
artemacademy2
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
Frederic Leger
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
OECD Directorate for Financial and Enterprise Affairs
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
gharris9
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
amekonnen
 
Updated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidismUpdated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidism
Faculty of Medicine And Health Sciences
 
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPointMẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
1990 Media
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
faizulhassanfaiz1670
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Rosie Wells
 
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
SkillCertProExams
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Access Innovations, Inc.
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
gharris9
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
samililja
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
ToshihiroIto4
 

Recently uploaded (19)

Burning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdfBurning Issue Presentation By Kenmaryon.pdf
Burning Issue Presentation By Kenmaryon.pdf
 
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...Competition and Regulation in Professions and Occupations – ROBSON – June 202...
Competition and Regulation in Professions and Occupations – ROBSON – June 202...
 
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
Presentatie 8. Joost van der Linde & Daniel Anderton - Eliq 28 mei 2024
 
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
Presentatie 4. Jochen Cremer - TU Delft 28 mei 2024
 
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
Suzanne Lagerweij - Influence Without Power - Why Empathy is Your Best Friend...
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
 
2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf2024-05-30_meetup_devops_aix-marseille.pdf
2024-05-30_meetup_devops_aix-marseille.pdf
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
 
Gregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptxGregory Harris' Civics Presentation.pptx
Gregory Harris' Civics Presentation.pptx
 
Tom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issueTom tresser burning issue.pptx My Burning issue
Tom tresser burning issue.pptx My Burning issue
 
Updated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidismUpdated diagnosis. Cause and treatment of hypothyroidism
Updated diagnosis. Cause and treatment of hypothyroidism
 
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPointMẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
Mẫu PPT kế hoạch làm việc sáng tạo cho nửa cuối năm PowerPoint
 
Media as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern EraMedia as a Mind Controlling Strategy In Old and Modern Era
Media as a Mind Controlling Strategy In Old and Modern Era
 
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie WellsCollapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
Collapsing Narratives: Exploring Non-Linearity • a micro report by Rosie Wells
 
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
Mastering the Concepts Tested in the Databricks Certified Data Engineer Assoc...
 
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdfSupercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
Supercharge your AI - SSP Industry Breakout Session 2024-v2_1.pdf
 
Gregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics PresentationGregory Harris - Cycle 2 - Civics Presentation
Gregory Harris - Cycle 2 - Civics Presentation
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
 

Test driven Infrastructure development with Ansible and Molecule

  • 1. Serena Lorenzini, PhD (www.biodec.com) serena@biodec.com PloneConf2019 serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 1 / 38
  • 2. Test-driven infrastructure development with Ansible & Molecule Test-driven infrastructure development with Ansible & Molecule serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 2 / 38
  • 3. Test-driven infrastructure development with Ansible & Molecule What is Ansible? “Working in IT, you’re likely doing the same tasks over and over. What if you could solve problems once and then automate your solutions going forward? Ansible is here to help.” Ansible is a simple provisioning tool that allows to automate tasks like: server provisioning software deployment serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 3 / 38
  • 4. Test-driven infrastructure development with Ansible & Molecule Why Ansible? It is agentless. Everything is done via ssh. Its YAML configuration files use structure and syntax that will be familiar to Python programmers. Ansible’s documentation is vast. Ansible is easily extended by roles. Roles are Ansibles’ reusable code. Many roles are available and evaluated by the community (stars, downloads, comments) It encourages to discipline yourself to never changing configuration on your target machines via login. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 4 / 38
  • 5. Test-driven infrastructure development with Ansible & Molecule Why Ansible for Plone? Here I found the documentation of an Ansible role to provision a Plone infrastructure. Installing Plone for production, particularly for a busy or complex site, is hard and requires you learn about a variety of moving parts: ZEO server ZEO clients Process-control Load balancing Reverse-proxy caching URL rewriting and HTTPS support including certificate management serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 5 / 38
  • 6. Test-driven infrastructure development with Ansible & Molecule Provisioning a Plone server HUGE disclaimer: I don’t know Plone and I have never installed one. But I like to jump into new things using Ansible when possible. So, let’s pretend that I had to provision a Plone infrastructure. What would I do and how would I test my deploy strategy? serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 6 / 38
  • 7. Test-driven infrastructure development with Ansible & Molecule Install Ansible Ansible is a Python package. So, install Ansible using virtualenv and pip – not your OS package manager $ apt install virtualenv $ virtualenv -p /usr/bin/python3.6 py36ansible281 $ source py36ansible281/bin/activate $ (py36ansible281)$ pip install ansible==2.8.1 Check that Ansible works $ echo "localhost" > inventory $ ansible all -m ping -i inventory $ localhost | success >> { "changed": false, "ping": "pong" } Yes, it is that simple. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 7 / 38
  • 8. Test-driven infrastructure development with Ansible & Molecule The ansible-playbook command Ping your instance using the ansible-playbook command. A playbook.yml file: hosts: all tasks: - name: Ping your instance action: ping ansible-playbook -i inventory playbook.yml TASK [Ping] ***************************************** ok: [localhost] => {"changed": false, "ping": "pong"} serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 8 / 38
  • 9. Test-driven infrastructure development with Ansible & Molecule Do Ansible the right way from the beginning: use roles. Roles provide a directory structure for breaking a (huge) playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. Plus, it makes them easier to be used by others. Roles can be created by using the ansible-galaxy init command. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 9 / 38
  • 10. Test-driven infrastructure development with Ansible & Molecule Molecule Instead of using ansible-galaxy I will use Molecule. Molecule is designed to aid in the development and testing of Ansible roles. Molecule was adopted by the Ansible project on September 26 2018. Molecule supports any provider that Ansible supports. Providers can be bare-metal, virtual, cloud or containers. If Ansible can use it, Molecule can test it. Molecule simply leverages Ansible’s module system to manage instances. So, let’s create a role using Molecule. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 10 / 38
  • 11. Test-driven infrastructure development with Ansible & Molecule Install Molecule Molecule is a python package, let’s manage it using virtualenv and pip, as usual. (py36ansible281)$ pip install molecule==2.22 (py36ansible281)$ pip install testinfra==3.2.0 serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 11 / 38
  • 12. Test-driven infrastructure development with Ansible & Molecule Develop a role with Molecule Create a role named example: $ molecule init role -r example -d docker --> Initializing new role example... Initialized role in /home/serena/Work/test/example successfully. This command uses ansible-galaxy behind the scenes to generate a new Ansible role, then it injects a molecule directory in the role, and sets it up to run builds and test runs in a docker environment. Inside the Molecule directory is a default directory, indicating the default test scenario. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 12 / 38
  • 13. Test-driven infrastructure development with Ansible & Molecule Develop a role with Molecule Now, what’s inside this role scaffold? The usual Ansible directory structure (tasks, defaults, handlers…), plus a molecule folder. $ tree -L 1 defaults handlers meta molecule README.md tasks vars serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 13 / 38
  • 14. Test-driven infrastructure development with Ansible Molecule Develop a role with Molecule In Ansible everything starts from the tasks/main.yml file: $ cd example $ cat tasks/main.yml There is nothing here. Let’s run some code! - name: A simple hello command: echo hello changed_when: False serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 14 / 38
  • 15. Test-driven infrastructure development with Ansible Molecule Develop a role with Molecule Molecule has also created a playbook to run the role, molecule/default/playbook.yml: $ ansible-playbook molecule/default/playbook.yml -v TASK [/home/serena/Work/test/example : A simple hello] *** ok: [localhost] = { changed: false, cmd: [echo hello]], } STDOUT: hello serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 15 / 38
  • 16. Test-driven infrastructure development with Ansible Molecule What about tests? So far we have seen how to: install Ansible install Molecule create the simplest Ansible role following the best practices. Now, what about tests? serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 16 / 38
  • 17. Test-driven infrastructure development with Ansible Molecule Test using Molecule In the molecule folder there is a sub folder called default. This is a Scenario. A scenario is a test suite for your newly created role. You can have as many scenarios as you like and Molecule will run one after the other. The molecule default Scenario is called “default” $ cd molecule/default/ $ tree -L 1 . Dockerfile.j2 INSTALL.rst molecule.yml playbook.yml tests serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 17 / 38
  • 18. Test-driven infrastructure development with Ansible Molecule Test using Molecule The molecule.yml is for configuring Molecule. It is a YAML file whose keys represent the high level components that Molecule provides. The default Molecule Scenario uses Docker. $ cat molecule.yml dependency: name: galaxy driver: name: docker platforms: - name: instance image: centos:7 The tests will run in a container, named instance and based onto the image variable. You can add multiple containers and dedicate parts of your role to a specific container. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 18 / 38
  • 19. Test-driven infrastructure development with Ansible Molecule A Molecule test molecule test --help Usage: molecule test [OPTIONS] Test (lint, cleanup, destroy, dependency, syntax, create, prepare, converge, idempotence, side_effect, verify, cleanup, destroy). The linting phases are quite difficult to pass, so, honestly, I usually disable them. I can re-apply the linting rules once the development phase is concluded. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 19 / 38
  • 20. Test-driven infrastructure development with Ansible Molecule A Molecule test My final molecule.yml: $ cat molecule/default/molecule.yml --- lint: name: yamllint enabled: False provisioner: name: ansible lint: name: ansible-lint enabled: False serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 20 / 38
  • 21. Test-driven infrastructure development with Ansible Molecule A simple Molecule test The test matrix (not complete): $ molecule test -- Validating schema . Validation completed successfully. -- Test matrix default lint create converge idempotence destroy serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 21 / 38
  • 22. Test-driven infrastructure development with Ansible Molecule A simple Molecule test Converge: -- Scenario: 'default' -- Action: 'converge' PLAY [Converge] ************************ TASK [Gathering Facts] **************** ok: [instance] TASK [example : A simple hello] ******* ok: [instance] PLAY RECAP **************************** instance : ok=2 changed=0 serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 22 / 38
  • 23. Test-driven infrastructure development with Ansible Molecule A simple Molecule test Idempotence: -- Action: 'idempotence' Idempotence completed successfully. Destroy: -- Action: 'destroy' PLAY [Destroy] ************************************ TASK [Destroy molecule instance(s)] **************** changed: [localhost] = (item=instance) TASK [Delete docker network(s)] **************** PLAY RECAP ********************************* localhost : ok=2 changed=2serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 23 / 38
  • 24. Test-driven infrastructure development with Ansible Molecule The Plone Ansible role Following the Plone documentation there is an Ansible role for provisioning a Plone infrastructure. Let’s take a look. $ git clone https://github.com/plone/ansible-playbook.git $ cd ansible-playbook $ make all $ ansible-galaxy -r requirements.yml -p roles install What’s inside this directory? serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 24 / 38
  • 25. Test-driven infrastructure development with Ansible Molecule The Plone Ansible role There are a lot of directories here, including roles and a few playbooks: $ tree -L 1 .... README.rst requirements.yml roles sample-medium.yml sample-multiserver.yml sample-small.yml sample-very-small.yml ... serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 25 / 38
  • 26. Test-driven infrastructure development with Ansible Molecule The Plone Ansible role Following the instructions here configure the playbook to include these roles: $ cat playbook.yml ... - name: Include vars from local-configure.yml if found include_vars: {{ item }} with_first_found: - local-configure.yml ... $ cp sample-very-small.yml local-configure.yml serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 26 / 38
  • 27. Test-driven infrastructure development with Ansible Molecule The Plone Ansible role Following the README I did: disable munin configure the admin credentials run make all to download some other Ansible roles serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 27 / 38
  • 28. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role Now I am ready to inject a molecule test here! I will use a non default scenario. I will test the Plone deployment using the ec2 (AWS EC2) driver. (Docker doesn’t work in this case. Services bother regular Docker containers). $ molecule init scenario -d ec2 -- Initializing new scenario default... Initialized scenario in ansible-playbook/molecule/default successfully. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 28 / 38
  • 29. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role Using the ec2 driver the default scenario layout looks a bit different: $ tree -L 1 molecule/default/ molecule/default/ create.yml destroy.yml INSTALL.rst molecule.yml playbook.yml prepare.yml tests serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 29 / 38
  • 30. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role Using the ec2 driver the molecule.yml file looks like this: driver: name: ec2 platforms: - name: instance image: ami-11bb0e7e instance_type: t2.micro ... serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 30 / 38
  • 31. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role There is a create.yml file instead of a Dockerfile. This playbook: creates a security group creates a keypair and puts it in .cache/molecule/ansible-playbook/ec2/ssh_key expects to find the env variable EC2_REGION expects to find the aws credentials in ~/.aws file starts the aws instance serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 31 / 38
  • 32. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role The create.yml file: --- - name: Create hosts: localhost connection: local vars: ssh_user: admin ssh_port: 22 security_group_name: molecule security_group_description: Security group security_group_rules: - proto: tcp from_port: {{ ssh_port }} to_port: {{ ssh_port }} cidr_ip: '0.0.0.0/0' ....serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 32 / 38
  • 33. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role Now change the main playbook that molecule will use: $ cat molecule/default/playbook.yml - name: Plone include: /home/serena/Work/test/ansible-playbook/playbook.ym To test the Plone Ansible role, which is a collection of ~13 roles. $ tree -L 1 roles/ roles/ audit default_config .... timezone varnish serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 33 / 38
  • 34. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role I will dare to say that this is becoming a test-driven cloud deployment with Molecule and Ansible. Now, that’s a statement! However, we are getting close to it. Let’s do this. $ molecule converge And about ~15 minutes of Ansible later … serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 34 / 38
  • 35. Test-driven infrastructure development with Ansible Molecule The Plone web interface Figure 1: Plone website serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 35 / 38
  • 36. Test-driven infrastructure development with Ansible Molecule Test the Plone Ansible role Remeber to destroy this instance: $ molecule destroy The molecule test command does the full converge/idempotence/destroy workflow, but, for keeping things alive for a bit, I run the molecule converge and molecule destroy commands one after the other. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 36 / 38
  • 37. Test-driven infrastructure development with Ansible Molecule Summary Ansible is easy Molecule really helps in developing roles and allows to have many pre-configured test scenarios serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 37 / 38
  • 38. Test-driven infrastructure development with Ansible Molecule The end Thank you for your attention. serena@biodec.com Serena Lorenzini, PhD (www.biodec.com) PloneConf2019 38 / 38