Hello,
I was doing lab and saw the question that moving the variable files to the group_vars after creating the directory.
what does group_vars exactly do and when should I use?
+actually want to know more about host_vars as well
That groups all variable to same directory, so that you no need to handle the variables in seperate yml files. Easiler to handle the variables.
Hi @spurs ,
The folders group_vars and host_vars are a way to organize the variables in a single place. As the name suggests, group_vars are for groups, and host_vars for hosts.
There are two ways to use them:
The files must be on YAML format and can have ".yml", ".yaml", ".json" extensions, or no extension at all.
Because there are several places we can define a variable, is important to observe the reading precedence that ansible utilizes. You can find more info here [1].
I hope this clarifies better.
Cheers
Ricardo
If you're trying to sort through the different group_vars and host_vars directories in the precedence list that @Ricardo posted:
"inventory group_vars" is the group_vars subdirectory in the same directory as your inventory file
"playbook group_vars" is the group_vars subdirectory in the same directory as your playbook file
And the same relationship is true for "inventory host_vars" and "playbook host_vars".
If your inventory file and your playbook are in the same directory, as it is with many of our exercises, obviously the variables in the host_vars and group_vars subdirectories are set as both inventory host_vars and playbook host_vars.
If you look at the precedence chart, this usually doesn't matter much because, for example, inventory host_vars are immediately below playbook host_vars in precedence. It only matters if your inventory file and playbook file are in different directories and the group_vars and host_vars files have different settings in the different directories -- in that case, the group_vars and host_vars in the playbook's directory wins with one step higher precedence.
Host or group based variables actually set directly inside an inventory file have lower priority than any host_vars or group_vars settings.
Variables set in the vars section of a play in the playbook ("play vars") have higher precedence than all of these host-based variables.
That groups all variable to same directory, so that you no need to handle the variables in seperate yml files. Easiler to handle the variables.
Thanks anthony!
Hi @spurs ,
The folders group_vars and host_vars are a way to organize the variables in a single place. As the name suggests, group_vars are for groups, and host_vars for hosts.
There are two ways to use them:
The files must be on YAML format and can have ".yml", ".yaml", ".json" extensions, or no extension at all.
Because there are several places we can define a variable, is important to observe the reading precedence that ansible utilizes. You can find more info here [1].
I hope this clarifies better.
Cheers
Ricardo
Thanks a lot, Ricardo, The other link about precedence was very helpful as well.
Cheers
If you're trying to sort through the different group_vars and host_vars directories in the precedence list that @Ricardo posted:
"inventory group_vars" is the group_vars subdirectory in the same directory as your inventory file
"playbook group_vars" is the group_vars subdirectory in the same directory as your playbook file
And the same relationship is true for "inventory host_vars" and "playbook host_vars".
If your inventory file and your playbook are in the same directory, as it is with many of our exercises, obviously the variables in the host_vars and group_vars subdirectories are set as both inventory host_vars and playbook host_vars.
If you look at the precedence chart, this usually doesn't matter much because, for example, inventory host_vars are immediately below playbook host_vars in precedence. It only matters if your inventory file and playbook file are in different directories and the group_vars and host_vars files have different settings in the different directories -- in that case, the group_vars and host_vars in the playbook's directory wins with one step higher precedence.
Host or group based variables actually set directly inside an inventory file have lower priority than any host_vars or group_vars settings.
Variables set in the vars section of a play in the playbook ("play vars") have higher precedence than all of these host-based variables.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.