Create An Ansible playbook Which Dynamically Loads Variable File Named the Same As Os_Name And Configure The Os
Create an Ansible Playbook which will dynamically load the variable file named the 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.)
Configuration plan
1. create two variable files named OS_name.yml
2. install an apache web server on managed nodes and start service
NOTE: all the source code is in the GitHub link provided in the last
Let’s Get Started
Inventory File[Data.txt]
Ansible uses this file to check host connectivity it is a kind of IP database.
[WebServer]192.168.0.109 ansible_connection=ssh ansible_user=venkatesh ansible_ssh_pass=root192.168.0.113 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root
Configuration File[ansible.cfg]
Ansible uses this file to check its configurations.
[defaults]inventory = data.txthost_key_checking = Falsedeprecation_warnings = falseroles_path = roles:/usr/share/ansible/roles
[privilege_escalation]become = yesbecome_method = sudobecome_user = rootbecome_ask_pass= true
step 1: Connect to the managed node
to check the connectivity of the managed node run the following command
ansible all -m ping
Step 2: create two variable files named OS_name.yml
Here we are working on Ubuntu and RedHat so our file names will be Ubuntu.yml and RedHat.yml But How we know what distribution running on nodes??
we have a concept of ansible Facts using this we can find out what Os is running inside my node
to gather such information we have the following command
ansible "node name" -m setup
so you can see in the following image we able to find out OS name using this command
Now, we got our os names so we will add some variables in that files so we can use them in further process
Ubuntu.ymlpackage: apache2web_port: 80RedHat.ymlpackage: httpdweb_port: 80
step 3: install an apache web server on managed nodes and start service
so our variable files are ready but if you observe one thing in variable files the package name of the apache web server that we are going to install is different in both OS that is the main reason we have to run OS-specific commands otherwise it will cause an error.
so how can we tell the ansible that it is Ubuntu so use Ubuntu.yml? Is your variable file or it is RedHat so use RedHat.yml as your variable file? somehow we can tell this on run time but it is not proper way if you have 100 nodes how can we tell 100times.
Here we will get help from ansible facts in the above process we have used facts to gather os name we will use this os name in our final playbook so ansible thinks he including one variable file but behind the scenes, he is including os specific files at run time so we don’t need to specify ansible again and ultimately ansible will get os specific variables or package names.
following is the final playbook that includes specific os files and installs apache webserver and will start servers.
- hosts: allvars_files: - "{{ansible_facts['distribution']}}.yml"tasks: - name: install apache webserver package: name: "{{ package }}" state: latest - name: Start Web server service service: name: "{{ package }}" state: started
look closely at file how we are including files using ansible facts
vars_files:- "{{ansible_facts['distribution']}}.yml"// we have taken os name as variable filename and as we already created os specific files so it will dynamically include only that file ultimatly ansible will get os specific package names
Now execute the final playbook
To run Playbook use the following command
ansible-playbook FileName.yml
HURRAY, Finally we have successfully implemented our use case
if you see that it actually included variables files according to OS name
GitHub Link:-
https://github.com/venkateshpensalwar/ARTH/tree/main/Ansible/Dynamic_files
Conclusion:
we have learned how to load variable files according to OS distribution dynamically in the ansible-playbook using the ansible facts concept.
using such a concept we have solved the issue of different names of the same software in different OS without using when keyword concept. now if we have a lot of different OS we can easily install packages or configure something according to OS specifications.
Hope this blog is helpful to you!!!!
No comments