Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Ansible - Playbook Testing

Published: 29-12-2013 | Author: Remy van Elst | Text only version of this article


Table of Contents


This Ansible article shows you how to run a basic test on your playbooks tocheck if their syntax is correct. It shows methods for both Ansible 1.3 and 1.4.When you get more complicated Ansible playbooks you sometimes have syntax (YAML)errors in them. It sometimes can take a while before those errors show upbecause they are lower in a playbook. By running the syntax check you make surethis won't happen.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Dummy inventory file

Lets say your playbooks are located in /home/username/ansible/playbooks. Youhave a few roles and a few playbooks. To test them, we need a dummyansible_hosts file. Create it:

cd ~/ansible/mkdir tests/vim tests/ansible_hosts

Put this in the file:

[local]127.0.0.1

Note that when executing the tasks it will not actually execute them on yourlocal machine. It only does a syntax check.

Testing

Use the --syntax-check and -list-tasks options, plus the dummy inventoryfile to do a full syntax check, including all includes roles and task files:

ansible-playbook --syntax-check --list-tasks -i tests/ansible_hosts./example-playbook.yml

If there are no errors, you will get a list of tasks which the playbook wilexecute:

playbook: ./playbooks/default-vps-setup.yml  play #1 (local):    apt name={{item}} state=latest update_cache=yes    apt pkg={{item}} state=absent    template src=localegen.j2 dest=/etc/locale.gen    template src=localepurge.j2 dest=/etc/locale.nopurge    template src=timezone.j2 dest=/etc/timezone    template src=issue.net.j2 dest=/etc/issue.net    template src=issue.net.j2 dest=/etc/issue    template src=hostname.j2 dest=/etc/hostname

If you have an error it will show up in red:

ansible-playbook --syntax-check --list-tasks -i tests/ansible_hosts./playbooks/default-vps-setup.ymlplaybook: ./playbooks/default-vps-setup.ymlERROR: Syntax Error while loading YAML script,/home/remy/ansible/playbooks/roles/vim/tasks/main.yml Note: The error mayactually appear before this position: line 3, column 7-dfi://dsf;apt: pkg=vim-tiny state=latest update_cache=yes  sudo: yes

Testing all the playbooks

My ansible git repository is set up like so:

 $ tree -L 3|-- ansible_hosts|-- ci.sh|-- playbooks|   |-- debug.yml|   |-- default-vps-setup.yml|   |-- group_vars|   |   |-- all.yml|   |   |-- apache-php.yml|   |   |-- lighttpd-php.yml|   |   |-- desktop-awesome.yml|   |   `-- nginx-php.yml|   |-- nginx-vps.yml|   `-- roles       |-- bash|       |   `-- tasks|       |-- basic-debian-setup|       |   |-- files|       |   |-- tasks|       |   `-- templates[...]|       |-- vim|       `-- vnstat`-- tests    `-- ansible_hosts

As you can see my playbooks are in the playbooks folder. To test all theplaybooks I use the following find command piped trough to ansible:

find ./playbooks -name '*.yml' -depth 1 | xargs -n1  ansible-playbook--syntax-check --list-tasks -i tests/ansible_hosts

The -depth 1 makes sure only playbooks are tested, testing task or variablefiles will fail.

You can very easily add this setup to Jenkins or any other CI. I have myplaybooks in Jenkins, a simple bash script named ci.sh is used as the onlytest step:

#!/usr/bin/env bashset -o errexitset -o nounset# set -o xtraceset -o pipefail__DIR__="$(cd "$(dirname "${0}")"; echo $(pwd))"__BASE__="$(basename "${0}")"__FILE__="${__DIR__}/${__BASE__}"echo "################################"echo "Build Information"echo "Directory: ${__DIR__}"echo "Filename: ${__FILE__}"echo "Version Information:"echo "Ansible Version: $(ansible --version)"echo "Ansible Playbook Version: $(ansible-playbook --version)"echo "Operating System: $(lsb_release -d | awk -F: '{ print $2 }' | tr -d '\t')"echo "Kernel: $(uname -a)"echo "################################"echo "### Starting tests"find ./playbooks -maxdepth 1 -name '*.yml'| xargs -n1  ansible-playbook--syntax-check --list-tasks -i tests/ansible_hosts
Tags: ansible, configuration-management, deployment, devops, python, syntax, testing, tests, tutorials, yaml