Add multistack support to tripleo-ansible-inventory

If tripleo-ansible-inventory is called with '--stack foo,bar',
then build an inventory for stack foo and stack bar; then
merge the two inventories into one before writing it out to
an inventory file.

An ansible inventory host group will be created for every
item in the cross product of stacks and roles. For example,
central_Controller, edge0_Compute, edge1_Compute, etc.

Change-Id: I23fdeaa4950244bf3aec8e8ea71e8a29f763c074
Depends-On: I522f1acbd39ad36ae1fec896188d3e20e0575566
Implements: blueprint split-control-plane-unified-inventory
This commit is contained in:
John Fulton 2019-06-07 21:10:26 +00:00
parent a4ac99c269
commit 35f3d60dcf
1 changed files with 35 additions and 22 deletions

View File

@ -28,8 +28,8 @@ import traceback
from oslo_config import cfg
from six.moves import configparser
from tripleo_common import inventory as inv
from tripleo_common import inventories as invs
from tripleo_validations import utils
@ -46,12 +46,14 @@ opts = [
cfg.StrOpt('project-name', default=os.environ.get(
'OS_PROJECT_NAME', os.environ.get('OS_TENANT_NAME'))),
cfg.StrOpt('cacert', default=os.environ.get('OS_CACERT')),
cfg.StrOpt('plan', default=os.environ.get('TRIPLEO_PLAN_NAME'),
help=('stack name to use for generating the '
'inventory data.')),
cfg.StrOpt('stack', help=('This arg has the same effect '
'as --plan. If both are specified, --stack '
'will take precedence.')),
cfg.ListOpt('plan', default=os.environ.get('TRIPLEO_PLAN_NAME'),
help=('stack name(s) to use for generating the '
'inventory data. If a comma delimited list '
'of stacks is passed, the inventory will '
'contain the union of those stacks.')),
cfg.ListOpt('stack', help=('This arg has the same effect '
'as --plan. If both are specified,'
' --stack will take precedence.')),
cfg.StrOpt('ansible_ssh_user', default=os.environ.get('ANSIBLE_SSH_USER',
'heat-admin')),
cfg.StrOpt('undercloud-connection',
@ -132,24 +134,35 @@ def main():
'timeout': 30
}
inventory = inv.TripleoInventory(
session=utils.get_auth_session(auth_variables),
hclient=utils.get_heat_client(auth_variables),
auth_url=configs.auth_url,
cacert=configs.cacert,
project_name=configs.project_name,
username=configs.username,
ansible_ssh_user=configs.ansible_ssh_user,
plan_name=configs.stack or configs.plan,
ansible_python_interpreter=configs.ansible_python_interpreter,
undercloud_connection=configs.undercloud_connection,
undercloud_key_file=configs.undercloud_key_file,
host_network=configs.ssh_network,
serial=configs.serial)
inventory_map = {}
for _plan in (configs.stack or configs.plan):
inventory_map[_plan] = inv.TripleoInventory(
session=utils.get_auth_session(auth_variables),
hclient=utils.get_heat_client(auth_variables),
auth_url=configs.auth_url,
cacert=configs.cacert,
project_name=configs.project_name,
username=configs.username,
ansible_ssh_user=configs.ansible_ssh_user,
plan_name=_plan,
ansible_python_interpreter=configs.ansible_python_interpreter,
undercloud_connection=configs.undercloud_connection,
undercloud_key_file=configs.undercloud_key_file,
host_network=configs.ssh_network,
serial=configs.serial)
inventory_keys = inventory_map.keys()
if len(inventory_keys) == 1:
inventory = inventory_map[inventory_keys[0]]
else:
inventory = invs.TripleoInventories(inventory_map)
inventory.merge()
if configs.list:
try:
inventory_list = inventory.list()
if len(inventory_keys) == 1:
inventory_list = inventory.list()
else:
inventory_list = inventory.inventory
print(json.dumps(inventory_list))
except Exception as e:
print("ERROR: Error creating inventory: {}".format(e),