TomTom
TomTom

Reputation: 370

Ansible delegate_to keyword - can I delegate a task to a group defined in an inventory rather than to a specific host?

In my playbook I want to run a delegated task on 4 load balancers. Can I just simply use delegate_to: loadbalancers_group in my task? Will delegate_to accept a group rather than a single host? I am not sure as the Ansible docs say:

delegate_to - Host to execute task instead of the target (inventory_hostname)

Upvotes: 3

Views: 5797

Answers (2)

tito kipkurgat
tito kipkurgat

Reputation: 7

You could also use when: Example,

- file:
    path: touched_file
    state: touch
  when: inventory_hostname  in groups['loadbalancers_group']

Upvotes: -2

Jack
Jack

Reputation: 6158

No. The delegate_to option needs to be a host, not a group.

What you can do is loop over the group, e.g.:

- file:
    path: touched_file
    state: touch
  delegate_to: "{{ item }}"
  loop: "{{ groups['loadbalancers_group'] }}"

Upvotes: 8

Related Questions