Dynamically Load the Variable file named same as OS Name and load the variables in that file in Ansible

Juzer Patanwala
3 min readMar 30, 2021

--

In this blog,we will create an Ansible Playbook which will dynamically load the variable file named same as OS name and by using the variable names we will configure webserver in our target node.

Pre-requiste

For doing this practical,you need to have Ansible installed and configured in the Ansible Controller Node.You can refer my earlier blog for installing and configuring Ansible from here.

Hope you have Ansible installed and configured.Now we can start off with our hands-on part.

Check the OS distribution and distribution version of the target node

For this,we can use the ansible facts.Ansible retrieves information about the target node and stores in a variable called ansible_facts.

debug: 
var: "{{ansible_facts['distribution']}}"
debug:
var: "{{ansible_facts['distribution_major_version']}}"

This will give us the OS distribution name and it’s version.

Include the file named same as OS Name in the Playbook

We will use vars_files keyword for including the file here.

vars_files:
- "vars/{{ansible_facts['distribution']}}-{{ansible_facts['distribution_major_version']}}.yml"

The target node in my case is working on RedHat-8,so I have created the variable file named RedHat-8.yml.You can create the file based on the OS name and version of your target node.

We can add the variable names in RedHat-8.yml as follows :

pkg_name: httpd
file: rhelindex.html.j2

The file rhelindex.html.j2 will be copied to the target node and looks something like this

This is { {ansible_facts['distribution'] }} OS

Install the apache webserver software

We will use the package module for installing the apache software

- name: Install apache webserver software
package:
name: "{{ pkg_name }}"
state: present

Copy the webpage file

Since,we have used the jinja template in the file rhelindex.html.j2 we will use the template module for copying the file here

- name: Copy the webpage
template:
src: "{{ file }}"
dest: /var/www/html/index.html

Start the apache webserver services

We will use the service module for this

- name: Start apache webserver services
service:
name: "{{ pkg_name }}"
state: started

This is the entire playbook for your reference

- hosts: task14
gather_facts: yes
vars_files:
- "vars/{{ansible_facts['distribution']}}-{{ansible_facts['distribution_major_version']}}.yml"
tasks:
- name: Install apache webserver software
package:
name: "{{ pkg_name }}"
state: present
- name: Copy the webpage
template:
src: "{{ file }}"
dest: /var/www/html/index.html
mode: 0777
- name: Start apache webserver services
service:
name: "{{ pkg_name }}"
state: started
- name: Start apache webserver services
service:
name: "{{ pkg_name }}"
state: started

Run the playbook

The webserver is successfully launched

Hence,the webserver is finally launched.

Thank You!!!!

--

--

Juzer Patanwala

Cloud Engineer @ Searce Inc || Technical Content Writer || Technology and Automation Enthusiast