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
My bad… managed to get it working by using: inventory_hostname in groups[‘zabbixslave’]
Cheers!
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?
vbotka
(Vladimir Botka)
December 15, 2021, 5:44am
4
>>
>> 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
> 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]
Interesting! And can I use the “delegate_to” command to more than one host at a time?
Cheers!
Lucas
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
[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 :
utoddl
(Todd Lewis)
December 15, 2021, 12:36pm
8
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. ”