# Delegate an already looping task to a group of hosts

**URL:** <https://forum.ansible.com/t/delegate-an-already-looping-task-to-a-group-of-hosts/1688>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 13, 2023, 5:47am UTC](https://forum.ansible.com/t/delegate-an-already-looping-task-to-a-group-of-hosts/1688 "2023-10-13T05:47:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igel/32/1159_2.png) [@Igel](https://forum.ansible.com/u/Igel)\
**Post date:** [October 13, 2023, 5:47am UTC](https://forum.ansible.com/t/delegate-an-already-looping-task-to-a-group-of-hosts/1688/1 "2023-10-13T05:47:21Z")

</div>

I want to loop over a task in my playbook with different items (for example, put a bunch of templated files on the host machine). I also want to execute this specific task on a group of hosts (A) that is not the same as the list of hosts defined at the start of the play book (B). A is a subset of B.

I’ve read online that you can use a for loop over a host group and use the items for the _delegate\_to_ field. But then I can’t loop over other items, right?

```auto
- name: do stuff on loop on a couple of hosts
  ansible.builtin.copy:
    src: "files/{{ item }}"
    dest: "/etc/blahfolder/{{ item }}"
  loop: "{{ file_list }}"
  delegate_to: "{{ other_items? }}"
  another_loop?: "{{ couple_of_hosts }}"

```

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 13, 2023, 10:04am UTC](https://forum.ansible.com/t/delegate-an-already-looping-task-to-a-group-of-hosts/1688/2 "2023-10-13T10:04:09Z")

</div>

Hi,

Simplest way would be to duplicate your task in two plays, or writing your task elsewhere and call it in your separate plays using `ansible.builtin.import_tasks:` (or include for dynamic reuse), targeting specific hosts for each play.

Here is a simple example with two plays running a duplicated task on their specified hosts:

```yaml
---

- hosts: host1,host2
  tasks:
    - name: Test task
      ansible.builtin.debug:
        msg: "truc"

- hosts: host3,host4
  tasks:
    - name: Test task
      ansible.builtin.debug:
        msg: "truc"

```

You could also use `delegate_to:` on your task, with `run_once: true` (since delegated tasks will indeed be executed on hosts you define in `delegate_to:`, but for each host you target on your play, so effectively running your task n times on delegated node, n being the number of hosts your play targets. Weird, I know).

Here is an example using `delegate_to:` (and `run_once: true`), looping through another hosts list:

```yaml
---

- hosts: host1,host2
  gather_facts: false
  pre_tasks:
    - name: Gather facts subset
      ansible.builtin.setup:
        filter: ['ansible_host']

  tasks:
    - name: Print hostname
      ansible.builtin.debug:
        msg: "{{ ansible_host }}"
      delegate_to: "{{ item }}"
      run_once: true
      loop:
        - host3
        - host4

```

```bash
PLAY [host1,host2] **************************************************************************************************************************************************************

TASK [Gather facts subset] **************************************************************************************************************************************************************
Friday 13 October 2023 12:01:03 +0200 (0:00:00.017) 0:00:00.017 ********
ok: [host1]
ok: [host2]

TASK [Print hostname] *******************************************************************************************************************************************************************
Friday 13 October 2023 12:01:06 +0200 (0:00:03.363) 0:00:03.380 ********
ok: [host1 -> host3] => (item=host3) => {
    "msg": "host3"
}
ok: [host1 -> host4] => (item=host4) => {
    "msg": "host4"
}

PLAY RECAP ******************************************************************************************************************************************************************************
host1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Friday 13 October 2023 12:01:06 +0200 (0:00:00.073) 0:00:03.454 ********
===============================================================================
Gather facts subset -------------------------------------------------------------------------------------------------------------------------------------------------------------- 3.36s
Print hostname ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.07s

```

Hope it answers your questions.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 12, 2023, 10:04am UTC](https://forum.ansible.com/t/delegate-an-already-looping-task-to-a-group-of-hosts/1688/3 "2023-11-12T10:04:31Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
