Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a running process using ansible?

Tags:

I have an ansible playbook to kill running processes and works great most of the time!, however, from time to time we find processes that just can't be killed so, "wait_for" gets to the timeout, throws an error and it stops the process.

The current workaround is to manually go into the box, use "kill -9" and run the ansible playbook again so I was wondering if there is any way to handle this scenario from ansible itself?, I mean, I don't want to use kill -9 from the beginning but I maybe a way to handle the timeout?, even to use kill -9 only if process hasn't been killed in 300 seconds? but what would be the best way to do it?

These are the tasks I currently have:

- name: Get running processes   shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print $2}'"   register: running_processes  - name: Kill running processes   shell: "kill {{ item }}"   with_items: "{{ running_processes.stdout_lines }}"  - name: Waiting until all running processes are killed   wait_for:     path: "/proc/{{ item }}/status"     state: absent   with_items: "{{ running_processes.stdout_lines }}" 

Thanks!

like image 397
Julio Fernandez Avatar asked Oct 01 '17 18:10

Julio Fernandez


People also ask

How do you kill Ansible playbook?

Here ansible playbook file is getting all java processes, killing it using simple kill -9 command.

How do I find out what processes are running in Ansible?

There is no module to manage/check processes in Ansible. You can use the command included in the question with shell module (pay attention to this), register the result and run a task to start the process.

How do you handle long running tasks in Ansible?

For long running asynchronous tasks, it's good to set poll=0 so that Ansible can immediately jump to the next task after starting the current one without waiting for the result. Register: It is basically used to register the results on a task in a variable.


1 Answers

You could ignore errors on wait_for and register the result to force kill failed items:

- name: Get running processes   shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print $2}'"   register: running_processes  - name: Kill running processes   shell: "kill {{ item }}"   with_items: "{{ running_processes.stdout_lines }}"  - wait_for:     path: "/proc/{{ item }}/status"     state: absent   with_items: "{{ running_processes.stdout_lines }}"   ignore_errors: yes   register: killed_processes  - name: Force kill stuck processes   shell: "kill -9 {{ item }}"   with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}" 
like image 176
Eric Citaire Avatar answered Oct 13 '22 07:10

Eric Citaire