Ansible Playbook: Include_vars

Abhishek Arora
2 min readJan 28, 2021

This task is about include_vars which is a way to use the variables in ansible playbook without the need to mention them in the playbook itself.

Now one may think that we need not to mention the variables in playbook but still we gonna mention variables somewhere, so what's the use of include_vars.

include_vars is used to fetch the variables from a specific file mentioned somewhere within the system in order to take variables and their value.
The purpose we solved in this task is that we need to mention the variables values but they need to be different for different OS, For instance say we have httpd service with name apache2 in ubuntu based OS and httpd in RedHat OS.

So we can use the same variable with different values by making the file with the name of OS name and it will automatically fetch the variables from file having the name of OS family.

Now we will begin the implementation part

  • Make ansible playbook YAML file with include_vars
  • Make files having variable values with name of os_family
  • Make ansible playbook to install software with package variable name

Voila!!! Task is done and the playbook will fetch the variable values from the files having name of OS

The complete video of demonstration is below

--

--