cancel
Showing results for 
Search instead for 
Did you mean: 
spurs
Flight Engineer
Flight Engineer
  • 3,115 Views

group_vars usage

Jump to solution

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

Labels (1)
3 Solutions

Accepted Solutions
anthonywongibm
Mission Specialist
Mission Specialist
  • 2,976 Views

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.

View solution in original post

0 Kudos
Ricardo
Flight Engineer Flight Engineer
Flight Engineer
  • 2,967 Views

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:

  1. Creating a file with the group/host name with your variables inside it.
  2. Creating a folder with the group/host name and dropping the files with the variables inside it. All files will be used

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

[1] https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#variable-precedence-...

View solution in original post

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 2,902 Views

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.

 

View solution in original post

5 Replies
anthonywongibm
Mission Specialist
Mission Specialist
  • 2,977 Views

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.

0 Kudos
spurs
Flight Engineer
Flight Engineer
  • 2,951 Views

Thanks anthony!

0 Kudos
Ricardo
Flight Engineer Flight Engineer
Flight Engineer
  • 2,968 Views

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:

  1. Creating a file with the group/host name with your variables inside it.
  2. Creating a folder with the group/host name and dropping the files with the variables inside it. All files will be used

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

[1] https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#variable-precedence-...

0 Kudos
spurs
Flight Engineer
Flight Engineer
  • 2,951 Views

Thanks a lot, Ricardo, The other link about precedence was very helpful as well.

Cheers

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 2,903 Views

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.

 

Join the discussion
You must log in to join this conversation.