HOW TO CREATE AN ANSIBLE PLAYBOOK WHICH DYNAMICALLY LOAD THE VARIABLE FILE NAMED SAME AS OS_NAME AND CONFIGURE THE OS.

Here we gonna discuss about How we can Create an Ansible playbook which 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 first discuss about what is Ansible?

WHAT IS ANSIBLE?

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. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

So, let’s go deep dive into the Practical Part.

STEP-1:- For doing this practical, you should have Two OS running( CONTROL NODE AND TARGET NODE), and here I’m using RED HAT 8 OS, that’s why I already configured yum in my both Nodes, and also I have ansible installed in my Controller Node.

STEP-2:- Here my Target Node IP is 192.168.43.249. Firstly we have to create an Inventory File of Ansible where we have to put our Target Node Credentials. Command to create an File is:-

vim file_name.txt

1.1

Here, I created one group named as WEBSERVER, and then I updated it by giving my USER & PASSWORD of Target Node.

STEP-3:- Now we have to tell the Ansible that treat this file as your Inventory File. So for doing this we have to update the Configuration file of Ansible. Location- /etc/ansible/ansible.cfg. Command:-

vim /etc/ansible/ansible.cfg

1.2

STEP-4:- Now create an Playbook File with extension .yml, where we will write our Ansible Code. Command:-

vim task14.yml

1.3

For dynamically Load the Variable name named as Same OS_NAME. I have used the ANSIBLE_FACTS variable of Ansible. And In package module and template module I have used two variables named as PACKAGE & PAGE respectively. This variable I already declared in my Variable File. Here I waana configure the Web Server in my Target Node.

ANSIBLE FACTS:- Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the ansible_facts variable. By default, you can also access some Ansible facts as top-level variables with the ansible_ prefix.

1.4

In Ansible facts variable we have One Key named as Distribution where ansible stores the OS Name. This Distribution key I used in my Playbook to load the Variable name file dynamically.

Let’s take an example, we have to configure the Web Server in our Target Nodes, but the challenge is in Ubuntu OS the web server name is APACHE and in RED HAT OS it named as HTTPD. And in Playbook we have to write the name of the Package. So, here we just have to create an OS_named Variable file, ansible playbook automatically fetch that file using ansible_facts variable.

STEP-5:- Now we just have to create an Variable File same named as the OS_Name. In this practical I created two files of two OS RED HAT 8 & UBUNTU OS.

In this files, we just have to create Variables. In package variable I have given the name of Web Server of both the OS, & in Page I have given the Web Page. By doing this our playbook become that much intelligent, that it automatically fetch the OS name and Web Server name of the Particular OS.

STEP-6:- Create the Web Page in same folder with same name that we have given in Variable file and Run the playbook. Command to run the playbook:-

ansible-playbook task14.yml

If your OS have Network Connectivity and if your Syntax is correct. Then you will get the above output. It means our playbook has run successfully.

NOTE:- CONFIGURE THE YUM FIRST IN TARGET NODE IF YOUR OS IS RED HAT.

STEP-7:- Now our Web Server is configured Successfully in IP 192.168.43.249 with the web page containing-

Now by using the URL in Browser-

http://192.168.43.249/RedHat.html

We will see the Web Page their-

HURRAY, FINALLY WE HAVE COMPLETED OUR PRACTICAL…

THANK YOU FOR READING THIS ARTICLE, HOPE IT IS HELPFULL FOR YOU….

FOR MORE SUCH TYPE OF ARTICLES, STAY TUNED.

GIT HUB LINK FOR CODE:-