🔰 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.)
Let’s start this task very simple task the approach i used is with the concept of var_files.
Before I start explaining my start First lets see what all files we have for this task.
ANSIBLE CONFIGURATION FILE
These are the aws instances I’m using here and that’s why I am using ssh_private_key. Note the ansible_user image to image this might change so can check this user on aws instance part. Their select on the instance click on connect their they show the user_name.
These are the instances I’ll be using.
These are my variable files
YML FILE (setup.yml)
hosts: all -: this part contains two users as we already seen in the above inventory file.
become: true -: using privilege escalation so that we can use sudo power to configure the webserver.
var_files: in this we are using variable name ansible_distribution which we get during gathering of fact if it’s RedHat our RedHat.html file will be called. Why it’s important becaure different distributions contains different different package and service name and this cause issue because to install any package we must know the name of the package and for that we first know the name of distribution we are using.
If u have some dependencies error this may occur due to ….
You are running this playbook in newly launched or provisioned instance this might fails due to extrapackages ( Ex. python) needed for ansible in the instances. For that U’ll have to add extra line of code in the starting of the yml code.
Ping the instances
ansible all -m ping
LETS RUN THE PLAYBOOK!!
- RedHat Instance
2. Ubuntu Instance
For any query ping me and connect with me on LinkedIn www.linkedin.com/in/vineet-negi