Create and Use Custom Facts

AnsibleBeginner
Practice Now

Introduction

Custom facts let managed hosts publish local data to Ansible. Facts placed under /etc/ansible/facts.d become available through ansible_local after fact gathering.

In this lab, you will install a small custom fact file and use it in a second play to render a report.

Create a Custom Fact Project

In this step, you will create a project for installing and reading a custom fact.

cd /home/labex/project
mkdir -p rhce-ansible/{inventory,files,playbooks,reports}
cd rhce-ansible
cat > ansible.cfg <<'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
retry_files_enabled = False
EOF
cat > inventory/hosts <<'EOF'
[managed]
host ansible_connection=local
EOF

Custom facts are installed on the managed host, so this lab needs both a source file under the project and a playbook that copies it into /etc/ansible/facts.d.

Create a Local Fact Source File

In this step, you will create an INI-style fact file. The section name and keys will appear under ansible_local.rhce_custom.general. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > files/rhce_custom.fact <<'EOF'
[general]
course=rhce-ansible
track=custom-facts
EOF

The file uses INI-style syntax. After fact gathering, the section name general and its keys become available under ansible_local.rhce_custom.general.

cat files/rhce_custom.fact

Write a Playbook that Installs and Reads the Fact

In this step, you will write a two-play playbook. The first play installs the fact file with privilege escalation; the second play gathers facts again so ansible_local includes the new data. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > playbooks/custom-fact.yml <<'EOF'
---
- name: Install custom fact
  hosts: managed
  become: true
  gather_facts: false
  tasks:
    - name: Ensure facts directory exists
      ansible.builtin.file:
        path: /etc/ansible/facts.d
        state: directory
        mode: '0755'

    - name: Install RHCE custom fact
      ansible.builtin.copy:
        src: ../files/rhce_custom.fact
        dest: /etc/ansible/facts.d/rhce_custom.fact
        mode: '0644'

- name: Read custom fact after gathering facts
  hosts: managed
  gather_facts: true
  tasks:
    - name: Write custom fact report
      ansible.builtin.copy:
        dest: /home/labex/project/rhce-ansible/reports/custom-fact.txt
        content: |
          course={{ ansible_local.rhce_custom.general.course }}
          track={{ ansible_local.rhce_custom.general.track }}
        mode: '0644'
EOF

The first play installs the fact file with become because /etc/ansible/facts.d is a system path. The second play gathers facts again; without that second gather, ansible_local would not include the newly installed fact in the same run.

ansible-playbook playbooks/custom-fact.yml --syntax-check

Run the Custom Fact Playbook

In this step, you will install the custom fact and render a report from ansible_local. The first play modifies system fact storage, and the second play proves Ansible can read it. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
ansible-playbook playbooks/custom-fact.yml | tee reports/custom-fact-run.txt

The first run should change the system by installing the fact file and writing the report. The saved output is useful if the second play cannot find ansible_local.

cat reports/custom-fact.txt

The report should contain the course and track values from the local fact file, proving Ansible gathered and used the custom fact.

Inspect ansible_local Directly

In this step, you will use the setup module with a filter to view the custom fact tree directly. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
ansible managed -m ansible.builtin.setup -a 'filter=ansible_local' | tee reports/ansible-local.txt

The setup module output is the raw fact tree. Filtering to ansible_local keeps the output focused on locally supplied custom facts.

grep -E 'rhce_custom|custom-facts|rhce-ansible' reports/ansible-local.txt

The grep output should show the custom fact name and the values you wrote. This is the direct inspection path to use when a playbook cannot see ansible_local.

Summary

You installed a host-local custom fact, gathered facts again, used ansible_local in a playbook, and inspected the custom fact through the setup module.