mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
allow users to control group var merge order (#22580)
* allow users to control group var merge order With ansible_group_priority var at each group will determine merge order with siblings, falling back to sorted names when priority is equal as it did before. Parent/children relationships still work as they did and have higher precedence than priority * move priority setting to groups, sourc indep * now both inventory and play group follow priority * removed double exception handling
This commit is contained in:
parent
5334814396
commit
4319ddd5de
5 changed files with 9 additions and 11 deletions
|
@ -124,7 +124,10 @@ class Group:
|
||||||
|
|
||||||
def set_variable(self, key, value):
|
def set_variable(self, key, value):
|
||||||
|
|
||||||
self.vars[key] = value
|
if key == 'ansible_group_priority':
|
||||||
|
self.set_priority(int(value))
|
||||||
|
else:
|
||||||
|
self.vars[key] = value
|
||||||
|
|
||||||
def clear_hosts_cache(self):
|
def clear_hosts_cache(self):
|
||||||
|
|
||||||
|
|
|
@ -155,6 +155,6 @@ class Host:
|
||||||
def get_group_vars(self):
|
def get_group_vars(self):
|
||||||
results = {}
|
results = {}
|
||||||
groups = self.get_groups()
|
groups = self.get_groups()
|
||||||
for group in sorted(groups, key=lambda g: (g.depth, g.name)):
|
for group in sorted(groups, key=lambda g: (g.depth, g.priority, g.name)):
|
||||||
results = combine_vars(results, group.get_vars())
|
results = combine_vars(results, group.get_vars())
|
||||||
return results
|
return results
|
||||||
|
|
|
@ -160,10 +160,7 @@ class InventoryParser(object):
|
||||||
# applied to the current group.
|
# applied to the current group.
|
||||||
elif state == 'vars':
|
elif state == 'vars':
|
||||||
(k, v) = self._parse_variable_definition(line)
|
(k, v) = self._parse_variable_definition(line)
|
||||||
if k != 'ansible_group_priority':
|
self.groups[groupname].set_variable(k, v)
|
||||||
self.groups[groupname].set_variable(k, v)
|
|
||||||
else:
|
|
||||||
self.groups[groupname].set_priority(v)
|
|
||||||
|
|
||||||
# [groupname:children] contains subgroup names that must be
|
# [groupname:children] contains subgroup names that must be
|
||||||
# added as children of the current group. The subgroup names
|
# added as children of the current group. The subgroup names
|
||||||
|
|
|
@ -85,10 +85,7 @@ class InventoryParser(object):
|
||||||
|
|
||||||
if 'vars' in group_data:
|
if 'vars' in group_data:
|
||||||
for var in group_data['vars']:
|
for var in group_data['vars']:
|
||||||
if var != 'ansible_group_priority':
|
self.groups[group].set_variable(var, group_data['vars'][var])
|
||||||
self.groups[group].set_variable(var, group_data['vars'][var])
|
|
||||||
else:
|
|
||||||
self.groups[group].set_priority(group_data['vars'][var])
|
|
||||||
|
|
||||||
if 'children' in group_data:
|
if 'children' in group_data:
|
||||||
for subgroup in group_data['children']:
|
for subgroup in group_data['children']:
|
||||||
|
|
|
@ -252,6 +252,7 @@ class VariableManager:
|
||||||
# first we merge in vars from groups specified in the inventory (INI or script)
|
# first we merge in vars from groups specified in the inventory (INI or script)
|
||||||
all_vars = combine_vars(all_vars, host.get_group_vars())
|
all_vars = combine_vars(all_vars, host.get_group_vars())
|
||||||
|
|
||||||
|
# these are PLAY host/group vars, inventory adjacent ones have already been processed
|
||||||
# next, we load any vars from group_vars files and then any vars from host_vars
|
# next, we load any vars from group_vars files and then any vars from host_vars
|
||||||
# files which may apply to this host or the groups it belongs to. We merge in the
|
# files which may apply to this host or the groups it belongs to. We merge in the
|
||||||
# special 'all' group_vars first, if they exist
|
# special 'all' group_vars first, if they exist
|
||||||
|
@ -260,7 +261,7 @@ class VariableManager:
|
||||||
for item in data:
|
for item in data:
|
||||||
all_vars = combine_vars(all_vars, item)
|
all_vars = combine_vars(all_vars, item)
|
||||||
|
|
||||||
for group in sorted(host.get_groups(), key=lambda g: (g.depth, g.name)):
|
for group in sorted(host.get_groups(), key=lambda g: (g.depth, g.priority, g.name)):
|
||||||
if group.name in self._group_vars_files and group.name != 'all':
|
if group.name in self._group_vars_files and group.name != 'all':
|
||||||
for data in self._group_vars_files[group.name]:
|
for data in self._group_vars_files[group.name]:
|
||||||
data = preprocess_vars(data)
|
data = preprocess_vars(data)
|
||||||
|
|
Loading…
Reference in a new issue