Skip to content

Commit

Permalink
find and delete obsolete static config files, so we do not have to de…
Browse files Browse the repository at this point in the history
…lete the entire directory when removing a single site
  • Loading branch information
acozine committed Dec 22, 2023
1 parent 3527d2e commit df64ab6
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions roles/nginxplus/tasks/conf/upload-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,52 @@
with_fileglob: "{{ nginx_http_upload_src }}"
notify: Reload NGINX
when: nginx_http_upload_enable
register: uploaded_static_files
tags: update_conf

# identify and remove obsolete static files
- name: Setup | list files in static config dir
ansible.builtin.find:
paths: "{{ nginx_http_upload_dest }}/"
recurse: false
file_type: file
register: all_static_files
tags: test_template

- name: Setup | set facts for removing obsolete static config files
set_fact:
static_files_on_server: "{{ all_static_files.files|map(attribute='path') }}"
static_files_we_want: "{{ uploaded_static_files.results|map(attribute='dest') }}"
tags: test_template

- name: Debug - view static files on server
ansible.builtin.debug:
var: static_files_on_server
tags: test_template

- name: Debug - view static files we want
ansible.builtin.debug:
var: static_files_we_want
tags: test_template

- name: Find obsolete static files
# ITEMS IN static_files_on_server NOT IN static_files_we_want
set_fact: obsolete_static_files="{{ static_files_on_server | difference(static_files_we_want) }}"
tags: test_template

- name: Debug - view obsolete static files
ansible.builtin.debug:
var: obsolete_static_files
tags: test_template

# when we're confident this works:
# - name: Delete obsolete static files
# ansible.builtin.file:
# path: "{{ item }}"
# state: absent
# loop: "{{ obsolete_static_files }}"
# tags: test_template

- name: Setup | Ensure cache directories exist
ansible.builtin.file:
path: /tmp/nginx/{{ item.short_name }}/NGINX_cache
Expand Down Expand Up @@ -83,7 +127,7 @@
register: all_files
tags: test_template

- name: Setup | set facts for removing old config files
- name: Setup | set facts for removing obsolete dynamic config files
set_fact:
files_on_server: "{{ all_files.files|map(attribute='path') }}"
sites_we_want: "{{ sites_loop.results|map(attribute='dest') }}"
Expand All @@ -99,16 +143,17 @@
var: sites_we_want
tags: test_template

- name: Find obsolete files (files on server not in sites we want)
- name: Find obsolete dynamic config files
# ITEMS in files on server NOT in sites we want
set_fact: obsolete_files="{{ files_on_server | difference(sites_we_want) }}"
tags: test_template

- name: Debug - view obsolete files
- name: Debug - view obsolete dynamic config files
ansible.builtin.debug:
var: obsolete_files
tags: test_template

- name: Delete obsolete files
- name: Delete obsolete dynamic config files
ansible.builtin.file:
path: "{{ item }}"
state: absent
Expand Down

0 comments on commit df64ab6

Please sign in to comment.