0Pricing
Ansible Academy · Lesson

Use ansible_facts in Tasks

Branch on OS, memory and IP facts.

Use ansible_facts in Tasks is a free Ansible Academy lesson on CoddyKit — lesson 3 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.

From Data to Decisions

Gathered facts are only useful if you act on them. Now you will pull values from ansible_facts straight into your tasks. 🌟

Reference a Fact

Use the same {{ }} syntax as any variable. Inside the braces, name the fact you want, like the host's distribution.

- debug:
    msg: "OS is {{ ansible_distribution }}"

Dictionary Access Too

You can also read facts through the ansible_facts dictionary with bracket keys. Both styles point to the same data.

{{ ansible_facts['distribution'] }}

Branch on the OS

Combine a fact with when to run a task only on matching hosts, like installing a package on Debian systems only.

when: ansible_os_family == "Debian"

Pick the Right Package

Facts let one task serve many distros. Choose apache2 or httpd based on the OS family instead of writing two playbooks.

name: "{{ 'apache2' if ansible_os_family == 'Debian' else 'httpd' }}"

Use Numeric Facts

Facts like ansible_memtotal_mb are numbers, so you can compare them and skip work on hosts that are too small.

when: ansible_memtotal_mb > 2048

Reach Nested Values

For nested facts, chain the keys with dots. The default IPv4 address sits under ansible_default_ipv4 then address.

{{ ansible_default_ipv4.address }}

Facts in Templates

The same fact names work inside Jinja2 templates. Drop a host's hostname or IP straight into a generated config file.

server_name {{ ansible_hostname }};

Loop Over List Facts

Some facts are lists, like ansible_interfaces. You can loop over them to act on every network interface a host has.

loop: "{{ ansible_interfaces }}"

Confirm Before Trusting

Not sure of a fact's exact value? Print it with debug first, then build the logic that depends on it.

- debug:
    var: ansible_distribution_version

One Playbook, Many Hosts

Because facts are per-host, fact-driven tasks make a single playbook behave correctly across a mixed fleet of machines.

Quick Check

How would you run a task only on Debian-family hosts?

Recap

You read facts with {{ }}, branched with when, reached nested values and looped over list facts to make tasks adaptive. ✨

Frequently asked questions

Is the “Use ansible_facts in Tasks” lesson free?

Yes — the full text of “Use ansible_facts in Tasks” 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 “Use ansible_facts in Tasks”?

Branch on OS, memory and IP facts. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Use ansible_facts in Tasks” 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

  1. What Facts Are & Why They Matter
  2. Explore Facts with the setup Module
  3. Use ansible_facts in Tasks
  4. Disabling Fact Gathering for Speed
← Back to Ansible Academy