Task- 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)
Usually to get this result we use when keyword in ansible but here we cannot use when keyword here therefore we have to create an Ansible Playbook which will dynamically load the variable file named same as OS_name.
Now we create separate *.yml files with the OS names:-
redhat.yml:
ubuntu.yml:
Now we import these files in our playbook:
- hosts: localhost
vars_files:
- "{{ansible_facts[ 'distribution' ] | lower}}.yml"
tasks:
- name: To install webserver
package:
name: "{{ h }}"
state: present
Now we run the playbook:
Now as Ansible-playbook has ran successfully and our host is a RedHat operating system so httpd must have been installed in the computer.
we can check by using command:
rpm -q httpd
httpd in installed in our operating system so your task is completed.