Allow passing extra vars when generating static inventory

This is needed because the undercloud deploy command uses the
"undercloud" stack name, which overwrites the hard-coded
undercloud inventory content, so we lose ansible_connection
settings to force a local connection.

Change-Id: Id9dfb8e387cf2be09a7bb689ba79f797e998002c
This commit is contained in:
Steven Hardy
2017-12-23 11:08:41 +00:00
committed by Emilien Macchi
parent 513620b115
commit dc2101ba72
2 changed files with 17 additions and 2 deletions

View File

@@ -265,7 +265,7 @@ class TripleoInventory(object):
# http://docs.ansible.com/ansible/developing_inventory.html
return {}
def write_static_inventory(self, inventory_file_path):
def write_static_inventory(self, inventory_file_path, extra_vars=None):
"""Convert inventory list to static yaml format in a file."""
allowed_extensions = ('.yaml', '.yml', '.json')
if not os.path.splitext(inventory_file_path)[1] in allowed_extensions:
@@ -278,5 +278,10 @@ class TripleoInventory(object):
self.hosts_format_dict = True
inventory = self.list()
if extra_vars:
for var, value in extra_vars.items():
if var in inventory:
inventory[var]['vars'].update(value)
with open(inventory_file_path, 'w') as inventory_file:
yaml.dump(inventory, inventory_file, TemplateDumper)

View File

@@ -333,9 +333,16 @@ class TestInventory(base.TestCase):
self.assertEqual(expected[k], inv_list[k])
def test_inventory_write_static(self):
self._inventory_write_static()
def test_inventory_write_static_extra_vars(self):
extra_vars = {'undercloud': {'anextravar': 123}}
self._inventory_write_static(extra_vars=extra_vars)
def _inventory_write_static(self, extra_vars=None):
tmp_dir = self.useFixture(fixtures.TempDir()).path
inv_path = os.path.join(tmp_dir, "inventory.yaml")
self.inventory.write_static_inventory(inv_path)
self.inventory.write_static_inventory(inv_path, extra_vars)
expected = {
'Compute': {'children': {'cp-0': {}},
'vars': {'ansible_ssh_user': 'heat-admin',
@@ -405,6 +412,9 @@ class TestInventory(base.TestCase):
'openstack-mistral-engine'],
'undercloud_swift_url': 'anendpoint',
'username': 'admin'}}}
if extra_vars:
expected['undercloud']['vars']['anextravar'] = 123
with open(inv_path, 'r') as f:
loaded_inv = yaml.safe_load(f)
self.assertEqual(expected, loaded_inv)