Ansible in a Minute · Ansible Core ·
Debug a when condition before you skip a task
Short answer
Print the values in a when expression so you can see why Ansible skips a task.
Subscribe for updates
Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.
By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.
Request
stat /etc/hosts then debug exists then when exists
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
---
- name: Debug a when condition
hosts: all
gather_facts: false
tasks:
- name: Stat /etc/hosts
ansible.builtin.stat:
path: /etc/hosts
register: file_stat
- name: Debug file_stat.stat.exists
ansible.builtin.debug:
msg: "{{ file_stat.stat.exists }}"
- name: Print when the file exists
ansible.builtin.debug:
msg: "File exists"
when: file_stat.stat.existsHow it works
A skipped task does not show the expression result. Print the registered value first. Then keep the same expression in when.
When to use it
- Finding why a task is skipped
- Checking a registered result before you add when
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-15
Notes
Run the playbook with -v to see skipped-task detail. Keep the debug task while you develop. Remove it when the condition is correct.
Reference
- Debugging conditionals (Ansible docs)
Built for every Ansible use case in every organization
BacklogZero can generate this kind of automation in your environment, just like it did here.