Created an Ansible Playbook which will dynamically load the variable file named same as OSname and just by using the variable names we can Configure our target node.
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…!!!!