What is the Ansible Debugger?
The Ansible debugger is an interactive prompt that activates when a task fails (or always, depending on configuration). It lets you inspect variables, modify arguments, and retry the task without restarting the entire playbook.
Enable the Debugger
Per-Play
- name: Deploy application
hosts: webservers
debugger: on_failed
tasks:
- name: Start service
ansible.builtin.service:
name: myapp
state: started
Per-Task
- name: Copy configuration
ansible.builtin.template:
src: app.conf.j2
dest: /etc/myapp/app.conf
debugger: on_failed
Global (ansible.cfg)
[defaults]
enable_task_debugger = true
Via Environment Variable
ANSIBLE_ENABLE_TASK_DEBUGGER=true ansible-playbook site.yml
Debugger Strategies
| Strategy | When debugger activates |
|---|---|
always | After every task |
never | Never (default) |
on_failed | Only on task failure |
on_unreachable | When host is unreachable |
on_skipped | When task is skipped |
Debugger Commands
When the debugger activates, you get an interactive prompt:
[server1] TASK: Start service (debug)>
| Command | Short | Description |
|---|---|---|
print | p | Print variable or task info |
task.args[key] = value | — | Modify task arguments |
task_vars[key] = value | — | Modify task variables |
redo | r | Retry the task with current args |
continue | c | Continue to next task (mark as failed) |
quit | q | Quit the playbook |
update_task | u | Update task from modified args |
Practical Example: Fixing a Typo
- name: Playbook with a typo
hosts: localhost
debugger: on_failed
vars:
message: "Hello from Ansible!"
tasks:
- name: Display the message
ansible.builtin.debug:
msg: "{{ massage }}" # Typo! Should be 'message'
When this fails:
[localhost] TASK: Display the message (debug)> p task.args
{'msg': '{{ massage }}'}
[localhost] TASK: Display the message (debug)> p task_vars['message']
'Hello from Ansible!'
[localhost] TASK: Display the message (debug)> task.args['msg'] = '{{ message }}'
[localhost] TASK: Display the message (debug)> r
ok: [localhost] => {
"msg": "Hello from Ansible!"
}
Inspecting Variables
# Print all task arguments
(debug)> p task.args
# Print a specific variable
(debug)> p task_vars['my_variable']
# Print host facts
(debug)> p task_vars['ansible_distribution']
# Print the result of the failed task
(debug)> p result._result
# Print the error message
(debug)> p result._result['msg']
Modifying and Retrying
# Fix a wrong file path
(debug)> task.args['dest'] = '/etc/nginx/nginx.conf'
(debug)> r
# Fix a wrong package name
(debug)> task.args['name'] = 'nginx'
(debug)> r
# Set a missing variable
(debug)> task_vars['db_port'] = 5432
(debug)> r
Real-World Debugging Workflow
---
- name: Deploy web application
hosts: webservers
debugger: on_failed
tasks:
- name: Install dependencies
ansible.builtin.apt:
name: "{{ packages }}"
state: present
vars:
packages:
- nginx
- python3-pip
- python3-venv
- name: Deploy app config
ansible.builtin.template:
src: "{{ app_config_template }}"
dest: /etc/myapp/config.yml
owner: myapp
group: myapp
mode: '0640'
- name: Start application
ansible.builtin.systemd:
name: myapp
state: started
enabled: true
If the template task fails (template not found), the debugger lets you:
p task.args— see which template path it tried- Fix the path:
task.args['src'] = 'templates/config.yml.j2' r— retry without restarting the playbook
Tips
- Use
on_failedin development,neverin production - Don't leave debugger enabled in CI/CD — it blocks on stdin
- Combine with
--stepto step through tasks one at a time:ansible-playbook site.yml --step - Use
-vvvalongside debugger for maximum visibility
Debugger vs Other Debugging Tools
| Tool | Best For |
|---|---|
debugger: on_failed | Interactive fix-and-retry |
debug module | Print variables during execution |
--check --diff | Dry run to preview changes |
--step | Execute tasks one at a time |
-vvv | Verbose connection/execution output |
ansible-lint | Catch issues before running |
Related Articles
- Ansible debug Module — Print variables and messages
- Ansible Error Handling — rescue, always, ignore_errors
- Ansible Dry Run — Check mode explained
- Ansible Best Practices — Write clean playbooks
Conclusion
The Ansible debugger turns playbook failures from frustration into learning moments. Enable it with debugger: on_failed, use p to inspect, fix the issue, and r to retry — all without restarting your playbook.