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.)

suman15
4 min readMay 29, 2021

--

LET’S JUMP TO OUR PRACTICAL

FIRST OF ALL, WE NEED TWO DIFFERENT TYPES OF OS FOR THIS PRACTICAL

Here we are going to launch EC2 instances manually over an AWS cloud

Use the below document for launching instances manually:

Either you can launch from AWS CLI :

Or you can launch it from ansible also, its up to you all!!

Finally we successfully launched redhat os and ubuntu os on AWS cloud

UPDATE THE IP’S OF THE OS IN THE INVENTORY FILE

The inventory file can list individual hosts or user-defined groups of hosts. This enables you to define groups of devices running OS with similar roles upon which to perform the same operational and configuration tasks.

UPDATE THE ANSIBLE CONFIGURATION FILE:

Ansible is so flexible, and it chooses its configuration file from one of several possible locations on the control node. One use case where this might be useful would be managing a web server and a database server. You might need to gather facts from one host and not on the other one. Having an ansible.cfg in the current project working directory can facilitate this behavior.

By default Ansible reads its configuration file in /etc/ansible/ansible.cfg

Make sure to change the permissions of the .pem file

# chmod 400 keyname.pem

After connecting to both the servers, we want that variable name which gives us a remote host type variable. The setup module gives many variables under the “ansible_distribution” section. This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks.

LET’S SEE THE VARIABLE NAME

For getting the variable name run the below command :

We got the below variable name for redhat OS

Do the same for ubuntu OS

Now, we know the distribution name, we will create variable files and give this distribution name to that files i.e Ubuntu.yml, RedHat.yml

CREATE TWO VARIABLE FILES

# vim RedHat.yml

# vim Ubuntu.yml

CREATE FINAL PLAYBOOK

Now we will create final playbook to load the particular variable file as per the os name which ansible will get from ansible facts and It will also configure webserver on both the OS

Below is the webpage for testing (we have used this page in the above playbook):

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, we can use the behavior or state of one system as configuration on other systems. For example, you can use the IP address of one system as a configuration value on another system.

NOW WE WILL RUN THE PLAYBOOK

ANSIBLE PLAYBOOK RAN SUCCESSFULLY

For checking the status of httpd, go to target node and run the below commands :

IN REDHAT OS
IN UBUNTU OS

OUTPUT

NOW, CHECK THE WEBPAGES

To access the webpage enter ip/index.html.j2

SUCCESSFULLY DONE!!

Link of Github repository for your reference:

We come to the end of this article. We have successfully load the variable file named same as OS_name and just by using the variable names we have configured our target node.

Thank you!!

--

--

No responses yet