From a6edd12b685d6dca1379215d8f1856f02ff09922 Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Thu, 31 Oct 2024 18:22:08 +0100 Subject: [PATCH 1/4] Add libvirt manifests cleanup tasks First draft to always get a clean manifests folder with all the effective .tf and .cfg files, and no residual manifests from previous runs. Fixes: #1 --- tasks/libvirt.yml | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tasks/libvirt.yml b/tasks/libvirt.yml index 2518f46..d99d3b6 100644 --- a/tasks/libvirt.yml +++ b/tasks/libvirt.yml @@ -56,6 +56,58 @@ loop_control: loop_var: tf_group_name +# Generate the files_to_keep list +- name: Create the list of .tf and .cfg files to keep + ansible.builtin.set_fact: + files_to_keep: [] + +- name: Add to files to keep list fixed resources files + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.tf' ] }}" + with_items: + - 'provider' + - 'networks' + - 'pools' + - 'volumes' + - 'cloud_init' + +- name: Add to files to keep list dynamic cloud_init.cfg files + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/cloud_init_' + item.name + '.cfg' ] }}" + with_items: + - "{{ tf_libvirt_cloud_inits }}" + +- name: Add to files to keep list dynamic .vms.tf files + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }}" + with_items: + - "{{ tf_libvirt_vms_groups }}" + +- name: Get the stats (including absolute paths) of the remote files + ansible.builtin.stat: + path: "{{ item }}" + loop: "{{ files_to_keep }}" + register: files_to_keep_stats + +- name: Create a list of files to keep with absolute paths + set_fact: + files_to_keep_full_path: "{{ files_to_keep_full_path | default([]) + [ item.stat.path ] }}" + loop: "{{ files_to_keep_stats.results }}" + +# Generate the list of all files +- name: Find all .tf and .cfg files in tf_config_dir + find: + paths: "{{ tf_config_dir }}" + patterns: "*.tf,*.cfg" + register: all_files + +- name: Delete files not to keep + file: + path: "{{ item.path }}" + state: absent + loop: "{{ all_files.files }}" + when: item.path not in files_to_keep_full_path + - name: Display a reminder for sourcing variables ansible.builtin.debug: msg: From fe2cb4dbb6a18c90b5e4165b24d18a757c555f6a Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Mon, 4 Nov 2024 12:13:48 +0100 Subject: [PATCH 2/4] Separate cleanup tasks in a dedicated file This commit creates the libvirt-cleanup.yml file that is included inside the main one. --- tasks/libvirt-cleanup.yml | 51 +++++++++++++++++++++++++++++++++++++ tasks/libvirt.yml | 53 ++------------------------------------- 2 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 tasks/libvirt-cleanup.yml diff --git a/tasks/libvirt-cleanup.yml b/tasks/libvirt-cleanup.yml new file mode 100644 index 0000000..96ce0e2 --- /dev/null +++ b/tasks/libvirt-cleanup.yml @@ -0,0 +1,51 @@ +--- + +- name: Generate list of .tf and .cfg files to keep + ansible.builtin.set_fact: + files_to_keep: [] + +- name: Add fixed resources files to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.tf' ] }}" + with_items: + - 'provider' + - 'networks' + - 'pools' + - 'volumes' + - 'cloud_init' + +- name: Add dynamic cloud_init.cfg files to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/cloud_init_' + item.name + '.cfg' ] }}" + with_items: + - "{{ tf_libvirt_cloud_inits }}" + +- name: Add dynamic .vms.tf files to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }}" + with_items: + - "{{ tf_libvirt_vms_groups }}" + +- name: Get the stats (including absolute paths) of the remote files + ansible.builtin.stat: + path: "{{ item }}" + loop: "{{ files_to_keep }}" + register: files_to_keep_stats + +- name: Create a list of files to keep with absolute paths + ansible.builtin.set_fact: + files_to_keep_full_path: "{{ files_to_keep_full_path | default([]) + [ item.stat.path ] }}" + loop: "{{ files_to_keep_stats.results }}" + +- name: Generate the list of all the .tf and .cfg files + ansible.builtin.find: + paths: "{{ tf_config_dir }}" + patterns: "*.tf,*.cfg" + register: all_files + +- name: Effectively delete files not to keep + ansible.builtin.file: + path: "{{ item.path }}" + state: absent + loop: "{{ all_files.files }}" + when: item.path not in files_to_keep_full_path diff --git a/tasks/libvirt.yml b/tasks/libvirt.yml index d99d3b6..f4dcdac 100644 --- a/tasks/libvirt.yml +++ b/tasks/libvirt.yml @@ -56,57 +56,8 @@ loop_control: loop_var: tf_group_name -# Generate the files_to_keep list -- name: Create the list of .tf and .cfg files to keep - ansible.builtin.set_fact: - files_to_keep: [] - -- name: Add to files to keep list fixed resources files - ansible.builtin.set_fact: - files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.tf' ] }}" - with_items: - - 'provider' - - 'networks' - - 'pools' - - 'volumes' - - 'cloud_init' - -- name: Add to files to keep list dynamic cloud_init.cfg files - ansible.builtin.set_fact: - files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/cloud_init_' + item.name + '.cfg' ] }}" - with_items: - - "{{ tf_libvirt_cloud_inits }}" - -- name: Add to files to keep list dynamic .vms.tf files - ansible.builtin.set_fact: - files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }}" - with_items: - - "{{ tf_libvirt_vms_groups }}" - -- name: Get the stats (including absolute paths) of the remote files - ansible.builtin.stat: - path: "{{ item }}" - loop: "{{ files_to_keep }}" - register: files_to_keep_stats - -- name: Create a list of files to keep with absolute paths - set_fact: - files_to_keep_full_path: "{{ files_to_keep_full_path | default([]) + [ item.stat.path ] }}" - loop: "{{ files_to_keep_stats.results }}" - -# Generate the list of all files -- name: Find all .tf and .cfg files in tf_config_dir - find: - paths: "{{ tf_config_dir }}" - patterns: "*.tf,*.cfg" - register: all_files - -- name: Delete files not to keep - file: - path: "{{ item.path }}" - state: absent - loop: "{{ all_files.files }}" - when: item.path not in files_to_keep_full_path +- name: Cleanup Libvirt manifests and configurations + ansible.builtin.include_tasks: libvirt-cleanup.yml - name: Display a reminder for sourcing variables ansible.builtin.debug: From ad604bb24f50172a7675d4e1fb6078bfff03a342 Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Mon, 4 Nov 2024 12:15:09 +0100 Subject: [PATCH 3/4] Add the cleanup note inside the Libvirt README This commit adds a note mentioning the cleanup in the Libvirt README. --- Libvirt.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Libvirt.md b/Libvirt.md index 4164685..9ccb30b 100644 --- a/Libvirt.md +++ b/Libvirt.md @@ -286,3 +286,6 @@ tf_libvirt_cloud_inits: but you can check the [cloud-init documentation](https://cloudinit.readthedocs.io/en/latest/reference/examples.html) to see how it is possible to change `cfg` contents and so the clod-init behavior. + +Note that for each run of `tfs_generator.yml` playbook a cleanup is made to +ensure that there are no residual `.tf` and `.cfg` files from previous runs. From 99c0d23683b939e92732a62a224b3b5f01cc27a5 Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Mon, 4 Nov 2024 12:28:58 +0100 Subject: [PATCH 4/4] Add cleanup for Azure files This commit adds the cleanup procedure also for the Azure generated files. --- tasks/azure-cleanup.yml | 54 +++++++++++++++++++++++++++++++++++++++++ tasks/azure.yml | 3 +++ 2 files changed, 57 insertions(+) create mode 100644 tasks/azure-cleanup.yml diff --git a/tasks/azure-cleanup.yml b/tasks/azure-cleanup.yml new file mode 100644 index 0000000..447a61f --- /dev/null +++ b/tasks/azure-cleanup.yml @@ -0,0 +1,54 @@ +--- + +- name: Generate list of .tf files to keep + ansible.builtin.set_fact: + files_to_keep: [] + +- name: Add fixed resources files to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/azure/' + item + '.tf' ] }}" + with_items: + - "{{ azure_resources }}" + - "{{ azure_vpn_resources }}" + +- name: Add key_vault.tf resource file to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/azure/key_vault.tf' }}" + when: + - azure_key_vault_enable | bool + +- name: Add database.tf resource file to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/azure/database.tf' }}" + when: + - azure_database_servers is defined + +- name: Add dynamic .vms.tf files to keep list + ansible.builtin.set_fact: + files_to_keep: "{{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }}" + with_items: + - "{{ azure_vms_groups }}" + +- name: Get the stats (including absolute paths) of the remote files + ansible.builtin.stat: + path: "{{ item }}" + loop: "{{ files_to_keep }}" + register: files_to_keep_stats + +- name: Create a list of files to keep with absolute paths + ansible.builtin.set_fact: + files_to_keep_full_path: "{{ files_to_keep_full_path | default([]) + [ item.stat.path ] }}" + loop: "{{ files_to_keep_stats.results }}" + +- name: Generate the list of all the .tf files + ansible.builtin.find: + paths: "{{ tf_config_dir }}" + patterns: "*.tf,*.cfg" + register: all_files + +- name: Effectively delete files not to keep + ansible.builtin.file: + path: "{{ item.path }}" + state: absent + loop: "{{ all_files.files }}" + when: item.path not in files_to_keep_full_path diff --git a/tasks/azure.yml b/tasks/azure.yml index 50ce2b3..d60ea8c 100644 --- a/tasks/azure.yml +++ b/tasks/azure.yml @@ -109,6 +109,9 @@ loop_control: loop_var: group_name +- name: Cleanup Azure manifests + ansible.builtin.include_tasks: azure-cleanup.yml + - name: Display a reminder for sourcing variables ansible.builtin.debug: msg: