Branch on OS Family Facts
Different steps for Debian vs RedHat.
Branch on OS Family Facts is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Playbook, Many Distros
Debian uses apt, RedHat uses dnf. With facts and when, a single playbook adapts to each operating system. 🐧
The os_family Fact
Ansible gathers ansible_os_family, a broad label like Debian or RedHat that groups related distributions together.
when: ansible_facts["os_family"] == "Debian"Family vs Distribution
os_family is broad; ansible_distribution is exact. Ubuntu and Debian share the Debian family but differ as distributions.
A Debian-Only Task
Gate an apt task so it runs only on the Debian family. RedHat hosts simply skip it.
- name: Install nginx (apt)
ansible.builtin.apt:
name: nginx
when: ansible_os_family == "Debian"The RedHat Counterpart
Add a sibling task for the RedHat family. Now both worlds are covered by the same play.
- name: Install nginx (dnf)
ansible.builtin.dnf:
name: nginx
when: ansible_os_family == "RedHat"The package Shortcut
For simple installs the generic package module picks the right tool automatically, so you may not even need the branch.
Strings Are Case Sensitive
Family names are exact: it is RedHat, not Redhat or redhat. A wrong case silently skips every host. ⚠️
Other Useful OS Facts
Beyond family there is ansible_distribution_version and ansible_distribution_major_version for finer, version-aware decisions.
when: ansible_distribution_major_version == "9"Inspect Before You Branch
Not sure of a value? A quick debug task prints the fact so you can write the exact condition you need.
- ansible.builtin.debug:
var: ansible_os_familyFacts Must Be Gathered
Branching on OS facts requires gather_facts to be on. If you disabled it, the os_family variable will be undefined.
Portable Playbooks Win
One file that handles every distro is easier to maintain than per-OS copies. Facts plus when make automation portable. 🌍
Quick Check
Let us pin down OS branching.
Recap
Use ansible_os_family in when to branch Debian vs RedHat. Keep facts on and match the exact name. ✅
Frequently asked questions
Is the “Branch on OS Family Facts” lesson free?
Yes — the full text of “Branch on OS Family Facts” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Branch on OS Family Facts”?
Different steps for Debian vs RedHat. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Branch on OS Family Facts” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Run a Task Only when True
- Branch on OS Family Facts
- Combining Conditions with and / or
- Register a Result, Then Decide