From: Oleg Broytman Date: Mon, 6 Jan 2020 16:29:10 +0000 (+0300) Subject: Feat(cleanup-pip-cache): Add a role to cleanup pip cache X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=defcd9e9afd7b4cba6d55a65094fb5e76b0b2634;p=ansible.git Feat(cleanup-pip-cache): Add a role to cleanup pip cache --- diff --git a/playbooks/roles/cleanup-pip-cache/tasks/cleanup-user.yml b/playbooks/roles/cleanup-pip-cache/tasks/cleanup-user.yml new file mode 100644 index 0000000..bed3003 --- /dev/null +++ b/playbooks/roles/cleanup-pip-cache/tasks/cleanup-user.yml @@ -0,0 +1,19 @@ +- name: List ~{{ user }}/.cache/pip before + become: "{{ become | default('false') }}" + shell: "du -sh ~{{ user }}/.cache/pip | cut -f1" + register: before + changed_when: false + +- name: Remove old files from ~{{ user }}/.cache/pip + become: "{{ become | default('false') }}" + shell: "exec find ~{{ user }}/.cache/pip -type f -mtime +{{ days | default('180') }} -delete" + +- name: List ~{{ user }}/.cache/pip after + become: "{{ become | default('false') }}" + shell: "du -sh ~{{ user }}/.cache/pip | cut -f1" + register: after + changed_when: false + +- name: Remove empty directories from ~{{ user }}/.cache/pip + become: "{{ become | default('false') }}" + shell: "exec find ~{{ user }}/.cache/pip -type d -empty -delete" diff --git a/playbooks/roles/cleanup-pip-cache/tasks/main.yml b/playbooks/roles/cleanup-pip-cache/tasks/main.yml new file mode 100644 index 0000000..e339681 --- /dev/null +++ b/playbooks/roles/cleanup-pip-cache/tasks/main.yml @@ -0,0 +1,37 @@ +- name: Check ~phd/.cache/pip + stat: + path: ~phd/.cache/pip + register: user_cache_exists + changed_when: false + +- name: Check ~root/.cache/pip + become: true + stat: + path: ~root/.cache/pip + register: root_cache_exists + changed_when: false + +- name: Cleanup ~phd/.cache/pip + import_tasks: cleanup-user.yml + vars: + user: phd + when: user_cache_exists.stat.exists + +- name: Cleanup ~root/.cache/pip + import_tasks: cleanup-user.yml + vars: + become: true + user: root + when: root_cache_exists.stat.exists + +- name: du ~phd/.cache/pip + import_tasks: show-user.yml + vars: + user: phd + when: user_cache_exists.stat.exists + +- name: du ~root/.cache/pip + import_tasks: show-user.yml + vars: + user: root + when: root_cache_exists.stat.exists diff --git a/playbooks/roles/cleanup-pip-cache/tasks/show-user.yml b/playbooks/roles/cleanup-pip-cache/tasks/show-user.yml new file mode 100644 index 0000000..05f7f33 --- /dev/null +++ b/playbooks/roles/cleanup-pip-cache/tasks/show-user.yml @@ -0,0 +1,3 @@ +- name: du ~{{ user }}/.cache/pip + debug: + msg: "Before: {{ before.stdout }} / After: {{ after.stdout }}"