What are host_vars and group_vars in Ansible?
Variables or VARS are used to input data or a variable into a place in a playbook instead of manually entering the value in multiple places.
The main difference between Ansible host_vars and group_vars is that hosts_vars represent a single host in the Ansible inventory and group_vars represent a group of hosts defined under groups.
Host_Vars and Group_Vars (group variables and host variables) allow you to associate variables to specific devices or groups of devices.
This video should explain it pretty well.
host_vars is a folder that you create and within the folder are YAML files which reference each specific device
group_vars is also a folder you create and within the folder are YAML files which reference groups of devices or all devices.
Both the Ansible host_vars directory and the group_vars directory have to be created manually and are not created by default.
The names of the YAML files in group_vars must match the group defined in the inventory and also the YAML files in host_vars must be named exactly as the hosts in the inventory.
You can also input variables or vars directly into an Ansible playbook.
You create a place holder and then reference the variable name which is defined elsewhere. The benefit is you can then change the value of the variable and it will update in all the places where the variable is defined in the playbook.
When using variables in Ansible there is a precedence where Ansible looks for the data
The full list is below
- 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)
How to use host_vars in Ansible
Ansible group_vars example
Ansible host_vars example
How do you define host vars in Ansible?
Host Vars in Ansible are defined in a YAML file with the same filename as the host the variables are referencing, the file is saved into a folder which must be called host_vars
Example below shows a device called N9Kv1 and it’s associated host vars file called N9Kv1.yml and stored in the host_vars folder
Ansible inventory vars
You can define an alias in the inventory using host variables
jumphost ansible_port=1234 ansible_host=188.8.131.52
Frequently asked questions
How do I make an Ansible host group?
Within your Ansible inventory you create a host group by defining the name of the group within square brackets and then, note: hyphens are deprecated in group names. Then underneath the group name you list all the hosts e.g.
What are host vars in Ansible
Host vars in Ansible are variables that are specific to a host only and are defined in a YAML file which must be named exactly as the host in the inventory file.