Dynamically Load Variable According to OS Type In Ansible

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
No alt text provided for this image
  • 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".
No alt text provided for this image
  • For this I created a playbook "test.yml" -
No alt text provided for this image
  • inventory file is "hosts" - ( "13.126.87.252" is Ubuntu and "13.232.235.43" is RedHat , Both system are running on AWS)
No alt text provided for this image
  • 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,)
No alt text provided for this image
  • Run "test.yml" playbook -
  # ansible-playbook test.yml

No alt text provided for this image
  • 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

要查看或添加评论,请登录

Govind Bhardwaj的更多文章

社区洞察

其他会员也浏览了