1 of 17

Testando Roles Ansible com Molecule

Jairo da Silva Junior @ DevOpsDays POA

2 of 17

@Sobre

Open Source, Open Source, Open Source...

Arquiteto @ Red Hat

Natural de Belém-PA e “morando” em Fortaleza-CE.

3 of 17

Roles 101

4 of 17

Ansible Roles

Coleção de recursos com mesmo propósito:

Tecnologia ou serviço: haproxy, mariadb, ntp, grafana

Ou: hardening, deploy, networking

*Principal unidade de reúso*

Um servidor = Um papel (lb, nameserver, db, appA)

5 of 17

Testes

6 of 17

Ciclo Manual

vagrant up� vagrant provision� # ajustar playbook� vagrant ssh # para verificar algum estado� # provision, ajustado playbook e ssh ad infinitum

7 of 17

Verificações automatizadas (py.test + testinfra)

def test_nginx_is_installed(host):

nginx = host.package("nginx")

assert nginx.is_installed

assert nginx.version.startswith("1.2")

def test_nginx_running_and_enabled(host):

nginx = host.service("nginx")

assert nginx.is_running

assert nginx.is_enabled

8 of 17

Four-phase tests

Setup (Preparação)

  • vagrant up

Exercise (Executar)

  • vagrant provision

Verify (Verificação)

  • py.test + testinfra

Teardown (Limpeza)

  • vagrant destroy

9 of 17

Alternativas

Roles:

Molecule + pytest + testinfra OU Ansible

Modules:

pytest + Ansible

10 of 17

Molecule

11 of 17

Molecule

Começar: molecule init role -r role_name

Driver: Azure, GCE, EC2, OpenStack, Docker, Vagrant

Verifier: testinfra, goss, inspec

12 of 17

Ciclo de vida Molecule

lint

syntax

create

prepare

converge

idempotence

verify

destroy

test

13 of 17

Exemplo (JBoss)

def test_jboss_running_and_enabled(host):

jboss = host.service('wildfly')

assert jboss.is_enabled

def test_jboss_listening_http(host):

socket = host.socket('tcp://0.0.0.0:8080')

assert socket.is_listening

def test_mgmt_user_authentication(host):

command = """curl --digest -L -D - http://localhost:9990/management \

-u ansible:ansible"""

cmd = host.run(command)

assert 'HTTP/1.1 200 OK' in cmd.stdout

14 of 17

CI

15 of 17

Exemplo (TravisCI)

---

sudo: required

language: python

services:

- docker

before_install:

- sudo apt-get -qq update

- pip install molecule

- pip install docker-py

script:

- molecule test

16 of 17

Referências

17 of 17

Obrigado!

Perguntas?