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: