🔰 Dynamic Configuration of Target nodes Using Variable File By Ansible 🔰
🔰 𝗧𝗮𝘀𝗸 𝗗𝗲𝘀𝗰𝗿𝗶𝗽𝘁𝗶𝗼𝗻 :
⚡ 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.
In This task we have to create a dynamic var files , with name of remote system which we will used in the playbook.
In this task I am going to used AWS instances 1. RedHat-8 , 2. Ubuntu-20 you can use your local system also. But the challenge is how ansible SSH them , for that we need to configure our host file and ansible.cfg inventory .
In host file we need to add AWS key , which have been used at the time of instance launching , and also we required the user name of OS. Give user name according to their distribution.
So Let’s start the practical :
1 . Launch the two instances on AWS :
2.Configure the host file :
Here we need to give the user name according to the distribution and also need same aws key pair which have been used at the time of OS launching. Give your key name in .pem format.
3.Configuring ansible.cfg file :
Here we need to configure inventory file to ssh the instances.
In a playbook, we may want to execute different tasks, or have different goals, depending on the value of a fact (data about the remote system), a variable, or the result of a previous task. We may want the value of some variables to depend on the value of other variables. Or we may want to create additional groups of hosts based on whether the hosts match other criteria. We can do all of these things with conditionals.
Ansible uses Jinja2 tests 😊 and filters 😊 in conditionals. Ansible supports all the standard tests and filters, and adds some unique ones as well.
Now I am going to create a var file according to the OS name of distribution.
- Create the variable file with the RedHat.yml name
2. Create the another variable file with the Ubuntu.yml name
Now create the playbook to configure the webserver
After that we have to write create the index.html file which we are going to copy to the root document
Now lets check the list of the hosts
Here you can see that , lists of remote nodes .
Now I am going to ping all of them . To check the connectivity between the controller node and target node .
Here you can see we are successfully able to ping with all the target node
Now run the playbook to configure the webserver
Here we can see that , playbook run successfully without any error.
Now go to the web-browser and check weather the web pages are copied or not ?
Here you can see the webpages are copied successfully and task completed successfully
I hope this article will help you to understand the concept !!