Use of Ansible Facts.

Mohd Sabir
3 min readFeb 20, 2021

--

Task Descriptionđź“„

Create an Ansible Playbook which will dynamically load the variable file named same as os_name and just by using the variable names we can Configure our target node.

Let’s start doing our task : —

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Let’s write a playbook to Install webserver :

vim web.yml
- hosts: all
tasks:
- name: Install webserver
package:
name: "httpd"
state: present

save this and run , But this playbook can install webserver on the top RedHat Linux, if this playbook we run on ubuntu Linux it will fail why?

Because for webserver apache community create two product one is httpd for RedHat Linux and another is apache2 for ubuntu Linux.

But In our datacenter we have heterogeneous environment , if we run this playbook this can only configure RedHat Linux server not ubuntu Linux server.

So Either we have to search all the ubuntu server and create another playbook for this , But again it is manual , we want automation ,we are using Ansible for automation.

To solve this challenge we can use Ansible facts

What is Ansible facts ?

Ansible facts are the information of remote hosts which is gathered by the Ansible controller. This information is stored in predefined variables on the controller node and the whole set of this information is prepared in JSON format. This is a very important feature as we can make decisions about which task is to perform on which remote machine based on these facts.

Ansible facts are fetched from remote hosts using setup module which runs automatically every time, if not disabled. This can be done by running the setup module on the command line using the adhoc method or by default in a playbook.

#adhoc command
ansible 192.168.43.83 -m setup

At the time of writing playbook we can use ansible facts to retrieve the name of OS , and write the playbook accordingly.

Let’s write the playbook which will automatically retrieve the name of Operating system in a variable “os_name” and then we use this variable in our playbook

- hosts: all
vars:
- os_name: " {{ ansible_facts['distribution'] }} "
tasks:
- name: Install the webserver in RedHat linux
package:
name: "httpd"
state: present
when: os_name == "Redhat"
- name: Install the webserver in ubuntu Linux
package:
name: "apache2"
state: present
when: os_name == "ubuntu"

Now this above playbook can run on ubuntu Linux and RedHat Linux .

Let’s run this playbook

By chance I have not ubuntu Linux in my environment so that’s why they skip the task .

Thanks for Reading.

--

--

Mohd Sabir
Mohd Sabir

Written by Mohd Sabir

DevOps Enthusiastic || Kubernetes || GCP || Terraform || Jenkins || Scripting || Linux ,, Don’t hesitate to contact on : https://www.linkedin.com/in/mohdsabir

No responses yet