In the latest release of Ansible 2.1, a new debug
strategy is added which allows debugging from within each playbook when run. This launches an actual debug console which allows one to inspect variables and the running task. Think of byebug
or debugger
in ruby but for ansible.
As an example, consider the sample playbook below:
We have a simple playbook which calls a non-existent variable ‘not_exists’ which gets passed to the ping command. Note that we set strategy
to be in debug mode. There are other playbook strategies which can be found here: Ansible Strategy.
Once we run the playbook, when it throws an exception it drops into a debugger console like the following:
We can also print the current result of the task by calling p result
:
The error indicates that the current task has failed because the variable ‘non_existent’ is undefined. It should have been defined as ‘var1’.
To change this we can fix the error directly in the playbook itself and re-run it. We can also make the changes directly in the debug console by changing the task variable to the correct one:
Calling redo
continues to execute the task till the end and now we have a passing task.
Other commands we can issue in the debugger include q
to quit and p
print statements for vars, host, task and result.
You can read more about it on the Ansible Debugger page.
I find this feature tremendously helpful when developing playbooks locally.
Happy Hacking!