This is a text-only version of the following page on https://raymii.org:
---
Title       : 	Ansible - Playbook Testing
Author      : 	Remy van Elst
Date        : 	29-12-2013
URL         : 	https://raymii.org/s/tutorials/Ansible_-_Playbook_Testing.html
Format      : 	Markdown/HTML
---



This Ansible article shows you how to run a basic test on your playbooks to
check 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 up
because they are lower in a playbook. By running the syntax check you make sure
this won't happen.

<p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>


### Dummy inventory file

Lets say your playbooks are located in `/home/username/ansible/playbooks`. You
have a few roles and a few playbooks. To test them, we need a dummy
`ansible_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 your
local machine. It only does a syntax check.

### Testing

Use the `--syntax-check` and `-list-tasks` options, plus the dummy inventory
file 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 wil
execute:

    
    
    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.yml
    
    playbook: ./playbooks/default-vps-setup.yml
    
    ERROR: Syntax Error while loading YAML script,
    /home/remy/ansible/playbooks/roles/vim/tasks/main.yml Note: The error may
    actually 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 the
playbooks 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 variable
files will fail.

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

    
    
    #!/usr/bin/env bash
    set -o errexit
    set -o nounset
    # set -o xtrace
    set -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
    

   [1]: https://www.digitalocean.com/?refcode=7435ae6b8212

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.