Cleanup via Ansible
[csit.git] / resources / tools / testbed-setup / ansible / roles / cleanup / tasks / kill_process.yaml
diff --git a/resources/tools/testbed-setup/ansible/roles/cleanup/tasks/kill_process.yaml b/resources/tools/testbed-setup/ansible/roles/cleanup/tasks/kill_process.yaml
new file mode 100644 (file)
index 0000000..4a1180b
--- /dev/null
@@ -0,0 +1,27 @@
+---
+# file: roles/cleanup/tasks/kill_process.yaml
+
+- name: Kill process - Get pid of {{ process }}
+  shell: "ps -ef | grep -v grep | grep -w {{ process }} | awk '{print $2}'"
+  when: >
+    process is defined and process != ""
+  register: running_processes
+  tags: kill-process
+
+- name: Kill process - Safe kill {{ process }}
+  shell: "kill {{ item }}"
+  with_items: "{{ running_processes.stdout_lines }}"
+  tags: kill-process
+
+- wait_for:
+    path: "/proc/{{ item }}/status"
+    state: absent
+  with_items: "{{ running_processes.stdout_lines }}"
+  ignore_errors: yes
+  register: killed_processes
+  tags: kill-process
+
+- name: Kill process - Force kill {{ process }}
+  shell: "kill -9 {{ item }}"
+  with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
+  tags: kill-process