Configure web server on different type os using ansible and dynamically take software name according to os type

Laveena Jethani
3 min readMar 30, 2021

--

Hello Everyone,

I want to show how I have configured the webserver on different types of OS like RedHat and Ubuntu. My ansible-playbook will dynamically take software names according to OS type. As we know that in ubuntu apache webserver software name is “apache2” and in RedHat Apache webserver software is “httpd”.So we don’t need of separate configuration for both the OS from one playbook we can configure every type of OS.

I have two target nodes one traget node is running in my local VM which is redhat8 and another trarget node in running in the AWS cloud service ec2 this traget node is Ubuntu OS .

This target node 1: Redhat OS

RedHat OS

This target node 2 Ubuntu OS: ec2 instance

Ubuntu OS

Inventory file

As previously I have mentioned I have two target nodes one running is running locally and one is running on ec2 instance. So in the inventory file, I have mentioned the SSH key for the ec2 instance and the password for the locally running VM.

ansible.cfg file

As we know that ec2 instance(target node 2) running over the AWS is of Ubuntu type and for login to this OS using ssh key we have to use “ubuntu” user. But by “ubuntu” user we can’t do the ansible task. So in the ansible.cfg file I have written the [ privilege_escalation ] keyword.

For dynamically taking software names according to OS type we will use a jinja template language in ansible playbook. So their are two variable file I have created RedHat.yml & Debian.yml. In which I have written software name, service name, content which will write on the web page.

RedHat.yml file

Debian.yml file

For configuring the webserver I have created web page index.html.

index.html file

inde.html

in the index.html file, I have given the {{ content }} variable for loading the content of the web page this content will be taken from the RedHat.yml and Debian.yml file.

PlayBook for configuring the webserver

I have created a file task.yml

Ansible Playbook

Let’s run the playbook task.yml

Playbook run

After running the playbook Webserver is configured in both target nodes

Let’s test both webservers

Webserver Output

As I have shown in the above SC both the IPs of different types of OS. Now web server is configured for both types of OS.

I have uploaded code at Github please have look at the code

GitHub Link:

https://github.com/Laveenajethani/Dynamically-SoftwareName-According-OStype-Configure-Webserver

--

--

Laveena Jethani

Technical Blog Writer | Research & Review different technologies | ARTH learner