Skip to content

Commit

Permalink
Separate cleanup tasks in a dedicated file
Browse files Browse the repository at this point in the history
This commit creates the libvirt-cleanup.yml file that is included inside the
main one.
  • Loading branch information
rascasoft committed Nov 4, 2024
1 parent a6edd12 commit 0eddcb5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
51 changes: 51 additions & 0 deletions tasks/libvirt-cleanup.yml
Original file line number Diff line number Diff line change
@@ -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
53 changes: 2 additions & 51 deletions tasks/libvirt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0eddcb5

Please sign in to comment.