Ansible in a Minute · Ansible Core ·
Use when with ansible_facts to skip a task by OS
Short answer
Use when with ansible_facts so a task runs only on hosts that match an OS family or distribution.
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
print a message only when ansible_facts os_family is Debian
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
---
- name: Run a task only on Debian-family hosts
hosts: all
gather_facts: true
tasks:
- name: Print on Debian-family systems
ansible.builtin.debug:
msg: "os_family is Debian"
when: ansible_facts['os_family'] == "Debian"How it works
Facts describe each host. Put a fact comparison in when without curly braces. Ansible skips the task on hosts where the expression is false.
When to use it
- Installing packages only on a given OS family
- Combining distribution and major version checks
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-16
Notes
Common keys include os_family, distribution, and distribution_major_version. Group multiple conditions with and, or, and parentheses. Not every fact exists on every host.
Reference
- Conditionals based on ansible_facts (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.