In this blog we will see the solution of the given problem statement:

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 ( Note: No need to use when keyword here. )

For doing this practical we require three operating systems, RHEL, Ubuntu and one for Controller Node. We need to have ansible installed and configured in our controller node.

Here, I am using the RedHat-8 and Ubuntu-20 operating systems to perform this but you can perform any type of operating system.

For doing this practical we need to know about ansible facts — With Ansible you can retrieve or discover certain variables containing information about your remote systems or about Ansible itself. Variables related to remote systems are called facts. With facts, you can use the behavior or state of one system as configuration on other systems.

Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries.

"ansible_distribution" and "ansible_distribution_major_version" facts use here to know about OS name and its version so that we can create a dynamic playbook for deploying the webpage without using any condition.

Now , we are create separate playbook for each manage node that we are using in main playbook.

RedHat-8.yml:

Ubuntu-20.yml:

webpage- index.html.j2 :

Now the main playbook which will deploy the webpage dynamically

And at last we need to run the main.yml using

ansible-playbook main.yml

Webpage in browser

This work is done in collaboration with Prithviraj Singh.

That’s all folks. Thank you for reading :)

Devops Enthusiast