ansible-collections / community.zabbix

Zabbix Ansible modules
http://galaxy.ansible.com/community/zabbix
Other
322 stars 284 forks source link

Zabbix 7.0 - zabbix_agent not setting `monitored_by` #1367

Closed andrew-landsverk-win closed 1 month ago

andrew-landsverk-win commented 2 months ago
SUMMARY

Since we updated our cluster to 7.0, whenever we add a new host to Ansible, using the zabbix_agent role, I see that the monitored_by parameter is not being set in: https://github.com/ansible-collections/community.zabbix/blob/760b134a2eaa88e99a8cc4c221b4071ba376b7c9/roles/zabbix_agent/tasks/api.yml#L16

This causes new hosts to not be monitored at all until I go in the UI and manually set the Monitored by to Proxy.

Just looking for a way to configure monitored_by to be used by the code I mentioned above. The zabbix_host module already supports it as a parameter and with some copy/paste of a bunch of default variables I was able to call zabbix_host outside of the role execution and successfully set monitored_by to proxy.

ISSUE TYPE
COMPONENT NAME

zabbix_agent role, zabbix_host module

ANSIBLE VERSION
ansible [core 2.15.3]
  config file = /home/andrew.landsverk/code/ansible/ansible.cfg
  configured module search path = ['/home/andrew.landsverk/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/andrew.landsverk/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/andrew.landsverk/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/andrew.landsverk/.local/bin/ansible
  python version = 3.9.18 (main, Jul  3 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
CONFIGURATION
ANSIBLE_FORCE_COLOR(/home/andrew.landsverk/code/ansible/ansible.cfg) = True
ANSIBLE_NOCOWS(/home/andrew.landsverk/code/ansible/ansible.cfg) = False
CALLBACKS_ENABLED(/home/andrew.landsverk/code/ansible/ansible.cfg) = ['profile_tasks', 'redhat.satellite.foreman']
CONFIG_FILE() = /home/andrew.landsverk/code/ansible/ansible.cfg
DEFAULT_FORKS(/home/andrew.landsverk/code/ansible/ansible.cfg) = 20
DISPLAY_SKIPPED_HOSTS(/home/andrew.landsverk/code/ansible/ansible.cfg) = False
MAX_FILE_SIZE_FOR_DIFF(/home/andrew.landsverk/code/ansible/ansible.cfg) = 1044480
TASK_TIMEOUT(/home/andrew.landsverk/code/ansible/ansible.cfg) = 900
OS / ENVIRONMENT / Zabbix Version

Zabbix running on Rocky 9, version 7.0.2

STEPS TO REPRODUCE

Running community.zabbix.zabbix_agent with standard parameters except zabbix_agent_proxy is being set to monitor with a proxy.

EXPECTED RESULTS

Ability to set monitored_by via Ansible Collection

ACTUAL RESULTS

monitored_by is set to Server regardless of proxy being set

hndrkx commented 2 months ago

A quick workaround is to add monitored_by: "{{ 'proxy' if zabbix_agent_proxy is defined else omit }}" to the following ansible_collections/community/zabbix/roles/zabbix_agent/tasks/api.yml

It should look something like this:

- name: "API | Create a new host or update an existing host's info"
  community.zabbix.zabbix_host:
    host_name: "{{ zabbix_agent_hostname }}"
    host_groups: "{{ zabbix_host_groups }}"
    link_templates: "{{ zabbix_agent_link_templates }}"
    status: "{{ zabbix_host_status }}"
    state: "{{ zabbix_agent_host_state }}"
    force: "{{ zabbix_agent_host_update }}"
    proxy: "{{ zabbix_agent_proxy }}"
    inventory_mode: "{{ zabbix_agent_inventory_mode }}"
    interfaces: "{{ zabbix_agent_interfaces }}"
    visible_name: "{{ zabbix_agent_visible_hostname | default(zabbix_agent_hostname) }}"
    tls_psk: "{{ zabbix_agent_tlspsk_secret | default(omit) }}"
    tls_psk_identity: "{{ zabbix_agent_tlspskidentity | default(omit) }}"
    tls_issuer: "{{ zabbix_agent_tlsservercertissuer | default(omit) }}"
    tls_subject: "{{ zabbix_agent_tls_subject | default(omit) }}"
    tls_accept: "{{ zabbix_agent_tls_config[zabbix_agent_tlsaccept | default('unencrypted')] }}"
    tls_connect: "{{ zabbix_agent_tls_config[zabbix_agent_tlsconnect | default('unencrypted')] }}"
    description: "{{ zabbix_agent_description | default(omit) }}"
    inventory_zabbix: "{{ zabbix_agent_inventory_zabbix | default({}) }}"
    ipmi_authtype: "{{ zabbix_agent_ipmi_authtype | default(omit) }}"
    ipmi_password: "{{ zabbix_agent_ipmi_password| default(omit) }}"
    ipmi_privilege: "{{ zabbix_agent_ipmi_privilege | default(omit) }}"
    ipmi_username: "{{ zabbix_agent_ipmi_username | default(omit) }}"
    tags: "{{ zabbix_agent_tags | default(omit) }}"
    monitored_by: "{{ 'proxy' if zabbix_agent_proxy is defined else omit }}"  <-------
  register: zabbix_api_host_created
  until: zabbix_api_host_created is succeeded
  retries: 10
  delegate_to: "{{ zabbix_api_server_host }}"
  changed_when: false
  tags:
    - api

If you have set the zabbix_agent_proxy var to a proxy server, it will then be set to be monitored by that proxy. This is just a quick fix and does not work with the new proxygroup feature I guess.

Zablove commented 2 months ago

Currently running into the same issue. Additional information: If new host is deployed in zabbix, no proxy will be linked. If a hosts is updated and a proxy is linked, it will remain linked (expected) If a host already is deployed and no proxy is linked, no proxy will be linked. If a hosts will be deployed and the proxy does not exists in Zabbix, an error will raise that the proxy not exists (expected)

Collection version 3.1.1 and tested against Zabbix 7.0.0 and 7.0.3

tl87 commented 2 months ago

Im having the same issue.

CE-RS42 commented 2 months ago

Same here! Hope this will be resolved soon. This is preventing us from upgrading atm.

the-iot commented 2 months ago

hi @all.

you can add a second task to you role to set the "monitored_by" proxy.

example:


    # Edit Host in Zabbix an set monitored by to proxy
    - name: 'Update Zabbix Host'
      vars:
        ansible_network_os: community.zabbix.zabbix
        ansible_connection: httpapi
        ansible_httpapi_port: 443
        ansible_zabbix_url_path: ''  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
        ansible_user: ''
        ansible_httpapi_pass: ''
        ansible_host: '#zabbixurl#'
      community.zabbix.zabbix_host:
        host_name: '{{ ansible_hostname | lower }}'
        # monitored_by: "{{ zabbix_version is version('7.0', '>=') | ternary('proxy', omit) }}"
        monitored_by: proxy
        proxy: '#insert proxy name or set variable#'
      register: zabbix_host
tl87 commented 2 months ago

I managed to get it to work.

Im using Zabbix v7.0.3 and community.zabbix v3.1.1.

I added community.zabbix to my collections/requirements.yml file:

---
collections:
  - name: community.zabbix
    version: ">=3.1.1"

Below is an example of the ansible tasks that I use:

- name: Create a new host in Zabbix
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_host: zabbix-server.domain.tld
    ansible_zabbix_url_path: ""
  become: false
  delegate_to: localhost
  community.zabbix.zabbix_host:
    force: true
    host_name: "{{ inventory_hostname }}"
    visible_name: "{{ inventory_hostname }}"
    description:
    host_groups:
      - Staging
    link_templates:
      - Windows by Zabbix agent active
    status: enabled
    state: present
    inventory_mode: automatic
    inventory_zabbix:
      tag:
      alias:
      notes:
      location:
      site_rack:
      os:
      hardware:
    ipmi_authtype:
    ipmi_privilege:
    ipmi_username:
    ipmi_password:
    interfaces:
      - type: 1
        main: 1
        useip: 0
        ip:
        dns: "{{ inventory_hostname }}"
        port: 10050
    monitored_by: "proxy"
    proxy: "proxy.domain.tld"
    state: "present"
    macros:
      - macro: OS
        value: Windows
    tags:
      - tag: target
        value: windows

- name: Update an existing host's TLS
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: ""
    ansible_host: zabbix-server.domain.tld
  become: false
  delegate_to: localhost
  community.zabbix.zabbix_host:
    host_name: "{{ inventory_hostname }}"
    visible_name: "{{ inventory_hostname }}"
    host_groups:
      - Staging
    tls_psk_identity: "{{ inventory_hostname }}"
    tls_connect: 2
    tls_accept: 2
    tls_psk: "{{ psk }}"
    force: true

Monitored_by can be set to: ["zabbix_server", "proxy", "proxy_group"] - take a look at https://github.com/ansible-collections/community.zabbix/blob/3.0.3/plugins/modules/zabbix_host.py#L76