Variables or Vars are used to store the data which is further used by ansible playbook. These variables can either take input from user or it can also be stored in YAML or INI file as well like main.yml or inventory. Variables stored in either playbook or in separate YAML or INI format file can be read and invoked by ansible playbook anytime during task execution these variables can be used to control the behavior of playbook jobs and provide data during endpoint configuration as well. In Ansible variables have precedence rule which help Ansible playbook to look for data accordingly. Below is the list of ansible variables location which have least to greatest precedence:
- command line values (for example, -u my_user, these are not variables)
- role defaults (defined in role/defaults/main.yml)
- inventory file or script group vars
- inventory group_vars/all
- playbook group_vars/all
- inventory group_vars/*
- playbook group_vars/*
- inventory file or script host vars
- inventory host_vars/*
- playbook host_vars/*
- host facts / cached set_facts
- play vars
- play vars_prompt
- play vars_files
- role vars (defined in role/vars/main.yml)
- block vars (only for tasks in block)
- task vars (only for the task)
- set_facts / registered vars
- role (and include_role) params
- include params
- extra vars (for example, -e “user=my_user”)(always win precedence)
Host Variables can be created directly in the inventory file in INI format or for managing host variables in an efficient way the Host variables can be created in separate YAML file with the same endpoints hostname file name which can be stored in host_vars folder in an Ansible project. As you can see in an example below NTP server variables are defined as per webservers region by using two methods:
- NTP server variables are defined in a host inventory. You can see I have defined each region based NTP servers name in inventory file in INI format.
- Below is another example of host variables which is recommended way for managing host variables efficiently here, you can see I have created separate YAML files with the name of webservers hostname under host_vars folder which contain each region NTP servers detail.
Below is the example of NTP sync ansible playbook code in which you can see I am invoking the host variables:
Using host variables in such condition where host has distinct variables is a good practice but in real-world scenario as per above example if count of endpoints is high and are scattered region wise then creating and managing the host variables will be difficult hence for addressing this situation Group Variables comes in the picture.
Group Variables (group_vars) read variables from YAML file which is stored under group_vars folder like host_vars folder group_vars folder also need to be created inside the Ansible Project. Group Variables read variables which are associated with endpoints group. The difference between host variables and group variables is that host variable is associated with a specific host or endpoint whereas group variables are associated with endpoints group. Group variables YAML file name should be matched with Group name which is created in inventory file.
A real-world scenario is to use group variables for endpoints which are scattered as per geographical, or region wise each region endpoints geographically or regionally grouped, and the variables are defined for those groups under group_var. As referring to previous example below webservers are grouped as per region wise their variables are also defined and stored in different region name based folder under group_vars for reading their respective region NTP server information:
Below is the example of NTP sync ansible playbook code in which you can see I am invoking the group variables. In this example, playbook will read the data from Group variables and sync the webservers time as per their respective region NTP servers:
Below is an example of tree structure of host and group variable files and also you can see the other YAML files as well like inventory and playbooks.
Host variables are only good in such conditions if there are distinct or specific variables for each host or endpoint but in case if you want to use variables on more then one endpoint which are also scattered across regions or globe then using group variables is recommended. Also, since ansible use precedence rule for its variables so using group_vars or host_vars is a best practice, but in case if you have defined or provided the same variables in exta_vars or extra variables during playbook execution then Ansible will only give preference to extra variables as per precedence rule.