# Yaml - Use for loop

**URL:** <https://forum.ansible.com/t/yaml-use-for-loop/28205>\
**Category:** Ansible Project\
**Created:** [July 31, 2018, 4:59am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205 "2018-07-31T04:59:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scofield](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Scofield](https://forum.ansible.com/u/Scofield)\
**Post date:** [July 31, 2018, 4:59am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/1 "2018-07-31T04:59:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [July 31, 2018, 5:30am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/2 "2018-07-31T05:30:27Z")

</div>

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

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

```

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

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [July 31, 2018, 5:32am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/3 "2018-07-31T05:32:54Z")

</div>

If you have multiple proceses to stop you can add with\_items to the task

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

```

```auto
    - httpd

```

```auto
    - ntpd

```

---

<div class="post-metadata">

**Author:** ![Scofield](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Scofield](https://forum.ansible.com/u/Scofield)\
**Post date:** [July 31, 2018, 6:05am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/4 "2018-07-31T06:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [July 31, 2018, 6:06am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/5 "2018-07-31T06:06:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2018, 9:15am UTC](https://forum.ansible.com/t/yaml-use-for-loop/28205/6 "2018-07-31T09:15:19Z")

</div>

You should probably just use pkill

- command: pkill -9 {{ item }}  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- test1.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- test2.sh

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