robertdebock / ansible-role-zabbix_agent

Install and configure zabbix_agent on your system.
https://robertdebock.nl/
Apache License 2.0
9 stars 2 forks source link

Error starting zabbix-agent if zabbix-agent is already installed #3

Open anutator opened 3 years ago

anutator commented 3 years ago

Your role doesn't work correctly on Debian (I think the same sutiation is on othe Linux distributions) if we have old version of zabbix-agent.

Please change present to latest in tasks.

- name: install zabbix-agent software
  ansible.builtin.package:
    name: zabbix-agent
    state: present

Add step to disable zabbix-agent before upgrading it because if it is not disabled you will have an error while upgrading (service can't start with old config). I suppose that we have errors when upgrade major version (from 4.x.x. to 5.x.x).

- name: install or upgrade zabbix-agent software
  block:
    - name: install latest zabbix-agent
      ansible.builtin.package:
        name: zabbix-agent
        state: latest
  rescue:
    - name: disable zabbix-agent service before upgrade
      ansible.builtin.service:
        name: "{{ zabbix_agent_service }}"
        state: stopped
        enabled: no
    - name: reinstall latest zabbix-agent after previous error
      ansible.builtin.package:
        name: zabbix-agent
        state: latest

This is output if upgrading zabbix failed: fatal: [ss7]: FAILED! => {"cache_update_time": 1628173320, "cache_updated": false, "changed": false, "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-confold\" install 'zabbix-agent'' failed: E: Sub-process /usr/bin/dpkg returned an error code (1)\n", "rc": 100, "stderr": "E: Sub-process /usr/bin/dpkg returned an error code (1)\n", "stderr_lines": ["E: Sub-process /usr/bin/dpkg returned an error code (1)"], "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nzabbix-agent is already the newest version (1:5.4.3-1+debian10).\nThe following packages were automatically installed and are no longer required:\n usb.ids usbutils\nUse 'sudo apt autoremove' to remove them.\n0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.\n1 not fully installed or removed.\nAfter this operation, 0 B of additional disk space will be used.\nSetting up zabbix-agent (1:5.4.3-1+debian10) ...\r\nJob for zabbix-agent.service failed because the control process exited with error code.\r\nSee \"systemctl status zabbix-agent.service\" and \"journalctl -xe\" for details.\r\ninvoke-rc.d: initscript zabbix-agent, action \"restart\" failed.\r\n* zabbix-agent.service - Zabbix Agent\r\n Loaded: loaded (\u001b]8;;file://prod-storage1/lib/systemd/system/zabbix-agent.service\u0007/lib/systemd/system/zabbix-agent.service\u001b]8;;\u0007; enabled; vendor preset: enabled)\r\n Active: activating (auto-restart) (Result: exit-code) since Thu 2021-08-05 21:02:27 MSK; 5ms ago\r\n Process: 478 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE \u001b[0;1;31m(code=exited, status=1/FAILURE)\u001b[0m\r\ndpkg: error processing package zabbix-agent (--configure):\r\n installed zabbix-agent package post-installation script subprocess returned error exit status 1\r\nErrors were encountered while processing:\r\n zabbix-agent\r\n", "stdout_lines": ["Reading package lists...", "Building dependency tree...", "Reading state information...", "zabbix-agent is already the newest version (1:5.4.3-1+debian10).", "The following packages were automatically installed and are no longer required:", " usb.ids usbutils", "Use 'sudo apt autoremove' to remove them.", "0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.", "1 not fully installed or removed.", "After this operation, 0 B of additional disk space will be used.", "Setting up zabbix-agent (1:5.4.3-1+debian10) ...", "Job for zabbix-agent.service failed because the control process exited with error code.", "See \"systemctl status zabbix-agent.service\" and \"journalctl -xe\" for details.", "invoke-rc.d: initscript zabbix-agent, action \"restart\" failed.", "* zabbix-agent.service - Zabbix Agent", " Loaded: loaded (\u001b]8;;file://prod-storage1/lib/systemd/system/zabbix-agent.service\u0007/lib/systemd/system/zabbix-agent.service\u001b]8;;\u0007; enabled; vendor preset: enabled)", " Active: activating (auto-restart) (Result: exit-code) since Thu 2021-08-05 21:02:27 MSK; 5ms ago", " Process: 478 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE \u001b[0;1;31m(code=exited, status=1/FAILURE)\u001b[0m", "dpkg: error processing package zabbix-agent (--configure):", " installed zabbix-agent package post-installation script subprocess returned error exit status 1", "Errors were encountered while processing:", " zabbix-agent"]}

With state: present if we have old zabbix-agent (for example 4.0.4), the package is not updated:

$ sudo dpkg-query -l "*zabbix*"
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version          Architecture Description
+++-==============-================-============-========================================
ii  zabbix-agent   1:4.0.4+dfsg-1   amd64        network monitoring solution - agent
ii  zabbix-release 1:5.4-1+debian10 all          Zabbix official repository configuration

But in your zabbix-agentd.conf template you have option:

AllowKey=system.run[*]

This option is not supported in old zabbix-agent versions so zabbix-agent can't restart:

Aug 05 18:54:16 server-name zabbix_agentd[32444]: zabbix_agentd [32444]: unknown parameter "AllowKey" in config file "/etc/zabbix/zabbix_agentd.conf", line 68