0% found this document useful (0 votes)
142 views19 pages

Testing Ansible Roles Using Molecule - Estu Fardani

The document discusses using Molecule to test Ansible roles. Molecule allows testing roles across multiple operating systems, distributions, virtualization providers, and test frameworks. It ensures roles are consistent, well-written, easy to understand and maintain. The document demonstrates initializing Molecule for a sample Nginx role, setting up the OS environment and tasks, adding handlers, using ServerSpec as the test framework to check packages, services and ports, and running Molecule tests to validate the role works as expected before deployment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views19 pages

Testing Ansible Roles Using Molecule - Estu Fardani

The document discusses using Molecule to test Ansible roles. Molecule allows testing roles across multiple operating systems, distributions, virtualization providers, and test frameworks. It ensures roles are consistent, well-written, easy to understand and maintain. The document demonstrates initializing Molecule for a sample Nginx role, setting up the OS environment and tasks, adding handlers, using ServerSpec as the test framework to check packages, services and ports, and running Molecule tests to validate the role works as expected before deployment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Testing Ansible Roles Using

Molecule
Estu Fardani | Geekcamp.id | 20170715
Estu Fardani
● Ops at Peentar.id
● Release Manager at BlankOn Linux
● Master at Binar Academy

@tuanpembual | estu@blankon.id

https://github.com/tuanpembual/molecule-ansible
Molecule
Molecule framework to aid in the development and testing of
Ansible roles. Base on Ansible-Galaxy.

Support multiple:

● instances,
● operating systems and distributions,
● virtualization providers,
● test frameworks,
● testing scenarios.
Molecule
Memastikan hasil role ansible konsisten, ditulis dengan
baik, mudah dipahami dan dipelihara.

Install stable v1.2 via pip:

$ sudo pip install molecule

Deps:
* your flavour provider | docker or vagrant
Initiation Molecule
$ molecule init --role nginx \\
--verifier serverspec \\
--driver docker

$ cd nginx; bundle install


Setup OS env
molecule.yml

docker:
containers:
- name: nginx
image: ubuntu
image_version: latest
Add Task
tasks/main.yml

---
# tasks file for nginx
- name: install apt-transport-https
apt: name=apt-transport-https update_cache=yes
become: yes
- name: install packages
apt: name={{ item }} update_cache=yes force=yes
with_items:
- nginx
- curl
- net-tools
become: yes
notify:
- restart nginx
Setup Handler to Restart Nginx
handlers/main.yml

---
# handlers file for nginx
- name: restart nginx
service: name=nginx state=restarted
become: yes
Test Frameworks
Default: tox, writing in python

But, I like ruby style. So I using ServerSpec.org.

Resource:

● Check package installed


● Check service running
● Check port listening
Setup Testing Scenario
spec/default_spec.rb

require 'spec_helper'

describe service('nginx') do
it { should be_running }
end

describe port(80) do
it { should be_listening }
end
Run Molecule
$ molecule --help
$ molecule test

It will do:

● Create instance
● Apply role
● Run test
● Destroy instance
Split Environment
- Env Development | for run molecule test on local
- Staging | for run staging env
- Prod | for run prod env

But you can always add more env


Picture
Additional Forder
Implement
After roles pass all test, it is ready to apply directly to
our server.

Run from jenkins:

● Install ansible in jenkins slave


● Testing ssh
● Run ansible from outer nginx role folder as jenkins shell script
$ ansible-playbook nginx/playbook.yml -i nginx/environment/staging --user
ubuntu
QUESTION
1...
Documentations
1. http://github.com/metacloud/molecule
2. http://molecule.readthedocs.io/
TERIMAKASIH

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy