summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-04-16 11:01:17 +0300
committerLars Wirzenius <liw@liw.fi>2022-04-16 11:01:17 +0300
commitac373eb8ceadfd615497c73cdeccf43bd4d8e312 (patch)
treeb768660051d237e220daa1cc5e79ffff229c8bb4
parent90d7c4f98e7a847b6491cbffd772274e2f3a267d (diff)
downloadvmdb2-ac373eb8ceadfd615497c73cdeccf43bd4d8e312.tar.gz
fix(ansible plugin): delete all temporary files
Remember the temporary files (inventory, variables) for the Ansible step, for every step using it, and remove all of them. Previously, when there were more than one "ansible" step, the Ansible plugin would only remember the temporary files for the last step. This meant that only those got removed at the end. Also, it would try to remove them for every "ansible" step, resulting in harmless, but worrying error messages. Sponsored-by: author
-rw-r--r--vmdb/plugins/ansible_plugin.py38
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()