How to terminate a process using Ansible

We can use Ansible shell module to check whether a process is running and kill that if needed.

Ansible code is as follows.

- name: Get the PID of running process
    ignore_errors: yes
    shell: "ps -few | grep processname | awk '{print $2}'"
    register: running_processes

- name: Kill running processes
    ignore_errors: yes
    shell: "kill {{ item }}"
    with_items: "{{ running_processes.stdout_lines }}"

Here we need to replace "processname" with the name of the required process.

Leave a Reply

Your email address will not be published. Required fields are marked *