Load var files based on ansible facts

Yogesh
5 min readMar 26, 2021

After reading this blog, you’ll be able to create an Ansible Playbook which will dynamically load the variable file named based on ansible facts. But in this particular tutorial, I’ll be guiding you as to how to do it based on OS distribution i.e. load a particular var file based on the Linux distribution - RedHat or Ubuntu. I’ll do this entire thing on AWS cloud so that I can save time installing different VMs on my base OS. So, the whole purpose of this tutorial itself is to load var files directly based on the distribution name instead of setting up multiple if-else conditions. Instead of doing it for the sake of doing it, we’ll pick up a use-case of setting up a simple webserver on two types of Linux flavours - RedHat and Ubuntu on AWS cloud. Now, that we have set our goals let's proceed ahead with setting up the inventory first.

First things first, I hope you have a dynamic inventory set up if not you can quickly do it, it's pretty straight forward. You can check it out in my previous blog. If you want to avoid the trouble of setting up a dynamic inventory you can follow the static approach too, but it’s convenient to work with a dynamic inventory.

Inventory

🔹 As mentioned previously we’ll first launch two EC2 instances of RedHat and Ubuntu flavours. You can use the GUI console of AWS or the CLI one, but I prefer using CLI. But whatever method you use make sure to tag the instances uniquely. I have tagged them with OS: RedHat and OS: Ubuntu respectively. Also, ensure that you have port 80 opened in your security group since we’ll be setting the web server on that port.

Launch RedHat Instance

Launch Ubuntu Instance

Note: I have used backticks for multiline command here since I’m running this on Windows PowerShell.

🔹 Now, that the instances are up and running we need to group them under a single group name say webserver in the inventory. Before that, I would like to show you how my group vars are in the dynamic inventory.

🔹 So, the dynamic inventory basically groups all the RedHat instances into the tag_OS_RedHat group and all the Ubuntu instances into tag_OS_Ubuntu group and we provide the credentials for ansible to login to the instances via SSH in the group_vars directory. Note that I have used tagas a prefix in my dynamic inventory as shown in my previous blog. Now create another file named hosts in the dynamic inventory directory with the below setup where we group both the above groups of instances to webserver group.

[tag_OS_RedHat][tag_OS_Ubuntu][webserver:children]
tag_OS_Ubuntu
tag_OS_RedHat

Now, that we are done setting up the inventory let us move on to the playbook and var files which is the creamy part of this blog.

Playbook

First, let us list down the tasks that the playbook has to accomplish:

✔️ Load the appropriate variable file based on the OS distribution

✔️ Install Apache webserver

✔️ Copy webpages to the document root

✔️ Finally, start the webserver

Note: We won't be needing a firewall since SG will take care of it in AWS.

- hosts: webserver
vars:
- webpages_path: "./webpages"
- params:
files:
- '{{ ansible_distribution|lower }}.yaml'
- '{{ ansible_os_family|lower }}.yaml'
tasks:
- name: "Load a variable file based on the OS distribution"
include_vars: "{{ lookup('first_found', params) }}"
- name: "Install Apache webserver"
package:
name: "{{ package }}"
state: "present"
- name: "Transfer webpages"
copy:
src: "{{ item }}"
dest: "/var/www/html/"
with_fileglob: "{{ webpages_path }}/*"
- name: "Start web server"
service:
name: "{{ package }}"
state: "started"

🔹 The important part to notice in this playbook is how we define the variable files in params . We are directly using the Ansible facts to fetch this information on the distribution name and converting it to lower case. Then we use include_vars module to load vars from the files named on this distribution. We also use the lookup function to retrieve the full path of the files by going up the directories. You can check more about lookup here in the documentation. The rest all are the usual procedures of a webserver except that the package variable will actually be coming from the var files redhat.yml and ubuntu.yml . We then basically install the webserver, copy web pages into the document root and start the webserver.

Var Files

🔹 The only difference between setting up a webserver in RedHat and Ubuntu is the apache web server package name and the commands associated with it (also the path of the configuration file). But here Ansible abstracts the commands for us so we need not worry about the commands. So we only have to separate out the package name into the var files.

🔹 If you know a little bit about webservers the difference between the redhat and ubuntu flavour is the package name and configuration path. The configuration file itself remains the same.

redhat.yml

package: "httpd"

ubuntu.yml

package: "apache2"

Finally, if you run this playbook you can check the website with a GET request to the public URL as follows.

You can find the code for the playbook, var files and the webpages on my GitHub repo.

Thank You for reading!

I hope you learned something new and enjoyed it. 😇

--

--