Ansible: Fix cleanup procedures
[csit.git] / resources / tools / testbed-setup / ansible / roles / cleanup / tasks / kill_process.yaml
index 4a1180b..30a9f45 100644 (file)
@@ -1,27 +1,33 @@
 ---
 # 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 - {{ process }}
+  block:
+  - 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
+  - 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
+  - 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
+  - name: Kill process - Force kill {{ process }}
+    shell: "kill -9 {{ item }}"
+    with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
+    tags: kill-process
+
+  rescue:
+  - fail:
+      msg: "Kill process {{ process }} failed!"