]> git.phdru.name Git - ansible.git/commitdiff
Feat(cleanup-pip-cache): Add a role to cleanup pip cache
authorOleg Broytman <phd@phdru.name>
Mon, 6 Jan 2020 16:29:10 +0000 (19:29 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 6 Jan 2020 16:29:10 +0000 (19:29 +0300)
playbooks/roles/cleanup-pip-cache/tasks/cleanup-user.yml [new file with mode: 0644]
playbooks/roles/cleanup-pip-cache/tasks/main.yml [new file with mode: 0644]
playbooks/roles/cleanup-pip-cache/tasks/show-user.yml [new file with mode: 0644]

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 (file)
index 0000000..bed3003
--- /dev/null
@@ -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 (file)
index 0000000..e339681
--- /dev/null
@@ -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 (file)
index 0000000..05f7f33
--- /dev/null
@@ -0,0 +1,3 @@
+- name: du ~{{ user }}/.cache/pip
+  debug:
+    msg: "Before: {{ before.stdout }} / After: {{ after.stdout }}"