Tuesday, October 27, 2020

Ansible Gathering Facts

In this Post we will learn about the Ansible facts, and what are ansible facts ..

Ansible facts are nothing but information about the Managed Nodes like OS distribution, releases, processors,Domain,IP,..etc. ..

we can collect or gather these above piece of information running as a task in ansible controller .

the task of collecting the remote system information is called as gathering the Facts, we ca call it as variable.

we can gather/collect facts about the remote server information by using setup module in Ad-hoc commands.

Important thing here is , by default ansible playbooks will call the setup module and perform the gathering facts task.
  
syntax for this is  :  ansible group_name(in my case my-servers is the group name) -m setup

ansible my-servers -m setup












we will get the lot of info about the managed servers(nodes), we can call it as Dictionary, it consists of list of values or some text 

so setup module will help us to gather the info about the managed servers .

now lets see how we can see only some piece of info from the  above output, ie mount points need to be print .

ansible my-servers -m setup -a "filter=ansible_mounts"



















there are 2 types of ansible Facts or Variable for Managed Nodes

 they are: 

  1.  Default Facts
  2.  Custom Facts
Default Facts: this is nothing but if we run the setup module we will get some facts or some variables or some information  they are called simply Default facts

if we want some extra facts from our managed nodes for that we need to create a custom facts 

custom Facts will helps us to get the user defined required facts 

for example i would like to find the httpd version, weblogic version, db version,git version for all managed nodes on every week,  i can say some sort of inventory of my managed nodes , we can get these details with ad-hoc commands but those will be more, so if we define custom facts that our playbook will get  simple and easy.

Custom Facts are not mandatory, if we want we can define our own to custom facts , that will be use for  playbook. ( reduced the lines in the play book) 

we try to create a custom facts(required info), for now git version and httpd versions through ad-hoc command

steps to create Custom Facts:

step 1: Create folder facts.d  under /etc/ansible on Controlled Node

[ansible@control-server ansible]$ pwd
/etc/ansible
[ansible@control-server ansible]$sudo mkdir facts.d










step2: Inside of facts.d place one or more custom facts files with extension as .fact(it                    may be a shell/python or any) 

   Note: The output of fact file should be a Json.










step 3: .fact file should have execution permissions.

sudo chmod 755 version.fact








output of the .version.fact  file will be as below:









so we got the required output with the Custom facts, now we have to place this custom Facts in all the managed nodes.

create the file in to the remote managed nodes.

ansible all -m file -a "path=/etc/ansible/facts.d state=directory" -b


















Now copy the file from controller node  in to all other managed nodes.

 ansible all -m copy -a "src=/etc/ansible/facts.d/version.fact dest=/etc/ansible/facts.d/version.fact mode=0755" -b

















now we will run the command to get the details from managed nodes.

Custom Facts should be under "ansible_local" variable in the setup module output

for me getting error because of latest ansible used for this lab..















like this we can create our own custom facts and we can make use of it .

No comments:

Post a Comment