Using ansible-adhocs and playbooks seems quite easy until you came across with some issue, failure or error. You need to know how to diagnose or identify the cause of the error and troubleshoot to fix the problem.
Ansible allows you to automate with you own Playbooks or reusing existing Roles and Collections. And in such situations, when you are into writing complex playbooks you need to know the troubleshooting skills.
Lets understand some of the important troubleshooting methods for Ansible Playbooks:
- Understand your Ansible Environment.
- Check the version of Ansible Binaries and Python you are running. Also, check if you have added OS packages or Python modules which is required by the playbooks, is the Ansible Interpreter Seeing them?
$ansible –version
ansible 2.9.22
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/home/admin/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 13 2020, 02:51:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
- Check the path used for modules, including whether the execution is using ansible.cfg file which is not the default?
Run the command:
ansible-config dump --only-changed
DEFAULT_BECOME(/home/admin/ansible/ansible.cfg) = True
DEFAULT_BECOME_USER(/home/admin/ansible/ansible.cfg) = root
DEFAULT_FORKS(/home/admin/ansible/ansible.cfg) = 10
DEFAULT_HOST_LIST(/home/admin/ansible/ansible.cfg) = ['/home/admin/ansible/inventory']
DEFAULT_ROLES_PATH(/home/admin/ansible/ansible.cfg) = ['/home/admin/ansible/roles']
HOST_KEY_CHECKING(/home/admin/ansible/ansible.cfg) = False
It will list the parameters that are different from the default ones.
2. Verbose Mode:
Add -v in the command to see the verbosity levels. You need to increase it gradually in multiple executions until you get what you need. For example: -v, -vv, -vvv, -vvvv, -vvvvv.
3. Use debug option along with filter type_debug.
You might need to know, change, or set the data type on a variable. For example, a registered variable might contain a dictionary when your next task needs a list, or a user prompt might return a string when your playbook needs a boolean value. Use the type_debug, dict2items, and items2dict filters to manage data types. The below example, will display the variable you need to see.
(...)
- name: Display the value of the counter
debug:
msg: "Counter={{ counter }} / Data type={{ counter | type_debug }}"
(...)
TASK [Display the value of the counter] ****************************************************************************
ok: [node1] => {
"msg": "Counter=42 / Data type=AnsibleUnicode"
}
4. Make sure that variables have right content and data type. Using the assert module we can achieve this and cause the task to fail in case of wrong content/data type.
5. Use task debugger: It will allow you to fix errors during execution instead of editing your playbook and running it again to see if that worked.You can enable the debugger in tasks, plays, roles, blocks.
Value | Result |
---|---|
always | Always invoke the debugger, regardless of the outcome |
never | Never invoke the debugger, regardless of the outcome |
on_failed | Only invoke the debugger if a task fails |
on_unreachable | Only invoke the debugger if a host is unreachable |
on_skipped | Only invoke the debugger if the task is skipped |