Skip to content

Commit e3435b3

Browse files
committed
fix(ansible): update cleanup clients
1 parent 4aee8ef commit e3435b3

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

.github/workflows/ansible--cleanup-clients.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ on:
1717
serial:
1818
description: 'The value for the variable_serial argument'
1919
type: number
20+
dry_run:
21+
description: 'Run in dry-run mode to see what would be deleted'
22+
type: boolean
23+
required: true
24+
default: false
2025

2126
jobs:
2227
run_playbook:
@@ -55,5 +60,5 @@ jobs:
5560
ansible-playbook -i inventory/linode.yml \
5661
play-any--cleanup-clients.yml \
5762
--private-key=~/.ssh/id_for_ansible.pem \
58-
-e '{"variable_host": "${{ matrix.host_groups }}", "variable_serial": '$SERIAL_VALUE'}' \
63+
-e '{"variable_host": "${{ matrix.host_groups }}", "variable_serial": '$SERIAL_VALUE', "variable_dry_run": '${{ inputs.dry_run }}'}' \
5964
${{ inputs.verbosity }}

ansible/play-any--cleanup-clients.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@
33
hosts: "{{ variable_host | default('null') }}"
44
serial: '{{ variable_serial | default(1) }}'
55
gather_facts: false
6+
vars:
7+
dry_run: '{{ variable_dry_run | default(false) }}'
68
tasks:
7-
- name: Capture directories older than 15 days
9+
- name: Get all release directories
810
ansible.builtin.find:
911
paths: /home/freecodecamp/client/releases
1012
recurse: false
1113
file_type: directory
12-
age: "{{ variable_age | default('15d') }}"
13-
register: files_to_delete
14+
register: all_releases
1415

15-
- name: Remove directories older than 15 days
16+
- name: Sort releases by modification time
17+
ansible.builtin.set_fact:
18+
sorted_releases:
19+
"{{ all_releases.files | sort(attribute='mtime', reverse=true) }}"
20+
21+
- name: Get releases to delete (excluding last 3)
22+
ansible.builtin.set_fact:
23+
releases_to_delete: '{{ sorted_releases[3:] }}'
24+
25+
- name: Log releases to be deleted
26+
ansible.builtin.debug:
27+
msg: 'Would delete release: {{ item.path }}'
28+
with_items: '{{ releases_to_delete }}'
29+
when: dry_run
30+
31+
- name: Remove old releases
1632
ansible.builtin.file:
1733
path: '{{ item.path }}'
1834
state: absent
19-
with_items: '{{ files_to_delete.files }}'
35+
with_items: '{{ releases_to_delete }}'
36+
when: not dry_run

0 commit comments

Comments
 (0)