Ansible Playbook which will Dynamically load the Variable file named same as OS_Name and by using the same file Configuring WebServer on the Target Node!
Hello Folks! In this Blog we’ll discuss about How we can Create an Ansible Playbook which will Dynamically load the Variable file and by using the same file how we can configure Ansible Target Nodes. So, guys let’s get going…
Here, Ansible Playbook will be created in such a way that it will retrieve the OS name (let’s say RedHat OS) and then will install the package (let’s say httpd) and further we configure the WebServer . But what if the OS name changes?? So, in that case the same Package name won’t work. For example, If the OS is Ubuntu then the Package to be installed for configuring WebServer would be apache. The Package vary from OS-to-OS.
So, here comes a solution to it, we can retrieve the OS name and save it in one Variable file. So, as and now when the Requirement come up the respective Packages will Installed Dynamically.
Pre-requisites:
- Ansible to be installed.
2. One should have at least Two OS(let’s say one is RedHat 8 and the other would be let’s say Ubuntu).
So, let’s implement the Practical →
Here , In my case I’ve one RedHat(RHEL8) Virtual Machine as one of my Target Node in which no packages are installed.
And the other is Ubuntu Virtual Machine which I’ve launched in AWS Cloud.
Step 1: Creating an Inventory file of Ansible.
Here, In my case I’ve created a separate directory /etc/hosts.txt and then I’ve provided the Credentials for Login to the Target Nodes
So, I’ve created one group named as Redhat for configuring RedHat Target Node and then I’ve created a group named called Ubuntu for configuring Ubuntu Target Node.
Step 2: Configuring Ansible Inventory.
For configuring Ansible →
Go inside the /etc/ansible/ansible.cfg and mentioned the inventories as mentioned below.
Check it by following command
ansible all --list-hosts
Check the connection between Controller Node and Target Node using the below command
ansible all -m ping
Step 3: Retrieving the Variable name from Ansible Facts.
Here, retrieving the variable name from Ansible facts is quite important as we’re gonna use it.
For doing so, use the below command.
ansible all -m setup
From the Ansible Facts you can get the Name and Version of Operating System
Now we’re good to go…
Step 4: Creating a Variable File.
For creating a variable file for both RedHat and Ubuntu you can use the syntax as below
<distribution-distribution-major-version.yml> Example: RedHat-8.yml
Ubuntu-20.yml
Step 5: Creating a Playbook that will Dynamically load the Variable file
Here is the Playbook for the same
And Finally, running the Playbook…
ansible-playbook <name.yml>
The Playbook ran successfully and the Packages names are Retrieved Successfully!!
Step 6: Testing the WebServer.
Fetch the IP of your System and browse it in your browser.
Voila!! Both the Web Server’s has been successfully Deployed.
Hope you find this Blog easy🤞!!
Happy Reading!!😊
Do like Share and Follow me
Thank you !!