Problem with delegate_to to group in inventory

95 views
Skip to first unread message

Lucas Possamai

unread,
Dec 14, 2021, 9:37:00 PM12/14/21
to ansible...@googlegroups.com
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

Lucas Possamai

unread,
Dec 14, 2021, 10:03:54 PM12/14/21
to ansible...@googlegroups.com
My bad.. managed to get it working by using: inventory_hostname in groups['zabbixslave']

Cheers! 

Lucas Possamai

unread,
Dec 14, 2021, 10:31:29 PM12/14/21
to ansible...@googlegroups.com
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?

Vladimir Botka

unread,
Dec 15, 2021, 12:44:20 AM12/15/21
to Lucas Possamai, ansible...@googlegroups.com
On Wed, 15 Dec 2021 16:30:40 +1300
Lucas Possamai <drum....@gmail.com> wrote:

> On Wed, 15 Dec 2021 at 16:03, Lucas Possamai <drum....@gmail.com> wrote:
> >>
> >> 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

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

Lucas Possamai

unread,
Dec 15, 2021, 1:00:51 AM12/15/21
to ansible...@googlegroups.com
Interesting! And can I use the "delegate_to" command to more than one host at a time?

Cheers!
Lucas 

Lucas Possamai

unread,
Dec 15, 2021, 1:06:17 AM12/15/21
to ansible...@googlegroups.com
Yes, I can! Using:

delegate_to: "{{ item }}"
delegate_facts: true
loop:
- zabbixslave
- zabbixslave02
 
Cheers!
Lucas 

Vasily Klimov

unread,
Dec 15, 2021, 5:16:24 AM12/15/21
to Ansible Project
[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:

Todd Lewis

unread,
Dec 15, 2021, 7:36:22 AM12/15/21
to Ansible Project
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."
Reply all
Reply to author
Forward
0 new messages