Improve the way of handling inventory for ansible_runner

In TripleO we have different ways to provide inventory for ansible
depending on the features:
update/upgrade gives a yaml dump, validation and undercloud
gives a str with the hostnames, the standalone gives a file.

This small private function aims to handle all that case in
order to provide the same way to ansible runner: an ansible
artifact written under artifacts temp directory

Change-Id: I454aa483aa069806ba6b149997804df8a507a4d0
This commit is contained in:
Mathieu Bultel
2020-02-17 00:13:10 +01:00
committed by mathieu bultel
parent 1ba4c660e6
commit 77db88b7b3

View File

@@ -306,6 +306,23 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
LOG.debug('Ansible playbook {} found'.format(play))
return play
def _inventory(inventory):
if inventory:
if isinstance(inventory, six.string_types):
# check is file path
if os.path.exists(inventory):
return inventory
elif isinstance(inventory, dict):
inventory = yaml.safe_dump(
inventory,
default_flow_style=False
)
return ansible_runner.utils.dump_artifact(
inventory,
ansible_artifact_path,
'hosts'
)
if not playbook_dir:
playbook_dir = workdir
@@ -516,18 +533,11 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
env['ANSIBLE_CONFIG'] = ansible_cfg
elif 'ANSIBLE_CONFIG' not in env and ansible_cfg:
env['ANSIBLE_CONFIG'] = ansible_cfg
inventory_file = None
if inventory and not os.path.exists(inventory):
inventory_file = ansible_runner.utils.dump_artifact(
inventory,
ansible_artifact_path,
'hosts'
)
r_opts = {
'private_data_dir': workdir,
'project_dir': playbook_dir,
'inventory': (inventory_file if inventory_file else inventory),
'inventory': _inventory(inventory),
'envvars': _encode_envvars(env=env),
'playbook': playbook,
'verbosity': verbosity,