Ansible Tutorial · Configuration Management · Lesson 11 of 20

Ansible Facts

Gather and use system information from managed nodes.

Members can save this page, take notes and track progress. See membership · Sign in

What are facts?

When a play starts, Ansible runs the setup module on every host to collect system information called facts: hostname, OS, IP addresses, CPU count, memory, disk layout and much more.

By default gather_facts: true (the default), so facts are available automatically.

Useful facts

Fact Example value
ansible_hostname web-1
ansible_fqdn web-1.example.com
ansible_os_family Debian
ansible_distribution Ubuntu
ansible_distribution_version 24.04
ansible_default_ipv4.address 10.0.1.5
ansible_memtotal_mb 4096
ansible_processor_vcpus 2
ansible_python_version 3.12.3

Using facts in tasks

- name: Show OS info
  ansible.builtin.debug:
    msg: "{{ ansible_distribution }} {{ ansible_distribution_version }} ({{ ansible_os_family }})"

- name: Set timezone (Debian only)
  community.general.timezone:
    name: UTC
  when: ansible_os_family == "Debian"

Filtering facts

Gathering all facts takes a few seconds. You can filter to collect only what you need:

ansible webservers -m setup -a "filter=ansible_*memory*"
ansible webservers -m setup -a "filter=ansible_distribution*"

In a playbook, you can limit fact gathering:

- name: Gather only network facts
  hosts: all
  gather_facts: false
  tasks:
    - ansible.builtin.setup:
        gather_subset:
          - network

Custom facts (local facts)

Create JSON or INI files in /etc/ansible/facts.d/ on the managed node and Ansible picks them up as ansible_local:

# On the managed node:
sudo mkdir -p /etc/ansible/facts.d
cat > /etc/ansible/facts.d/app.fact << 'EOF'
[release]
version=2.5.1
deploy_date=2026-11-20
EOF

Access in a playbook:

- debug:
    msg: "App version {{ ansible_local.app.release.version }}"

Disabling fact gathering

For plays that do not need facts, skip gathering to speed things up:

- name: Quick task
  hosts: all
  gather_facts: false
  tasks:
    - name: Ping hosts
      ansible.builtin.ping:

Fact caching

For large inventories, enable fact caching so Ansible does not re-gather facts on every run:

# ansible.cfg
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_fact_cache
fact_caching_timeout = 3600

smart means: only gather if facts are not cached or the cache has expired.

Gather a single fact
ansible localhost -m setup -a "filter=ansible_hostname" 2>/dev/null

Output

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "control-node"
    },
    "changed": false
}

Run this on your computer — see “Get Started” for setup.

Filter distribution facts
ansible localhost -m setup -a "filter=ansible_distribution*" 2>/dev/null

Output

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "Ubuntu",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/os-release",
        "ansible_distribution_file_variety": "Debian",
        "ansible_distribution_major_version": "24",
        "ansible_distribution_release": "noble",
        "ansible_distribution_version": "24.04"
    },
    "changed": false
}

Run this on your computer — see “Get Started” for setup.

Check total memory
ansible localhost -m setup -a "filter=ansible_memtotal_mb" 2>/dev/null

Output

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_memtotal_mb": 7938
    },
    "changed": false
}

Run this on your computer — see “Get Started” for setup.

Test yourself

What does 'gather_facts: false' do?

  1. Skips the setup module, so no facts are collected
  2. Collects facts but does not store them
  3. Collects only custom facts
  4. Disables fact caching
Show answer

Answer: Skips the setup module, so no facts are collected

Setting gather_facts to false prevents Ansible from running the setup module at the start of the play, saving time when facts are not needed.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake