Testando Roles Ansible com Molecule
Jairo da Silva Junior @ DevOpsDays POA
@Sobre
Open Source, Open Source, Open Source...
Arquiteto @ Red Hat
Natural de Belém-PA e “morando” em Fortaleza-CE.
Roles 101
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)
Testes
Ciclo Manual
vagrant up� vagrant provision� # ajustar playbook� vagrant ssh # para verificar algum estado� # provision, ajustado playbook e ssh ad infinitum
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
Four-phase tests
Setup (Preparação)
Exercise (Executar)
Verify (Verificação)
Teardown (Limpeza)
Alternativas
Roles:
Molecule + pytest + testinfra OU Ansible
Modules:
pytest + Ansible
Molecule
Molecule
Começar: molecule init role -r role_name
Driver: Azure, GCE, EC2, OpenStack, Docker, Vagrant
Verifier: testinfra, goss, inspec
Ciclo de vida Molecule
lint
syntax
create
prepare
converge
idempotence
verify
destroy
test
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
CI
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
Referências
Obrigado!
Perguntas?