Dynamic Ansible-Playbook: Without Keywords

Dynamic Ansible-Playbook: Without Keywords

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:

Screenshot 2021-03-28 031237.jpeg

ubuntu.yml:

Screenshot 2021-03-28 031344.jpeg

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:

oeaZkpLtS.jpeg

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

FgF9fOeBG.jpeg

httpd in installed in our operating system so your task is completed.