Ansible Playbook which will dynamically load the variable file named same as OS_name:

Hey ConnectionâŠ!!!đââïž
Task14.3
đ©âđ»Here this is my another blog based on
Creating 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 without using keyword âwhenâ.
đDescription:
âTo get the variable in which ansible stores the os_name of the host.
âcreating two variable files for each OS type
for Ubuntu: Ubuntu-20.yml
for RedHat: RedHat-8.yml
So onâŠ
âCreating a main playbook to configure webserver over Redhat and Ubuntu OS which would dynamically load that particular variable file as per OS name ansible retrieve from facts.
Description:
here, we are creating a playbook in such a way that it will retrieve the OS name for example if it is a Redhat OS then you package for httpd server is âhttpdâ but if you want the same for ubuntu OS then again the package name changes which is âapacheâ
So, we can do one thing that we can save this OS name in a variable OS_name then create two variable file one for Redhat OS and Ubuntu OS containing respective package name for Apache webserver.
dynamically load this variable file to the playbook.
Lets startâŠ
Step1: Updated ips of systems in inventory

You can verify the connection using command
#ansible -m ping all
Step 2: To get the variable in which ansible stores the osname of the host
we need to go through the collected facts using command
#ansible all -m setup


Here, you can get the name and version of OS since you need to name variable file as <osname-version.yml>
ansible_distribution and ansible_distribution_version as the variables
Step 3: creating two variable files
for Ubuntu: Ubuntu-20.yml

for RedHat: RedHat-8.yml

Step 4: Creating final playbook to configure webserver over Redhat and Ubuntu OS.
this would dynamically load that particular variable file as per OS name ansible gets from facts.

I have add this to my webpage

Running the playbook we get


Now, checking for the webpage


Thankyou so much âŠ!!!!
Hope you find it interestingâŠ!!!!
Thankyou so much Vimal Daga Sir