diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index d7562389c9..35ec8513aa 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -871,6 +871,11 @@ def _parse_global_variables(user_cidr, inventory, user_defined_config): user_defined_config['global_overrides'] ) + # Remove global overrides that were deleted from inventory, too + for key in inventory['all']['vars'].keys(): + if key not in user_defined_config['global_overrides']: + del inventory['all']['vars'][key] + def append_if(array, item): """Append an ``item`` to an ``array`` if its not already in it. diff --git a/releasenotes/notes/remove-overrides-17ef7d0496f6a6c7.yaml b/releasenotes/notes/remove-overrides-17ef7d0496f6a6c7.yaml new file mode 100644 index 0000000000..bfd65a1036 --- /dev/null +++ b/releasenotes/notes/remove-overrides-17ef7d0496f6a6c7.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Deleting variable entries from the ``global_overrides`` dictionary in + ``openstack_user_config.yml`` now properly removes those variables from + the ``openstack_inventory.json`` file. See Bug #1588902. diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 1beb70721c..d7d3adfd5e 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -382,6 +382,12 @@ class TestConfigChecks(unittest.TestCase): with open(USER_CONFIG_FILE, 'wb') as f: f.write(yaml.dump(self.user_defined_config)) + def restore_config(self): + # get back our initial user config file + os.remove(USER_CONFIG_FILE) + os.rename(USER_CONFIG_FILE + ".tmp", USER_CONFIG_FILE) + self.config_changed = False + def test_missing_container_cidr_network(self): self.delete_provider_network('container') with self.assertRaises(SystemExit) as context: @@ -519,9 +525,7 @@ class TestConfigChecks(unittest.TestCase): def tearDown(self): if self.config_changed: - # get back our initial user config file - os.remove(USER_CONFIG_FILE) - os.rename(USER_CONFIG_FILE + ".tmp", USER_CONFIG_FILE) + self.restore_config() class TestStaticRouteConfig(TestConfigChecks): @@ -643,6 +647,45 @@ class TestNetAddressSearch(unittest.TestCase): self.assertEqual(pns, new_pns) +class TestGlobalOverridesConfigDeletion(TestConfigChecks): + def setUp(self): + super(TestGlobalOverridesConfigDeletion, self).setUp() + self.inventory = get_inventory() + + def add_global_override(self, var_name, var_value): + """Adds an arbitrary name and value to the global_overrides dict.""" + overrides = self.user_defined_config['global_overrides'] + overrides[var_name] = var_value + + def remove_global_override(self, var_name): + """Removes target key from the global_overrides dict.""" + overrides = self.user_defined_config['global_overrides'] + del overrides[var_name] + + def test_global_overrides_delete_when_merge(self): + """Vars removed from global overrides are removed from inventory""" + self.add_global_override('foo', 'bar') + + di._parse_global_variables({}, self.inventory, + self.user_defined_config) + + self.remove_global_override('foo') + + di._parse_global_variables({}, self.inventory, + self.user_defined_config) + + self.assertNotIn('foo', self.inventory['all']['vars'], + "foo var not removed from group_vars_all") + + def test_global_overrides_merge(self): + self.add_global_override('foo', 'bar') + + di._parse_global_variables({}, self.inventory, + self.user_defined_config) + + self.assertEqual('bar', self.inventory['all']['vars']['foo']) + + class TestMultipleRuns(unittest.TestCase): def test_creating_backup_file(self): inventory_file_path = os.path.join(TARGET_DIR,