$ who am i --- - name: Ton Kersten creds: work: UNIX/Linux consultant and Trainer @ AT Computing linux: UNIX/Linux Geek (UNIX since 1986, Linux since 1992) scripting: Scripting / programming Freak cfgmgmt: Configuration Management Addict - ansible: - Ansible user and contributor since 2012 - Red Hat Certified Specialist in Ansible Automation - Ansible Ambassador since 2015 foss: Free and Open Source Software Enthusiast things: Big fan of things that just work ans-v1.25-3
Why test roles Ansible playbooks and roles are production code Check installation of required packages Process and check needed templates Check files for contents Check running and enabled services Trigger handlers · · · ans-v1.25-4
Get started Version 1 $ molecule init --role chrony --driver docker Version 2 $ molecule init role \ --role-name=chrony \ --driver-name=docker \ --verifier-name=testinfra ans-v1.25-7
Create tests 1 molecule/default/tests/test_default.py import testinfra.utils.ansible_runner as ti # Get all hosts from the Ansible inventory # The inventory is generated from the molecule.yml file inventory = '.molecule/ansible_inventory' hosts = ti.AnsibleRunner(inventory).get_hosts('all') ans-v1.25-13 Setup framework and get all Ansible hosts
Create tests 3 molecule/default/tests/test_default.py if present: for this in present: assert this.is_installed ans-v1.25-15 Check if all packages are installed
Create tests 4 molecule/default/tests/test_default.py if present: for this in present: assert this.exists assert this.is_file assert this.uid == 0 assert this.gid == 0 assert this.mode == 0o644 assert this.contains("# Copyright (c) 2012-today by ACME") ans-v1.25-16 Check if files have the correct content and credentials
Create tests 5 molecule/default/tests/test_default.py if present: for this in present: assert this.is_running assert this.is_enabled ans-v1.25-17 Check if all services are running and enabled
Problems Fragile environment Versions of tools must match Sudden, unexpected breaking of test line Needs Python Docker < 3.0 Large difference between version 1 and 2 Documentation not always clear · · · ans-v1.25-19
Question Time! Questions?? Contact me [email protected] http://www.atcomputing.nl https://github.com/tonk https://speakerdeck.com/tonk @TonKersten on Twitter TKersten on IRC Created with L A TEX Beamer Vim Poppler Tools ImageMagick Evince ans-v1.25-20