Debugging Ansible Flashcards
First line of debugging is reading logs. The default ansible log formats can be intimidating for humans. How can you do?
Change the output format using ansible output callback plugin. The configiguration can be done in ansible.cfg
[defaults]
stdout_callback=debug
SSH connection to hosts sometimes fail. How do you troubleshoot them?
Run the playbook with increased verbose level.
> ansible-playbook -vvv
or
> ansible-playbook -vvvv
You want to see the value of a variable when running the playbook. What do you do?
use the debug module
- debug: var=myvariable
- debug msg=”The value of my variable is {{ var }}”
- debug var=hostvars[inventory_hostname]
What is the playbook debugger?
Playbook debugger is an interactive debugger for ansible. When a task fails, ansible drops into the debugger.
How do I enable ansible debugger?
Add the clause “strategy: debug” to the playbook.
How do I navigate the ansible debugger?
using ansinble deugger commands:
p var: print a variable r: rerun the failed task c: continue executing the play q: abort the play task.args[key]=value: modify an argument vars[key]=value modify a variable value
What are example of variables supported by the ansible debugger?
task, result, vars, vars[key], task.args
What is the use of the assert module?
assert module will fail the playbook with an error if a condition is not met.
Provide an example using the assert plugin
- name: assert that eth1 exists
assert:
that: ansible_eth1 is defined
What is the language used in assert statement?
Jinja2
I want to assert that /etc/nginx is a directory. What do I do? Write the task.
1) Call the stat module to get stats on the file system
2) Assert of the type of file property that included in the provided stats
- name: stat /etc/nginx
stat: /etc/nginx
register: st - name: assert that /etc/nginx is a dir
assert:
that: st.stat.isdir
Ansible-playbook provides many flags that allow you to inspect or sanitize your playbook before running it. what are these flags?
- -syntax-check
- -list-hosts
- -list-tasks
- -check (run ansible in check mode/ dry run)
- -diff (show files changed on the managed hosts as result of execution)
What mechanisms allow me to restrict the way I can run a playbook?
1) step: ansible prompts me before executing each task
2) start-at-task: start running playbook at given tag
3) –tags: run only tasks with a given tag
How do I tag a task?
- name: run a command
command: /opt/myprog
tags:
- bar
- quux
Provide a command for running playbook.yml with tags foo and quux
> ansible-playbook –tags=foo,quux playbook.yml