Configuring target node using only variable name same as OS_name.

Harshil Shah
4 min readMar 27, 2021

Creating an Ansible Playbook which will dynamically load the variable file named same as OS_name and just by using the variable names we will Configure our target node.( Note: No need to use when keyword here. )

In different OS, software name is different. For example, web server software name in RedHat Linux is httpd, while same software’s name in Ubuntu OS will be apache2. So will use ansible facts in combination with variables to configure dynamically in both OSs with same playbook.

Note: This practical I have performed on AWS cloud instances. I have launched three instances.

  1. Ansible manage node. → Amazon Linux OS

2. Target node → RedHat Linux OS

3. Target node → Ubuntu OS

Now I have already installed and configured ansible in my controller node. You can refer to below article if you haven’t already configured ansible 👇🏼

Inventory file 👇🏼 :

→ Checking the connectivity with both target nodes 👇🏼 Looks good.

Creating two variable files having name same as target node’s OS name.

Like when I ran ansible command of setup module for target node having IP → 13.213.4.183 👇🏼

ansible 13.213.4.183 -m setup

than I found that the OS distribution name is RedHat 👇🏼

So I will create a variable file having same name as this → RedHat.yml

And it will contain a variable named software, having value → httpd. Since RedHat OS supports this software 👇🏼

Similarly running setup module for ubuntu OS, the OS distribution name is Ubuntu.

So I will create a variable file having same name as this → Ubuntu.yml

And it will contain a variable named software, having value → apache2. Since Ubuntu OS supports this software 👇🏼

Now creating main playbook and running it. multipleos.yml

Running the playbook:

playbook is ran successfully.

Now checking if apache web server is successfully installed and started in both systems. i.e RedHat and Ubuntu.

First checking in Ubuntu 👇🏼

Now in RedHat OS 👇🏼

Web server is successfully running. Now I will quickly try and connect to both web servers. As we know, web server by default listens on port number 80.

(Left image) Ubuntu and (right image) RedHat

My php code for demo web-page 👇🏼

That is it for this article. See you all in next one 😀

--

--