Skip to content

Lesson 11 of 13 · 20m

Debugging When Ansible Tells You Nothing

module_stderr, -vvv, the on-failed debugger, ansible-console, and --start-at-task — the five tools that answer where the run actually died.

Ansible's failure output is famously unhelpful on purpose: a task fails, you get a red block of module JSON, and the actual reason is three keys deep in module_stderr — or absent entirely, because the module returned {"failed": true, "msg": "..."} and the useful part is what didn't print. Debugging Ansible well is mostly knowing which of five tools answers the question you're actually asking.

First move: read the result, don't re-run blind

A failed task's JSON contains rc, stdout, stderr, module_stdout, module_stderr, msg. The pattern that matters: module_stderr is where the remote Python traceback hides. A command failure gives you stderr; a module that crashed gives you module_stderr — different keys, and people who only look at stderr conclude "there was no error output" when the traceback was sitting right there.

ansible-playbook site.yml -vvv    # task-level detail: resolved vars, module args
ansible-playbook site.yml -vvvv   # + SSH layer: connections, transfers, commands on the wire

-v levels are additive: -vvv shows you what the module was told (the resolved arguments — this catches eighty percent of "why did it use the staging value"), -vvvv shows you the SSH plumbing. Start at -vvv. -vvvv when you suspect the transport.

The interactive debugger: the tool nobody knows exists

Since ansible-core 2.5, a failed task can drop you into a REPL on the control node — a real debugger where you can inspect every variable and re-run the task with different arguments:

- name: The suspicious task
  ansible.builtin.template: { src: app.conf.j2, dest: /etc/app.conf }
  debugger: on_failed

Or globally: ANSIBLE_ENABLE_TASK_DEBUGGER=True ansible-playbook site.yml. Inside the debugger: p task_vars, p result, redo to re-run with edits, quit to continue. debugger: on_failed on a play catches any task; on a task, just that one. This replaces the "add a debug: task, re-run, add another" loop for anything non-trivial — the loop that turns a 10-minute diagnosis into an afternoon.

ansible-console: the inventory REPL

ansible-console webservers -i inventories/production
[acme@webservers (4)]$ setup filter=ansible_distribution
[acme@webservers (4)]$ service name=nginx state=restarted --become
[acme@web01.prod.acme.corp (1)]$ shell dmesg | tail -20

ansible-console is a REPL over your inventory — tab-complete a group, run ad-hoc modules against it, narrow to one host. It exists specifically for "what does that host actually look like right now" questions that are beneath a playbook's dignity: which nginx version is installed, whether the fact you templated on is what you think it is, whether become works before you run 40 tasks that need it.

Re-run less: the targeting flags

ansible-playbook site.yml --start-at-task "Ship app config"   # resume mid-play
ansible-playbook site.yml --step                              # confirm each task
ansible-playbook site.yml --list-tasks --tags deploy          # what WOULD run
ansible-playbook site.yml --list-hosts                        # what WOULD it hit
ansible-inventory --host db01                                 # resolved vars for one host
ansible-playbook site.yml --syntax-check                      # parses ≠ correct, but catches typos

--start-at-task resumes a failed run at the named task — the fix for "task 38 of 45 failed and I don't want to re-run 37." --list-tasks/--list-hosts are the read-only audit: before you claim the playbook targets what you think, ask it. The --list-* flags answer "what does this playbook do" faster than reading the YAML, because they resolve tags, imports, and host patterns for you.

debug module: var= over msg=

- name: What did the API actually return
  ansible.builtin.debug:
    var: api_result

var= prints the resolved structure — dict keys and all — where msg: "{{ api_result }}" prints a stringification that hides types. var= is the right default; msg= is for composed sentences ("deploying {{ app_version }} to {{ ansible_play_batch | length }} nodes").

The structured post-mortem

When a block/rescue fires, the failure facts are the autopsy:

rescue:
  - name: Record the failure
    ansible.builtin.debug:
      msg: "died at {{ ansible_failed_task.name }}: {{ ansible_failed_result.msg | default('no msg') }}"

ansible_failed_task (which task), ansible_failed_result (its full result blob). Log both — "task X on host Y failed with Z" beats paging through scrollback for the red block.

Field notes

  • strategy: free changes ordering — interleaved task output makes failure timelines hard to read; lockstep linear is the debugging default for a reason.
  • ansible-doc ansible.builtin.template answers "what args does this module actually take" without leaving the terminal — faster and more accurate than a blog post from 2019.
  • Half of "Ansible did something weird" is variable precedence. ansible-inventory --host x plus debug: var= is the whole investigation; the rest is patience.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.