Dynamically Load Variable According to OS Type In Ansible
Hello Guys ,
In this article we will load a variable file in Ansible according to Operating System Type ( Means If we are configuring Ubuntu then playbook will load Ubuntu respectively file or same like RedHat or other Systems )
ARTH - Task 14.3 ???????
Task Description?? -
- ?? 14.3 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. )
Task Steps -
- For this practical I have a Virtual Machine on the top of VirtualBox . RedHat 8 is installed at this Virtual machine . In my case I have already installed Ansible .
# ansible --version
- We know that in RedHat to install apache http server package name is "httpd" and in Ubuntu "apache2" . I created two files "RedHat.yml" and "Ubuntu.yml" . In these file I set variable "apache_pkg" .
- Now we want that if target system is Ubuntu then ansible task dynamically include this variable "Ubuntu.yml" and print "apache_pkg" value "apache2" and if target system is RedHat then ansible task dynamically include this variable "RedHat.yml" and print "apache_pkg" value "httpd".
- For this I created a playbook "test.yml" -
- inventory file is "hosts" - ( "13.126.87.252" is Ubuntu and "13.232.235.43" is RedHat , Both system are running on AWS)
- Ansible configuration file - ( I have attached "hello" key to both AWS EC2 instances , "hello.pem" file also present on current directory "/task14.3" on my local system,)
- Run "test.yml" playbook -
# ansible-playbook test.yml
- We know First play if for Ubuntu system "13.126.87.252" so it is loading "Ubuntu.yml" so variable "apache_pkg" value is "apache2" and Second play is for RedHat system "13.232.235.43" , so it is loading "RedHat.yml" so variable "apache_pkg" value is "httpd" .
Now Our Task is successfully done