Allow ansible_group_vars to be overriden by RoleGroupVars

We have introduced {{role.name}}ExtraGroupVars in THT; which allows an
operator to override any Ansible group vars.

In this patch, we will override the ansible_group_vars (from RoleData)
with the RoleGroupVars defined by THT.

It also updates the unit tests to cover 2 scenarios:

- chrony_host set to localhost in RoleData but overriden by 192.168.2.1
  in RoleGroupVars; in actual ansible group vars we end up with
  192.168.2.1.
- chrony_acl set to none in RoleData, never overriden; in actual ansible
  group vars we end up with none.
- chrone_foo isn't set in Role Data, but only by RoleGroupVars and it
  ends up being in actual ansible group vars.

Also, it removes the "if role_name not in role_group_vars" condition, as
the condition is never met. There is always a role_name key in
role_group_vars (e.g. max_fail_percentage, etc).

Change-Id: I2684174bcd9744f0ec0c1ec42cf11d2c467fc7ba
This commit is contained in:
Emilien Macchi 2020-04-02 23:00:33 -04:00
parent 7998c9553d
commit b3e803425d
3 changed files with 24 additions and 7 deletions

View File

@ -1,2 +1,5 @@
max_fail_percentage: 15
any_errors_fatal: True
chrony_host: 192.168.2.1
chrony_acl: none
chrony_foo: bar

View File

@ -317,10 +317,19 @@ class TestConfig(base.TestCase):
'uuid': 2},
'overcloud-novacompute-2': {
'uuid': 3}}}},
{'output_key': 'RoleData',
'output_value': {
'Controller': {
'ansible_group_vars': {
'chrony_host': 'localhost',
'chrony_acl': 'none',
}}}},
{'output_key': 'RoleGroupVars',
'output_value': {
'Controller': {
'any_errors_fatal': True,
'chrony_host': '192.168.2.1',
'chrony_foo': 'bar',
'max_fail_percentage': 15},
'Compute': {
'any_errors_fatal': True,
@ -722,10 +731,19 @@ class TestConfig(base.TestCase):
'a7db3010-a51f-4ae0-a791-2364d629d20d',
'8b07cd31-3083-4b88-a433-955f72039e2c',
'169b46f8-1965-4d90-a7de-f36fb4a830fe']}}},
{'output_key': 'RoleData',
'output_value': {
'Controller': {
'ansible_group_vars': {
'chrony_host': 'localhost',
'chrony_acl': 'none',
}}}},
{'output_key': 'RoleGroupVars',
'output_value': {
'Controller': {
'any_errors_fatal': True,
'chrony_host': '192.168.2.1',
'chrony_foo': 'bar',
'max_fail_percentage': 15},
'Compute': {
'any_errors_fatal': True,

View File

@ -246,13 +246,9 @@ class Config(object):
with self._open_file(filepath) as param_config:
param_config.write(json.dumps(role[config]))
elif config == 'ansible_group_vars':
# NOTE(aschultz): ansible group vars are for specific role
# services so we merge them in with the others so they
# end up in the role vars. This means the last var
# definition wins and will override them all.
if role_name not in role_group_vars:
role_group_vars[role_name] = {}
role_group_vars[role_name].update(role[config])
role_config = role[config].copy()
role_config.update(role_group_vars[role_name])
role_group_vars[role_name] = role_config
else:
# NOTE(emilien): Move this condition to the
# upper level once THT is adapted for all tasks to be