Ansible FACTS and Custom FACTS

Ansible_Logo

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
    tasks:
        – name: Gather facts
            ansible.builtin.debug:
                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

=== ==
[general]
user=user800
uid=1010
state=present
[a1]
user=user810

===== ===

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
    tasks:
        – name: Create a directory
            ansible.builtin.file:
                path: /etc/ansible/facts.d
                state: directory

        – name: Copy the file
            ansible.builtin.copy:
                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: [192.168.48.129]

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

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

PLAY RECAP *********************************************************************
192.168.48.129 : 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
    tasks:
        – name: User creation
            ansible.builtin.user:
                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: [192.168.48.129]

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

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

===== ======

[root@centosMYOBvm ~]# cat /etc/passwd | grep user800
user800:x:1010:1010::/home/user800:/bin/bash