Skip to content

Commit

Permalink
Merge pull request #2 from mmul-it/fix-manifests-cleanup
Browse files Browse the repository at this point in the history
Fix manifests cleanup
  • Loading branch information
rascasoft authored Nov 7, 2024
2 parents d0fce7e + 99c0d23 commit 7456c2a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Libvirt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
54 changes: 54 additions & 0 deletions tasks/azure-cleanup.yml
Original file line number Diff line number Diff line change
@@ -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' ] }}"

Check warning on line 9 in tasks/azure-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep + [ tf_config_dir + '/azure/' + item + '.tf' ] }} -> {{ 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' ] }}"

Check warning on line 28 in tasks/azure-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }} -> {{ 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 ] }}"

Check warning on line 40 in tasks/azure-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep_full_path | default([]) + [ item.stat.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
3 changes: 3 additions & 0 deletions tasks/azure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
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' ] }}"

Check warning on line 9 in tasks/libvirt-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep + [ tf_config_dir + '/' + item + '.tf' ] }} -> {{ 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' ] }}"

Check warning on line 19 in tasks/libvirt-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep + [ tf_config_dir + '/cloud_init_' + item.name + '.cfg' ] }} -> {{ 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' ] }}"

Check warning on line 25 in tasks/libvirt-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep + [ tf_config_dir + '/' + item + '.vms.tf' ] }} -> {{ 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 ] }}"

Check warning on line 37 in tasks/libvirt-cleanup.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {{ files_to_keep_full_path | default([]) + [ item.stat.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
3 changes: 3 additions & 0 deletions tasks/libvirt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
loop_control:
loop_var: tf_group_name

- name: Cleanup Libvirt manifests and configurations
ansible.builtin.include_tasks: libvirt-cleanup.yml

- name: Display a reminder for sourcing variables
ansible.builtin.debug:
msg:
Expand Down

0 comments on commit 7456c2a

Please sign in to comment.