saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
14.17k stars 5.48k forks source link

[BUG]salt.states.zabbix_host: KeyError: 'bulk' #60379

Closed colttt closed 3 years ago

colttt commented 3 years ago

Description If a host in Zabbix exist, I got the following error:

salt-minion-1:
----------
          ID: create_host_to_zabbix
    Function: zabbix_host.present
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call
                  *cdata["args"], **cdata["kwargs"]
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/zabbix_host.py", line 260, in present
                  hostintf.pop("bulk")
              KeyError: 'bulk'
     Started: 11:20:28.568907
    Duration: 968.72 ms
     Changes:  

strange: on another host salt-minion-2 it works fine without an error

Setup zabbix_add_host.sls

create_host_to_zabbix:
  zabbix_host.present:
    - host: {{ grains['host'] }}
    - groups:
      - 2
    - interfaces:
      - {{ grains['fqdn'] }}:
        - ip: {{ grains['ipv4'][1] }}
        - type: agent
        - port: 10050
        - main: true
        - useip: true
    - templates:
      - "Template OS Linux"
    #    - require: 
      #      - pkg: zabbix-agent
    - _connection_user: ***
    - _connection_password: ****
    - _connection_url: https://monitoring.*****/api_jsonrpc.php

Steps to Reproduce the behavior here is the complete debug output:

salt 'salt-minion-1.****' state.apply linux.zabbix-agent.add_host -l debug --state-output=full --state-verbose=True
[DEBUG   ] Reading configuration from /etc/salt/master
[DEBUG   ] Including configuration from '/etc/salt/master.d/gitfs.conf'
[DEBUG   ] Reading configuration from /etc/salt/master.d/gitfs.conf
[DEBUG   ] Changed git to gitfs in master opts' fileserver_backend list
[DEBUG   ] Using cached minion ID from /etc/salt/minion_id: salt-test.****
[DEBUG   ] Missing configuration file: /root/.saltrc
[DEBUG   ] Using pkg_resources to load entry points
[DEBUG   ] Override  __grains__: <module 'salt.loaded.int.log_handlers.sentry_mod' from '/usr/lib/python3/dist-packages/salt/log/handlers/sentry_mod.py'>
[DEBUG   ] Configuration file path: /etc/salt/master
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG   ] Reading configuration from /etc/salt/master
[DEBUG   ] Including configuration from '/etc/salt/master.d/gitfs.conf'
[DEBUG   ] Reading configuration from /etc/salt/master.d/gitfs.conf
[DEBUG   ] Changed git to gitfs in master opts' fileserver_backend list
[DEBUG   ] Using cached minion ID from /etc/salt/minion_id: salt-test.****
[DEBUG   ] Missing configuration file: /root/.saltrc
[DEBUG   ] MasterEvent PUB socket URI: /var/run/salt/master/master_event_pub.ipc
[DEBUG   ] MasterEvent PULL socket URI: /var/run/salt/master/master_event_pull.ipc
[DEBUG   ] Using pkg_resources to load entry points
[DEBUG   ] Using pkg_resources to load entry points
[DEBUG   ] Using pkg_resources to load entry points
[DEBUG   ] Popen(['git', 'version'], cwd=/root, universal_newlines=False, shell=None)
[DEBUG   ] Popen(['git', 'version'], cwd=/root, universal_newlines=False, shell=None)
[DEBUG   ] Initializing new AsyncZeroMQReqChannel for ('/etc/salt/pki/master', 'salt-test.****_master', 'tcp://127.0.0.1:4506', 'clear')
[DEBUG   ] Connecting the Minion to the Master URI (for the return server): tcp://127.0.0.1:4506
[DEBUG   ] Trying to connect to: tcp://127.0.0.1:4506
[DEBUG   ] Closing AsyncZeroMQReqChannel instance
[DEBUG   ] LazyLoaded local_cache.get_load
[DEBUG   ] Reading minion list from /var/cache/salt/master/jobs/52/d81c4948166ab03257c2a3f8cc475dd256da99662ff8547fa6f6b643d37cba/.minions.p
[DEBUG   ] get_iter_returns for jid 20210616092635732114 sent to {'salt-minion-1.****'} will timeout at 11:26:40.741902
[DEBUG   ] jid 20210616092635732114 return from salt-minion-1.****
[DEBUG   ] return event: {'salt-minion-1.****': {'ret': {'zabbix_host_|-create_host_to_zabbix_|-create_host_to_zabbix_|-present': {'result': False, 'name': 'salt-minion-1', 'changes': {}, 'comment': 'An exception occurred in this state: Traceback (most recent call last):\n  File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call\n    *cdata["args"], **cdata["kwargs"]\n  File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper\n    return f(*args, **kwargs)\n  File "/usr/lib/python3/dist-packages/salt/states/zabbix_host.py", line 260, in present\n    hostintf.pop("bulk")\nKeyError: \'bulk\'\n', '__sls__': 'linux.zabbix-agent.add_host', '__run_num__': 0, 'start_time': '11:26:36.311764', 'duration': 1357.149, '__id__': 'create_host_to_zabbix'}}, 'out': 'highstate', 'retcode': 2, 'jid': '20210616092635732114'}}
[DEBUG   ] Using pkg_resources to load entry points
[DEBUG   ] LazyLoaded highstate.output
salt-minion-1.****:
----------
          ID: create_host_to_zabbix
    Function: zabbix_host.present
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call
                  *cdata["args"], **cdata["kwargs"]
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/zabbix_host.py", line 260, in present
                  hostintf.pop("bulk")
              KeyError: 'bulk'
     Started: 11:26:36.311764
    Duration: 1357.149 ms
     Changes:   

Summary for salt-minion-1.****
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   1.357 s
[DEBUG   ] jid 20210616092635732114 found all minions {'salt-minion-1.****'}
[DEBUG   ] Closing IPCMessageSubscriber instance
ERROR: Minions returned with non-zero exit code

Expected behavior no error, just a message that the host already exist

Versions Report

Saltminion: ```salt-minion -V Salt Version: Salt: 3002.5 Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: 2.7.3 docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed Jinja2: 2.10 libgit2: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack: 0.5.6 msgpack-pure: Not Installed mysql-python: Not Installed pycparser: Not Installed pycrypto: Not Installed pycryptodome: 3.6.1 pygit2: Not Installed Python: 3.7.3 (default, Jul 25 2020, 13:03:44) python-gnupg: Not Installed PyYAML: 3.13 PyZMQ: 17.1.2 smmap: Not Installed timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.1 System Versions: dist: debian 10 buster locale: UTF-8 machine: x86_64 release: 4.19.0-14-amd64 system: Linux version: Debian GNU/Linux 10 buster ``` Saltmaster: ``` salt --versions-report Salt Version: Salt: 3003 Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: 2.7.3 docker-py: Not Installed gitdb: 2.0.5 gitpython: 2.1.11 Jinja2: 2.10 libgit2: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack: 0.5.6 msgpack-pure: Not Installed mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: 3.6.1 pygit2: Not Installed Python: 3.7.3 (default, Jan 22 2021, 20:04:44) python-gnupg: Not Installed PyYAML: 3.13 PyZMQ: 17.1.2 smmap: 2.0.5 timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.1 System Versions: dist: debian 10 buster locale: UTF-8 machine: x86_64 release: 4.19.0-16-amd64 system: Linux version: Debian GNU/Linux 10 buster ```

Additional context It was also strange that I got a different error, and with no changes it works from one minute to the other..

salt-minion-1.***:
----------
          ID: create_host_to_zabbix
    Function: zabbix_host.present
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call
                  *cdata["args"], **cdata["kwargs"]
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/zabbix_host.py", line 375, in present
                  **connection_args
                File "/usr/lib/python3/dist-packages/salt/modules/zabbix.py", line 1176, in host_create
                  ret = _query(method, params, conn_args["url"], conn_args["auth"])
                File "/usr/lib/python3/dist-packages/salt/modules/zabbix.py", line 192, in _query                                                                                                                                            
                  ret["error"]["message"], ret["error"]["data"]                                                                                                                                                                              
              salt.exceptions.SaltException: Zabbix API: Application error. (No permissions to referred object or it does not exist!)                                                                                                        
     Started: 10:50:34.410166
    Duration: 607.857 ms
     Changes:
OrangeDog commented 3 years ago

Are the minions on different versions? I see the failing one is older than the master. zabbix_host got various fixes in 3003.

piterpunk commented 3 years ago

Are the minions on different versions? I see the failing one is older than the master. zabbix_host got various fixes in 3003.

Yes, this specific error:

          ID: create_host_to_zabbix
    Function: zabbix_host.present
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call
                  *cdata["args"], **cdata["kwargs"]
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/zabbix_host.py", line 260, in present
                  hostintf.pop("bulk")
              KeyError: 'bulk'
     Started: 11:26:36.311764
    Duration: 1357.149 ms
     Changes: 

Is fixed in 3003 version, it now checks if the bulk element is present before "pop" it:

318                 if "bulk" in hostintf:
319                     hostintf.pop("bulk")

The line numbers are a bit off because 3003 have a lot of changes.

colttt commented 3 years ago

updated to the latest version 3003, not it works fine