We have all been there. Your Ansible playbook refuses to work.

Unfortunately, Ansible does not shine when it comes to debugging. It’s not uncommon to get a cryptic error message like this:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "I'm on holiday. I don't want to work."}

You probably spent the last hour trying to understand what the error message means and wondereing why can’t Ansible just tell you what’s wrong.

Thankfully you can resort to the most powerful debugging method ever: print!

One way of getting the output of every single command is to run ansible or ansible-playbook with the -vvvv flag, which makes it very, very, very, very verbose.

It quikly gets very very messy too, if you are runnign your playbook on many instances at the same time.

To get the output of a single command, you have to use the debug module. The way it works is by asking Ansible to store the result of the command we want to diagnose in a variable using register on command or shell modules, then passing it to debug:

- name: List the home contents
  command: ls -la /home/admin
  register: ls_home

- debug:
  msg: "{{ ls_home.stdout }}"

Are you just getting started with Ansible?

If you just inherited a bunch of Ansible playbooks and you are not sure how to get started, you might want to check out my Ansible for beginners post. It’s a quick introduction to Ansible that will get you up and running in no time. It also includes a few tips and tricks that will help you get the most out of Ansible.