Yaml - Use for loop

Hi,

Can able to find the script “test.sh” pid and kill it using below yml code.

I need to kill multiple script like test1.sh test2.sh test3.sh in single yml code like using for loop to give input

Can you suggest the idea to implement the above.

if those processes were started using services, you can use the ansible service module to stop it like this

- name: Stop service httpd, if running
  service:
    name: httpd
    state: stopped

For more information on the service module see https://docs.ansible.com/ansible/latest/modules/service_module.html

If you have multiple proceses to stop you can add with_items to the task

- name: Stop services, if running
  service:
    name: "{{ item.value }}"
    state: stopped
  with_items:
    - httpd
    - ntpd

Hi Tony Chia,

It’s not a services, I need to stop the shell scripts which is running in background. find the pid using script name and kill it.

Can you suggest the better idea to implement it

If you want to do tat add serial :1 and try ,that will work out

You should probably just use pkill

- command: pkill -9 {{ item }}
   with_items:
     - test1.sh
     - test2.sh

Pkill also have lot of other option to make sure you don't kill the wrong process name.