ansible-collections / community.zabbix

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

Cannot change "zabbix_api_*" variables via inventory/group_vars #1072

Open emilbasv3 opened 10 months ago

emilbasv3 commented 10 months ago
ISSUE TYPE
SUMMARY

While setting the following variables in playbook or in group_vars/all.yml works:

zabbix_api_server_host: HOST
zabbix_api_server_port: 80
ansible_zabbix_url_path: "api_jsonrpc.php"
zabbix_api_login_pass: PASS
zabbix_api_login_user: USER

We cannot figure out how to overwrite some of these variables via group_vars/GROUP.yml as per standard Ansible behavior. The only one that seems working is "zabbix_api_server_host" var.

COMPONENT NAME
community.zabbix.zabbix_agent v2.1.0
ANSIBLE VERSION
# ansible --version
ansible [core 2.15.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/.local/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
CONFIGURATION
# ansible-config dump --only-changed
CONFIG_FILE() = /etc/ansible/ansible.cfg
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = ['/etc/ansible/inventory.d']
DEFAULT_LOAD_CALLBACK_PLUGINS(/etc/ansible/ansible.cfg) = True
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = yaml
OS / ENVIRONMENT / Zabbix Version

OS (ansible): Debian 11 OS (zabbix server): Debian 12 Target OS: windows 2016,2019,2022; Debian 11, Fedora Zabbix: 6.4.2

STEPS TO REPRODUCE

Playbook:

---
- name: "Install & configure zabbix-agent playbook"
  hosts: "{{ variable_host|default('all') }}"
  roles:
    - role: community.zabbix.zabbix_agent

  vars:
    zabbix_agent2: true
    zabbix_agent2_tlspsk_auto: true
    zabbix_api_create_hostgroup: true
    zabbix_api_server_host: zabbix-server-01.example.com
    #zabbix_api_server_port: 80 (DEFAULT)
    ansible_zabbix_url_path: "api_jsonrpc.php"
    zabbix_api_login_pass: PASS
    #zabbix_api_login_user: Admin (DEFAULT)
    zabbix_api_create_hosts: true
    zabbix_agent_hostgroups_state: present
    zabbix_agent_host_state: present
    zabbix_agent_inventory_mode: automatic
    zabbix_useuip: 0
    zabbix_agent_interfaces:
      - type: 1
        main: 1
        useip: "{{ zabbix_useuip }}"
        ip: "{{ zabbix_agent_ip }}"
        dns: "{{ zabbix_agent2_hostname }}"
        port: "{{ zabbix_agent_listenport }}"

Inventory

---
linux:
  children:
    cluster:
      children:
        managers:
          hosts:
            server-00:
        workers:
          hosts:
            server-[01:03]:
    fedora:
      hosts:
        server-FF:
windows:
  children:
    server_2016:
      hosts:
        WIN-2016:
    server_2019:
      hosts:
        WIN-2019:
    server_2022:
      hosts:
        WIN-2022:

Group variables (inventory/group_vars/windows.yml)

---
zabbix_host_groups:
  - Windows Servers

ansible_user: "administrator"
ansible_shell_type: powershell

zabbix_api_server_host: zabbix-server-02.example.com
zabbix_api_server_port: 8080
EXPECTED RESULTS

We exepect the the role honor group dedicated variables Eg. zabbix_api_server_port for windows group is set 8080 but the role connects to the default 80 (verified with tcpdump).

ACTUAL RESULTS

Only zabbix_api_server_host is inherited from group_vars.

WORKAROUND

To work around the problem we have the same credendial, url, port, user and password on both zabbix servers (prod and test).

BGmot commented 10 months ago

Can you add debugging before this task https://github.com/ansible-collections/community.zabbix/blob/main/roles/zabbix_agent/tasks/main.yml#L74 just to show value of zabbix_api_server_port when you set this variable in group_vars/windows.yml ?

BGmot commented 10 months ago

community.zabbix.zabbix_agent role uses delegation https://github.com/ansible-collections/community.zabbix/blob/main/roles/zabbix_agent/tasks/api.yml#L10

delegate_to: "{{ zabbix_api_server_host }}"

zabbix_api_server_host is not a member of your windows host group this is why zabbix_api_server_port is not set correctly for API operations.

MarcoEmilioVentura commented 10 months ago

Can you add debugging before this task https://github.com/ansible-collections/community.zabbix/blob/main/roles/zabbix_agent/tasks/main.yml#L74 just to show value of zabbix_api_server_port when you set this variable in group_vars/windows.yml ?

Hi, i am is the emilbasv3's team we created a task before line 74:

- name: "Activate Debug"
  debug:
    var: "zabbix_api_server_port"

we changed port from 80 to 8080, the print show that the variable is readed as below:

TASK [community.zabbix.zabbix_agent : Activate Debug] ************************************************************************************************************************************************************************************************************************************
ok: [WIN-23H3PDJ0CRT.example.com] => 
  zabbix_api_server_port: 8080
ok: [WIN-QVJ14UN8PL2.example.com] => 
  zabbix_api_server_port: 8080
ok: [WIN-33RUVE5NKBH.example.com] => 
  zabbix_api_server_port: 8080

Our server is working on port 80 and all work on port 80, so port 8080 is ignored (?)

TASK [community.zabbix.zabbix_agent : Run the API calls to Zabbix Server] ****************************************************************************************************************************************************************************************************************
included: /root/.local/lib/python3.9/site-packages/ansible_collections/community/zabbix/roles/zabbix_agent/tasks/api.yml for WIN-23H3PDJ0CRT.example.com, WIN-QVJ14UN8PL2.example.com, WIN-33RUVE5NKBH.example.com

TASK [community.zabbix.zabbix_agent : API | Create host groups] **************************************************************************************************************************************************************************************************************************
ok: [WIN-33RUVE5NKBH.example.com -> v3-re-docker-test.example.com]
ok: [WIN-23H3PDJ0CRT.example.com -> v3-re-docker-test.example.com
ok: [WIN-QVJ14UN8PL2.example.com -> v3-re-docker-test.example.com]

TASK [community.zabbix.zabbix_agent : API | Create a new host or update an existing host's info] *****************************************************************************************************************************************************************************************
skipping: [WIN-23H3PDJ0CRT.example.com]
skipping: [WIN-QVJ14UN8PL2.example.com]
skipping: [WIN-33RUVE5NKBH.example.com]

TASK [community.zabbix.zabbix_agent : API | Create a new host using agent2 or update an existing host's info] ****************************************************************************************************************************************************************************
ok: [WIN-QVJ14UN8PL2.example.com -> v3-re-docker-test.example.com]
ok: [WIN-23H3PDJ0CRT.example.com -> v3-re-docker-test.example.com]
ok: [WIN-33RUVE5NKBH.example.com -> v3-re-docker-test.example.com]

TASK [community.zabbix.zabbix_agent : API | Updating host configuration with macros] *****************************************************************************************************************************************************************************************************
skipping: [WIN-23H3PDJ0CRT.example.com]
skipping: [WIN-QVJ14UN8PL2.example.com]
skipping: [WIN-33RUVE5NKBH.example.com]

TASK [community.zabbix.zabbix_agent : Including userparameters] **************************************************************************************************************************************************************************************************************************
skipping: [WIN-23H3PDJ0CRT.example.com]
skipping: [WIN-QVJ14UN8PL2.example.com]
skipping: [WIN-33RUVE5NKBH.example.com]
emilbasv3 commented 9 months ago

community.zabbix.zabbix_agent role uses delegation https://github.com/ansible-collections/community.zabbix/blob/main/roles/zabbix_agent/tasks/api.yml#L10

delegate_to: "{{ zabbix_api_server_host }}"

zabbix_api_server_host is not a member of your windows host group this is why zabbix_api_server_port is not set correctly for API operations.

So.. is there any way to customize these variables per inventory host?

zabbix_api_server_host zabbix_api_server_port zabbix_api_login_pass zabbix_api_login_user

Other than to have two or more distinct playbooks..