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: stat /etc/hosts then debug exists then when exists
hosts: all
gather_facts: true
tasks:
- name: Check if /etc/hosts exists
ansible.builtin.stat:
path: /etc/hosts
register: hosts_stat
- name: Debug if /etc/hosts exists
ansible.builtin.debug:
msg: "/etc/hosts exists"
when: hosts_stat.stat.exists
- name: Run task when /etc/hosts exists
ansible.builtin.command:
cmd: echo "Task executed because /etc/hosts exists"
when: hosts_stat.stat.existsHow it works
Stat /etc/hosts, then debug when it exists, then run the command with the same when expression. A skipped task does not show the expression result. Print or debug the registered value first so you can see why Ansible skips later work.
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-16
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.