# Problem with delegate\_to to group in inventory

**URL:** <https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112>\
**Category:** Ansible Project\
**Created:** [December 15, 2021, 2:36am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112 "2021-12-15T02:36:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Possamai](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@Lucas\_Possamai](https://forum.ansible.com/u/Lucas_Possamai)\
**Post date:** [December 15, 2021, 2:36am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/1 "2021-12-15T02:36:12Z")

</div>

Hi guys.

I’m trying to use the “delegate\_to”, to only run a certain task to a group in my inventory.

inventory file:

[zabbixprimary]  
localhost ansible\_port=2235 # zabbix primary

[zabbixslave]  
localhost ansible\_port=2236 # zabbix secondary

[zabbix:children]  
zabbixprimary  
zabbixslave

main.yml:

- name: Setup Zabbix and Grafana  
hosts: zabbix  
become: yes  
become\_user: root  
gather\_facts: False

task.yml:

- name: Enable Zabbix HA - primary  
run\_once: true  
ansible.builtin.shell: sed -i ‘s/# HANodeName=/HANodeName={{ hostname\_secondary }}/g’ /etc/zabbix/zabbix\_server.conf && sed -i ‘s/# NodeAddress=localhost:10051/NodeAddress={{ hostname\_primary }}:10051/g’ /etc/zabbix/zabbix\_server.conf  
when: not ansible\_check\_mode and zabbix\_ha  
delegate\_to: zabbixprimary

- name: Enable Zabbix HA - secondary  
run\_once: true  
ansible.builtin.shell: sed -i ‘s/# NodeAddress=localhost:10051/NodeAddress={{ hostname\_secondary }}:10051/g’ /etc/zabbix/zabbix\_server.conf  
when: not ansible\_check\_mode and zabbix\_ha  
delegate\_to: zabbixslave

But I get an error:  
fatal: [localhost → zabbixprimary]: UNREACHABLE! =\> {“changed”: false, “msg”: “Failed to connect to the host via ssh: ssh: Could not resolve hostname zabbixprimary: nodename nor servname provided, or not known”, “unreachable”: true}

Am i doing it right? I couldn’t find much information online about how to achieve this.

Thanks!  
Lucas

---

<div class="post-metadata">

**Author:** ![Lucas\_Possamai](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@Lucas\_Possamai](https://forum.ansible.com/u/Lucas_Possamai)\
**Post date:** [December 15, 2021, 3:03am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/2 "2021-12-15T03:03:06Z")

</div>

My bad… managed to get it working by using: inventory\_hostname in groups[‘zabbixslave’]

Cheers!

---

<div class="post-metadata">

**Author:** ![Lucas\_Possamai](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@Lucas\_Possamai](https://forum.ansible.com/u/Lucas_Possamai)\
**Post date:** [December 15, 2021, 3:30am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/3 "2021-12-15T03:30:40Z")

</div>

Well… Didn’t quite get it, actually.

My task.yml looks like this:

- name: Enable Zabbix HA - primary  
ansible.builtin.shell: sed -i ‘s/# HANodeName=/HANodeName={{ hostname\_secondary }}/g’ /etc/zabbix/zabbix\_server.conf && sed -i ‘s/# NodeAddress=localhost:10051/NodeAddress={{ hostname\_primary }}:10051/g’ /etc/zabbix/zabbix\_server.conf  
when: inventory\_hostname in groups[‘zabbixprimary’] and zabbix\_ha and not ansible\_check\_mode  
delegate\_to: “{{ item }}”  
delegate\_facts: true  
with\_items: “{{ groups[‘zabbixprimary’] }}”

- name: Enable Zabbix HA - secondary  
ansible.builtin.shell: sed -i ‘s/# NodeAddress=localhost:10051/NodeAddress={{ hostname\_secondary }}:10051/g’ /etc/zabbix/zabbix\_server.conf  
when: inventory\_hostname in groups[‘zabbixslave’] and zabbix\_ha and not ansible\_check\_mode  
delegate\_to: “{{ item }}”  
delegate\_facts: true  
with\_items: “{{ groups[‘zabbixslave’] }}”

However, looks like only one of the hosts is updated. Am I doing it right?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 15, 2021, 5:44am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/4 "2021-12-15T05:44:02Z")

</div>

> \>\>  
> \>\> inventory file:  
> \>\> [zabbixprimary]  
> \>\> localhost ansible\_port=2235 # zabbix primary  
> \>\>  
> \>\> [zabbixslave]  
> \>\> localhost ansible\_port=2236 # zabbix secondary  
> \>\>  
> \>\> [zabbix:children]  
> \>\> zabbixprimary  
> \>\> zabbixslave  
> \>\>  
> \>\> - name: Enable Zabbix HA - primary  
> \>\> ansible.builtin.shell: sed -i 's/# HANodeName=/HANodeName={{  
> \>\> ...  
> \>\> delegate\_to: zabbixprimary  
> \>\>  
> \>\> fatal: [localhost -\> zabbixprimary]: UNREACHABLE! =\> {"changed": false,  
> \>\> "msg": "Failed to connect to the host via ssh: ssh: Could not resolve  
> \>\> hostname zabbixprimary: nodename nor servname provided, or not known",  
> \>\> "unreachable": true}

It's not possible to delegate a task to a group. Delegate to a host  
instead. Change the inventory. For example

&nbsp;&nbsp;\> cat hosts  
&nbsp;&nbsp;zabbixprimary ansible\_host=localhost ansible\_port=2235  
&nbsp;&nbsp;zabbixslave ansible\_host=localhost ansible\_port=2236

&nbsp;&nbsp;[zabbix]  
&nbsp;&nbsp;zabbixprimary  
&nbsp;&nbsp;zabbixslave

You should be able to delegate to the hosts. For example

&nbsp;&nbsp;- hosts: zabbix  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: echo OK  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: zabbixprimary  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: echo OK  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: zabbixslave  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

should work as expected

&nbsp;&nbsp;TASK [shell] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;changed: [zabbixprimary -\> zabbixprimary]

&nbsp;&nbsp;TASK [shell] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;changed: [zabbixprimary -\> zabbixslave]

---

<div class="post-metadata">

**Author:** ![Lucas\_Possamai](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@Lucas\_Possamai](https://forum.ansible.com/u/Lucas_Possamai)\
**Post date:** [December 15, 2021, 6:00am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/5 "2021-12-15T06:00:07Z")

</div>

Interesting! And can I use the “delegate\_to” command to more than one host at a time?

Cheers!  
Lucas

---

<div class="post-metadata">

**Author:** ![Lucas\_Possamai](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@Lucas\_Possamai](https://forum.ansible.com/u/Lucas_Possamai)\
**Post date:** [December 15, 2021, 6:05am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/6 "2021-12-15T06:05:33Z")

</div>

> > It’s not possible to delegate a task to a group. Delegate to a host  
> > instead. Change the inventory. For example
> > 
> > > cat hosts  
> > > zabbixprimary ansible\_host=localhost ansible\_port=2235  
> > > zabbixslave ansible\_host=localhost ansible\_port=2236
> > 
> > [zabbix]  
> > zabbixprimary  
> > zabbixslave
> > 
> > You should be able to delegate to the hosts. For example
> > 
> > - hosts: zabbix  
> > tasks:
> > - shell: echo OK  
> > delegate\_to: zabbixprimary  
> > run\_once: true
> > - shell: echo OK  
> > delegate\_to: zabbixslave  
> > run\_once: true
> > 
> > should work as expected
> > 
> > TASK [shell] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > changed: [zabbixprimary → zabbixprimary]
> > 
> > TASK [shell] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > changed: [zabbixprimary → zabbixslave]
> > 
> > –  
> > Vladimir Botka
> 
> Interesting! And can I use the “delegate\_to” command to more than one host at a time?

Yes, I can! Using:

delegate\_to: “{{ item }}”  
delegate\_facts: true  
loop:

- zabbixslave
- zabbixslave02

---

<div class="post-metadata">

**Author:** ![Vasily\_Klimov](https://avatars.discourse-cdn.com/v4/letter/v/2bfe46/32.png) [@Vasily\_Klimov](https://forum.ansible.com/u/Vasily_Klimov)\
**Post date:** [December 15, 2021, 10:16am UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/7 "2021-12-15T10:16:24Z")

</div>

[zabbix]  
zabbixprimary ansible\_host=IP\_ADDRESS ansible\_port=PORT  
zabbixslave ansible\_host=IP\_ADDRESS ansible\_port=PORT

[zabbix:vars]  
ansible\_python\_interpreter=/usr/bin/python3  
ansible\_user=login  
ansible\_password=password

Good luck.

среда, 15 декабря 2021 г. в 09:06:17 UTC+3, [drum....@gmail.com](mailto:drum....@gmail.com):

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 15, 2021, 12:36pm UTC](https://forum.ansible.com/t/problem-with-delegate-to-to-group-in-inventory/36112/8 "2021-12-15T12:36:22Z")

</div>

BTW, your initial problem was that you had

delegate\_to: zabbixprimary  
where you needed

delegate\_to: “{{ zabbixprimary }}”

But you’re well past that now.

As my old CompSci prof. used to say, “_Don’t let these little things confuse you. Try to get confused on a higher level._”
