From 77db88b7b370b6bf862ad463cdce019c7e6ea3e4 Mon Sep 17 00:00:00 2001 From: Mathieu Bultel Date: Mon, 17 Feb 2020 00:13:10 +0100 Subject: [PATCH] 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 --- tripleoclient/utils.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tripleoclient/utils.py b/tripleoclient/utils.py index 44fabe8f4..03047018e 100644 --- a/tripleoclient/utils.py +++ b/tripleoclient/utils.py @@ -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,