Debugging Ansible Flashcards

1
Q

First line of debugging is reading logs. The default ansible log formats can be intimidating for humans. How can you do?

A

Change the output format using ansible output callback plugin. The configiguration can be done in ansible.cfg

[defaults]
stdout_callback=debug

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

SSH connection to hosts sometimes fail. How do you troubleshoot them?

A

Run the playbook with increased verbose level.

> ansible-playbook -vvv

or

> ansible-playbook -vvvv

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

You want to see the value of a variable when running the playbook. What do you do?

A

use the debug module

  • debug: var=myvariable
  • debug msg=”The value of my variable is {{ var }}”
  • debug var=hostvars[inventory_hostname]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the playbook debugger?

A

Playbook debugger is an interactive debugger for ansible. When a task fails, ansible drops into the debugger.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do I enable ansible debugger?

A

Add the clause “strategy: debug” to the playbook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do I navigate the ansible debugger?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are example of variables supported by the ansible debugger?

A

task, result, vars, vars[key], task.args

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the use of the assert module?

A

assert module will fail the playbook with an error if a condition is not met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Provide an example using the assert plugin

A
  • name: assert that eth1 exists
    assert:
    that: ansible_eth1 is defined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the language used in assert statement?

A

Jinja2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

I want to assert that /etc/nginx is a directory. What do I do? Write the task.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Ansible-playbook provides many flags that allow you to inspect or sanitize your playbook before running it. what are these flags?

A
  • -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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What mechanisms allow me to restrict the way I can run a playbook?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do I tag a task?

A
  • name: run a command
    command: /opt/myprog
    tags:
    - bar
    - quux
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Provide a command for running playbook.yml with tags foo and quux

A

> ansible-playbook –tags=foo,quux playbook.yml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Provide a command for running playbook.yml while skipping tasks tagged with foo and quux

A

> ansible-playbook –skip-tags=foo,quux playbook.yml