DEV Community

Cover image for Configuration of webserver using Dynamic Variable
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Configuration of webserver using Dynamic Variable

Hello everyone,

In this blog, we will be 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.

Let's get started:

First create inventory in the Controller node

Alt Text

Here I have created an inventory and added the IP of my target nodes.

Now check the connectivity between managed node and controller node by pinging

Alt Text

Now create an ansible configuration file

To create an ansible conf file, create a file with an extension cfg.

Alt Text

Now I have created Variable files for RedHat Os and Ubuntu Os

  • For Ubuntu OS create a variable file named Ubuntu-20.yml

Alt Text

  • For Redhat Os create a variable file named RedHat-8.yml

Alt Text

Now I have created an HTML page

Alt Text

I have used a variable in the code that will dynamically fetch the OS name and print it.

And I will upload this page to the root document of OS

Now create Playbook to configure Webserver in both OS

Alt Text

This playbook will dynamically find the OS and configure the Webserver in it.

Now run the Playbook

To run the playbook use the command ansible-playbook

Alt Text

Here you can see the playbook run successfully!!!

Now let's check in our Os is the webserver configured or not?

  • In RedHat OS

Alt Text

Here you can see the webserver is configured successfully!!

  • In Ubuntu OS

Alt Text

Here also Web server is configured successfully!!

Now lets check webserver is working or not

  • copy Redhat OS IP and paste that IP in browser with HTML web page that we have uploaded

Alt Text

Here you can see it is working.

  • Now let's check for Ubuntu.

Alt Text

So that how we can configure web servers in different OS using Dynamic Variable.

Thank you!!!

Top comments (0)