diff options
-rw-r--r-- | vmdb/plugins/ansible_plugin.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/vmdb/plugins/ansible_plugin.py b/vmdb/plugins/ansible_plugin.py index 564a321..ad438e3 100644 --- a/vmdb/plugins/ansible_plugin.py +++ b/vmdb/plugins/ansible_plugin.py @@ -49,14 +49,18 @@ class AnsibleStepRunner(vmdb.StepRunnerInterface): mount_point = state.tags.get_builder_mount_point(tag) rootfs_tarball = settings["rootfs-tarball"] - state.ansible_inventory = self.create_inventory(mount_point, group_name) - vmdb.progress( - "Created {} for Ansible inventory".format(state.ansible_inventory) - ) + inventory = self.create_inventory(mount_point, group_name) + if not hasattr(state, "ansible_inventory"): + state.ansible_inventory = [] + state.ansible_inventory.append(inventory) + vmdb.progress(f"Created {inventory} for Ansible inventory") - extra_vars['rootfs_tarball'] = rootfs_tarball - state.ansible_vars_file = self.create_vars_file(extra_vars) - vmdb.progress(f"Created {state.ansible_vars_file} for Ansible variables") + extra_vars["rootfs_tarball"] = rootfs_tarball + vars_file = self.create_vars_file(extra_vars) + if not hasattr(state, "ansible_vars_file"): + state.ansible_vars_file = [] + state.ansible_vars_file.append(vars_file) + vmdb.progress(f"Created {vars_file} for Ansible variables") env = dict(os.environ) env["ANSIBLE_NOCOWS"] = "1" @@ -65,19 +69,18 @@ class AnsibleStepRunner(vmdb.StepRunnerInterface): env["ANSIBLE_CONFIG"] = config_file vmdb.progress(f"Using Ansible config file {config_file}") else: - raise RuntimeError( - f"Ansible config file {config_file} does not exist") + raise RuntimeError(f"Ansible config file {config_file} does not exist") vmdb.runcmd( [ "ansible-playbook", "-c", "chroot", "-i", - state.ansible_inventory, + inventory, "--tags", ansible_tags, "-e", - f"@{state.ansible_vars_file}", + f"@{vars_file}", playbook, ], env=env, @@ -85,12 +88,15 @@ class AnsibleStepRunner(vmdb.StepRunnerInterface): def teardown(self, values, settings, state): if hasattr(state, "ansible_vars_file"): - vmdb.progress(f"Removing {state.ansible_vars_file}") - os.remove(state.ansible_vars_file) - + self.remove(state.ansible_vars_file) if hasattr(state, "ansible_inventory"): - vmdb.progress("Removing {}".format(state.ansible_inventory)) - os.remove(state.ansible_inventory) + self.remove(state.ansible_inventory) + + def remove(self, filenames): + for filename in filenames: + if os.path.exists(filename): + vmdb.progress("Removing {}".format(filename)) + os.remove(filename) def create_inventory(self, chroot, group_name): fd, filename = tempfile.mkstemp() |