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. )
Description: Configure Apache webserver in RedHat and Ubuntu OS using Ansible Playbook. For RedHat OS we have httpd as a package name whereas for Ubuntu OS we have apache2 package for apache webserver.
Launched two instances of Redhat and Ubuntu/Debian OS
We are going to use two variable from the ansible facts i.e distribution and distribution_major_version in the ansible playbook to load the variable file dynamically.
Create a variable file for Redhat and Ubuntu in a format like (<distribution>-<distribution_major_version>.yml)
(Note: It is case sensitive)
(RedHat-8.yml)
package_name: httpd
service_name: httpd
src_file: redhat.html
(Ubuntu-20.yml)
package_name: apache2
service_name: apache2
src_file: ubuntu.html
I created different html file for Ubuntu (ubuntu.html) and RedHat (redhat.html) OS
(ubuntu.html)
Ubuntu's Apache2
and
(redhat.html)
Redhat's httpd
This is the main ansible playbook
(task.yml)
- hosts: allvars_files:
- "{{ansible_facts['distribution']}}-{{ansible_facts['distribution_major_version']}}.yml"tasks:
- name: "Downloading httpd"
package:
name: "{{ package_name }}"
state: present- name: "starting httpd service"
service:
name: "{{ service_name }}"
state: started- name: "Coping html pages"
copy:
dest: "/var/www/html"
src: "{{src_file}}"
After running the PlayBook check the webserver configured or not