BacklogZero®

Ansible in a Minute · Ansible Core ·

Use facts.d to add local facts

Short answer

Place a .fact file in /etc/ansible/facts.d and read it as ansible_local after setup runs.

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

create facts.d preferences.fact and print ansible_local preferences general asdf

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Add a local fact with facts.d
  hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: Create the facts.d directory
      ansible.builtin.file:
        path: /etc/ansible/facts.d
        state: directory
        mode: "0755"

    - name: Install a static preferences fact
      ansible.builtin.copy:
        dest: /etc/ansible/facts.d/preferences.fact
        mode: "0644"
        content: |
          [general]
          asdf=1
          bar=2

    - name: Re-read local facts
      ansible.builtin.setup:
        filter: ansible_local

    - name: Print the local fact
      ansible.builtin.debug:
        msg: "{{ ansible_local['preferences']['general']['asdf'] }}"

How it works

facts.d files become ansible_local on the next setup run. The file name is the first key. This play prints 1. Re-run setup in the same play if you created the file during that play.

When to use it

  • Storing permanent host preferences that setup can read
  • Separating site facts from playbook vars

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-28

Notes

The .fact file must not be executable when it is INI or JSON. Executable .fact files must print JSON. INI keys become lowercase. Use fact_path on the play when the directory is not /etc/ansible/facts.d.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Use facts.d to add local facts · Level Up Labs