{"id":1100,"date":"2024-02-27T06:04:17","date_gmt":"2024-02-27T06:04:17","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1100"},"modified":"2024-03-12T05:55:34","modified_gmt":"2024-03-12T05:55:34","slug":"ansible-facts-and-custom-facts","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1100","title":{"rendered":"Ansible FACTS and Custom FACTS"},"content":{"rendered":"<p>FACTS are variables related to remote systems.\u00a0 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.<\/p>\n<p>Facts of a managed node can be gathers by below playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> vim example_facts.yml<\/p>\n<p>====== ==<\/p>\n<p><em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Example for ansible facts<\/em><br \/>\n<em>&nbsp; &nbsp;  hosts: testGRP<\/em><br \/>\n<em>&nbsp; &nbsp;  tasks:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp;  &#8211; name: Gather facts<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ansible.builtin.debug:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var: ansible_facts<\/em><\/p>\n<p>====== ==<\/p>\n<p>Executing the playbook as shown below will gather and display in output several facts about the managed node. Try searching for the\u00a0<em>hostname<\/em> in the output !<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout example_facts.yml<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Custom Facts in Ansible<\/strong><\/span><\/p>\n<p>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\u00a0executing playbooks from Ansible server.<\/p>\n<p>The below exercise created a Custom Fact file in the server, copies them to managed node&#8221;s folder via a playbook. The Facts file needs to have &#8220;.fact&#8221; file extension and should be copied to &#8220;\/etc\/ansible\/facts.d&#8221; folder.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat custom.fact<\/p>\n<p>=== ==<br \/>\n<em>[general]<\/em><br \/>\n<em>user=user800<\/em><br \/>\n<em>uid=1010<\/em><br \/>\n<em>state=present<\/em><br \/>\n<em>[a1]<\/em><br \/>\n<em>user=user810<\/em><\/p>\n<p>===== ===<\/p>\n<p>Now create a playbook that will create a folder in the managed node and copy the file:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat file.yml<\/p>\n<p>====== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em> &#8211; name: Copying a file from local server to the managed node<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: testGRP<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Create a directory<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.file:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path: \/etc\/ansible\/facts.d<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: directory<\/em><\/p>\n<p><em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Copy the file<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.copy:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: custom.fact<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dest: \/etc\/ansible\/facts.d\/custom.fact<\/em><\/p>\n<p>==== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout file.yml<\/p>\n<p>==== ====<\/p>\n<p><em>PLAY [Copying a file from local server to the managed node] ********************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Create a directory] ******************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Copy the file] ***********************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n==== ====<\/p>\n<p>Let us check the managed node to see if the file got copied<\/p>\n<p><strong>[root@centosMYOBvm ~]#<\/strong> cat \/etc\/passwd | grep shiju<br \/>\n<strong>[root@centosMYOBvm ~]#<\/strong> ls -ltr \/etc\/ansible\/facts.d\/<\/p>\n<p>======<br \/>\n<em>total 4<\/em><br \/>\n<em>-rw-r&#8211;r&#8211;. 1 root root 64 Mar 2 20:37 custom.fact<\/em><\/p>\n<p>==========<\/p>\n<p>Now let us create a playbook and execute it, to add a user based on the Custom Fact stored in the managed nodes.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat example5.yml<\/p>\n<p>====== =====<\/p>\n<p><em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Create a user with custom facts<\/em><br \/>\n<em>  &nbsp; &nbsp; hosts: testGRP<\/em><br \/>\n<em>  &nbsp; &nbsp; tasks:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp; &#8211; name: User creation<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; ansible.builtin.user:<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; name: &#8220;{{ ansible_facts[&#8216;ansible_local&#8217;][&#8216;custom&#8217;][&#8216;general&#8217;][&#8216;user&#8217;] }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; uid: &#8220;{{ ansible_facts[&#8216;ansible_local&#8217;][&#8216;custom&#8217;][&#8216;general&#8217;][&#8216;uid&#8217;] }}&#8221;<\/em><br \/>\n<em>  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp;  &nbsp; &nbsp; state: &#8220;{{ ansible_facts[&#8216;ansible_local&#8217;][&#8216;custom&#8217;][&#8216;general&#8217;][&#8216;state&#8217;] }}&#8221;<\/em><\/p>\n<p>===== ======<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout example5.yml<\/p>\n<p>===== ======<\/p>\n<p><em>PLAY [Create a user with custom facts] *****************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [User creation] ***********************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>===== ======<\/p>\n<p><span style=\"color: #993300;\"><strong>[root@centosMYOBvm ~]#<\/strong><\/span> cat \/etc\/passwd | grep user800<br \/>\nuser800:x:1010:1010::\/home\/user800:\/bin\/bash<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>FACTS are variables related to remote systems.\u00a0 Ansible has the capability to discover and retrieve certain values from a remote node and store then in <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1100\" title=\"Ansible FACTS and Custom FACTS\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1100","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1100"}],"version-history":[{"count":20,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions"}],"predecessor-version":[{"id":1180,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions\/1180"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}