Configuring Dynamic Playbook where variable values automatically change as the O.S. name changes…

Arjun Nigam
3 min readDec 27, 2020

Yes, you read the title right. I am back with another interesting practical where I am gonna show you how to 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 ).

And indeed, we are gonna complete this task without using “when” keyword.

Wanna know how???
So let’s get started…

For this task, I have taken Two O.S. two configure: RedHat and Ubuntu.

Before getting on with the task, let’s check our connectivity with our hosts.

And as you can observe in the image below, we are connected…

In this task we are gonna configure web servers on both the O.S.

And as you all know:

In Redhat 8.3 — web server is httpd

But in Ubuntu / Debian-20.04 — httpd name is changed to apache 2

As you can observe in the images below, I have created 2 .yml file one having variable values for RedHat and the other for Debian.

As you can observe in the image below, I have used Ansible facts pre — defined variables for dynamically loading the respective O.S. file and configuring Web Server in my Ansible Playbook, link to which is given at the end of this article.

Please note: As you can see in the image below , the content web page also has a pre-defined variable which contains the name of the O.S.

So when we will check for the Web Page , the name of the O.S. will change with respect to the O.S. it is running on. :)

And as you can image below , my playbook ran successfully…

And now it’s time to check for our web pages.

And as you can observe in the images below. It’s working perfectly fine and the O.S. name is different in both the webpages which tells the name of the O.S. the webpage is configured on.

Web Server running on RedHat

Web Server running on Debian/Ubuntu

That is how you can 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. And without using “when” keyword .

That was all from my side. Hope you found this interesting, I will be back with some other interesting practicals.

Till then, keep learning :)

View the Playbook used on Github:

https://github.com/Arjun0071/osbased_play

Arjun Nigam

https://www.linkedin.com/in/arjun-nigam-4728b11b8

Signing Off!!!

--

--