Dynamically loading the Ansible Module Arguments based up on the OS distribution

B Manoj
Dec 16, 2020

Let me show you the code:

For details about variable separation, please see :

https://ansible-tips-and-tricks.readthedocs.io/en/latest/os-dependent-tasks/variables/

Before looking into code, you should also check this part :

Here, you can see distribution vars file for redhat-8 and ubuntu 18.

The names should be as it is

The execution result is :

Code is :

- hosts: httpd

name: identifying the flavor distributions and loading the files accordingly

vars_files:

- “{{ ansible_facts[‘distribution’] }}-{{ ansible_facts[‘distribution_major_version’] }}.yaml”

tasks:

- name: Install web server s/w

package:

name: “{{ package }}”

state: present

- name: Copy webpage

template:

dest: “{{ doc_root }}”

src: homepage.html

- name: Start the service

service:

name: “{{ package }}”

state: started

~

~

~

--

--