Delegate an already looping task to a group of hosts

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?

- 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 }}"

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:

---

- 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:

---

- 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
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.

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