Skip to content

Example 12

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

files/samplefacts1.fact

{
    "jsonfacts": {
        "key1": "value1",
        "key2": "value2"
    }
}

files/samplefacts2.fact

[inifacts]
key1 = value1
key2 = value2

playbook.yml

- name: Example playbook
  hosts: provisioner
  tasks:
    - debug:
        msg: "{{ ansible_local.samplefacts1.jsonfacts }}"
    - debug:
        msg: "{{ ansible_local.samplefacts2.inifacts }}"

Tip

Ansible allows the user to set his custom facts in JSON or INI formats. The files need to have a .fact extension.

Execute the following command

ansible-playbook playbook.yml

What is the expected output of the command ?

The command will fail since we have not pointed the location of the newly created facts to Ansible.

Bonus round

Using the playbook-scope directive fact_path and special built-in variable {{ playbook_dir }} point Ansible towards the directory, where the custom facts are stored.