Ansible FACTS and Custom FACTS


FACTS are variables related to remote systems.  Ansible has the capability to discover and retrieve certain values from a remote node and store then in variables. This includes capturing the number of CPUs in the remote system, the RAM, hostname, IP, etc.

Facts of a managed node can be gathers by below playbook

[root@centos9vm ~]# vim example_facts.yml

====== ==

– – –
– name: Example for ansible facts
    hosts: testGRP
        – name: Gather facts
                var: ansible_facts

====== ==

Executing the playbook as shown below will gather and display in output several facts about the managed node. Try searching for the hostname in the output !

[root@centos9vm ~]# ansible-navigator run -m stdout example_facts.yml

Custom Facts in Ansible

While the Facts mentioned above are prebuilt variables that holds values gathered from managed nodes, Custom Facts are Ansible facts created by users. These are facts user created variables stored as facts in managed nodes, which can be used when executing playbooks from Ansible server.

The below exercise created a Custom Fact file in the server, copies them to managed node”s folder via a playbook. The Facts file needs to have “.fact” file extension and should be copied to “/etc/ansible/facts.d” folder.

[root@centos9vm ~]# cat custom.fact

=== ==

===== ===

Now create a playbook that will create a folder in the managed node and copy the file:

[root@centos9vm ~]# cat file.yml

====== ====
– – –
– name: Copying a file from local server to the managed node
    hosts: testGRP
        – name: Create a directory
                path: /etc/ansible/facts.d
                state: directory

        – name: Copy the file
                src: custom.fact
                dest: /etc/ansible/facts.d/custom.fact

==== ====

[root@centos9vm ~]# ansible-navigator run -m stdout file.yml

==== ====

PLAY [Copying a file from local server to the managed node] ********************

TASK [Gathering Facts] *********************************************************
ok: []

TASK [Create a directory] ******************************************************
changed: []

TASK [Copy the file] ***********************************************************
changed: []

PLAY RECAP ********************************************************************* : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
==== ====

Let us check the managed node to see if the file got copied

[root@centosMYOBvm ~]# cat /etc/passwd | grep shiju
[root@centosMYOBvm ~]# ls -ltr /etc/ansible/facts.d/

total 4
-rw-r–r–. 1 root root 64 Mar 2 20:37 custom.fact


Now let us create a playbook and execute it, to add a user based on the Custom Fact stored in the managed nodes.

[root@centos9vm ~]# cat example5.yml

====== =====

– – –
– name: Create a user with custom facts
    hosts: testGRP
        – name: User creation
                name: “{{ ansible_facts[‘ansible_local’][‘custom’][‘general’][‘user’] }}”
                uid: “{{ ansible_facts[‘ansible_local’][‘custom’][‘general’][‘uid’] }}”
                state: “{{ ansible_facts[‘ansible_local’][‘custom’][‘general’][‘state’] }}”

===== ======

[root@centos9vm ~]# ansible-navigator run -m stdout example5.yml

===== ======

PLAY [Create a user with custom facts] *****************************************

TASK [Gathering Facts] *********************************************************
ok: []

TASK [User creation] ***********************************************************
changed: []

PLAY RECAP ********************************************************************* : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

===== ======

[root@centosMYOBvm ~]# cat /etc/passwd | grep user800