ansible-collections / community.zabbix

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

Unable to import template #277

Closed JG127 closed 3 years ago

JG127 commented 3 years ago
SUMMARY

Can not import a template with the zabbix_template module.

ISSUE TYPE
COMPONENT NAME

zabbix_template module

ANSIBLE VERSION
ansible 2.9.15
  config file = None
  configured module search path = ['/home/foo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/foo/Projects/bar/venv/lib/python3.8/site-packages/ansible
  executable location = /home/foo/Projects/playbooks_bar/venv/bin/ansible
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
CONFIGURATION
output of ansible-config dump --only-changed command is blank
OS / ENVIRONMENT / Zabbix Version

Linux amd64 / Mint 20 Zabbix 5.2.2 from https://github.com/zabbix/zabbix-docker docker compose file docker-compose_v3_alpine_pgsql_latest.yaml Docker CE 19.03.14, build 5eb3275d40 docker-compose version 1.24.1, build 4667896b Python library zabbix-api 0.5.4

STEPS TO REPRODUCE

The command: ansible-playbook -vvv template.yml

The playbook:

---

- hosts: localhost
  gather_facts: false
  connection: local
  vars:
    zabbix_api_url: "http://localhost:8081"
    zabbix_api_username: Admin
    zabbix_api_password: zabbix
  tasks:
    - name: Create template 
      local_action:
        module: zabbix_template
        server_url: "{{ zabbix_api_url }}"
        login_user: "{{ zabbix_api_username }}"
        login_password: "{{ zabbix_api_password }}"
        state: present
        template_json: "{{ lookup('file', '/tmp/template.json') }}"
...

The template json in /tmp/template.json:

{
   "zabbix_export":{
      "date":"2017-11-29T16:37:24Z",
      "templates":[
         {
            "templates":[

            ],
            "description":"",
            "httptests":[

            ],
            "screens":[

            ],
            "applications":[

            ],
            "discovery_rules":[

            ],
            "groups":[
               {
                  "name":"Templates"
               }
            ],
            "name":"Test Template",
            "items":[

            ],
            "macros":[

            ],
            "template":"test"
         }
      ],
      "version":"3.2",
      "groups":[
         {
            "name":"Templates"
         }
      ]
   }
}
EXPECTED RESULTS

This should have succeeded.

ACTUAL RESULTS

When running the playbook the error message as shown below. I'm getting this very same message for anything I'm trying to do with templates.

$ ansible-playbook -vvv template.yml 
ansible-playbook 2.9.15
  config file = None
  configured module search path = ['/home/foo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/ansible
  executable location = /home/foo/Projects/playbooks_bar/venv/bin/ansible-playbook
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
No config file found; using defaults
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAYBOOK: template.yml ********************************************************************************************************************************************
1 plays in template.yml

PLAY [localhost] *****************************************************************************************************************************************************
META: ran handlers

TASK [Create template] *************************************************************************************************************************************
task path: /home/foo/Projects/playbooks_bar/assets/playbooks/zabbix/template.yml:15
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: foo
<localhost> EXEC /bin/sh -c 'echo ~foo && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/foo/.ansible/tmp `"&& mkdir "` echo /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535 `" && echo ansible-tmp-1607014738.8149276-158602-46729370034535="` echo /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535 `" ) && sleep 0'
Using module file /home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/ansible/modules/monitoring/zabbix/zabbix_template.py
<localhost> PUT /home/foo/.ansible/tmp/ansible-local-158596jhs9rn_e/tmpt6s4g7vw TO /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535/AnsiballZ_zabbix_template.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535/ /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535/AnsiballZ_zabbix_template.py && sleep 0'
<localhost> EXEC /bin/sh -c '/home/foo/Projects/playbooks_bar/venv/bin/python /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535/AnsiballZ_zabbix_template.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/foo/.ansible/tmp/ansible-tmp-1607014738.8149276-158602-46729370034535/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_********_template_payload_x_t15sn6/ansible_********_template_payload.zip/ansible/modules/monitoring/********/********_template.py", line 599, in import_template
  File "/home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/********_api.py", line 341, in method
    return self.universal("%s.%s" % (self.data["prefix"], name), opts[0])
  File "/home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/********_api.py", line 79, in wrapper
    return self.do_request(self.json_obj(method, opts))['result']
  File "/home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/********_api.py", line 348, in do_request
    return self.parent.do_request(req)
  File "/home/foo/Projects/playbooks_bar/venv/lib/python3.8/site-packages/********_api.py", line 299, in do_request
    raise ZabbixAPIException(msg, jobj['error']['code'])
********_api.ZabbixAPIException: ('Error -32602: Invalid params., Invalid parameter "/rules": unexpected parameter "templateScreens". while sending {"jsonrpc": "2.0", "method": "configuration.import", "params": {"format": "json", "source": "{\\"********_export\\": {\\"date\\": \\"2017-11-29T16:37:24Z\\", \\"templates\\": [{\\"templates\\": [], \\"description\\": \\"\\", \\"httptests\\": [], \\"screens\\": [], \\"applications\\": [], \\"discovery_rules\\": [], \\"groups\\": [{\\"name\\": \\"Templates\\"}], \\"name\\": \\"Test Template\\", \\"items\\": [], \\"macros\\": [], \\"template\\": \\"test\\"}], \\"version\\": \\"3.2\\", \\"groups\\": [{\\"name\\": \\"Templates\\"}]}}", "rules": {"applications": {"createMissing": true, "deleteMissing": true}, "discoveryRules": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "graphs": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "httptests": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "items": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "templates": {"createMissing": true, "updateExisting": true}, "templateLinkage": {"createMissing": true}, "templateScreens": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "triggers": {"createMissing": true, "updateExisting": true, "deleteMissing": true}, "valueMaps": {"createMissing": true, "updateExisting": true}}}, "auth": "aa140eef94c69a0f320fd7bdeeafc2b6", "id": 3}', -32602)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "details": "('Error -32602: Invalid params., Invalid parameter \"/rules\": unexpected parameter \"templateScreens\". while sending {\"jsonrpc\": \"2.0\", \"method\": \"configuration.import\", \"params\": {\"format\": \"json\", \"source\": \"{\\\\\"********_export\\\\\": {\\\\\"date\\\\\": \\\\\"2017-11-29T16:37:24Z\\\\\", \\\\\"templates\\\\\": [{\\\\\"templates\\\\\": [], \\\\\"description\\\\\": \\\\\"\\\\\", \\\\\"httptests\\\\\": [], \\\\\"screens\\\\\": [], \\\\\"applications\\\\\": [], \\\\\"discovery_rules\\\\\": [], \\\\\"groups\\\\\": [{\\\\\"name\\\\\": \\\\\"Templates\\\\\"}], \\\\\"name\\\\\": \\\\\"Test Template\\\\\", \\\\\"items\\\\\": [], \\\\\"macros\\\\\": [], \\\\\"template\\\\\": \\\\\"test\\\\\"}], \\\\\"version\\\\\": \\\\\"3.2\\\\\", \\\\\"groups\\\\\": [{\\\\\"name\\\\\": \\\\\"Templates\\\\\"}]}}\", \"rules\": {\"applications\": {\"createMissing\": true, \"deleteMissing\": true}, \"discoveryRules\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"graphs\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"httptests\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"items\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"templates\": {\"createMissing\": true, \"updateExisting\": true}, \"templateLinkage\": {\"createMissing\": true}, \"templateScreens\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"triggers\": {\"createMissing\": true, \"updateExisting\": true, \"deleteMissing\": true}, \"valueMaps\": {\"createMissing\": true, \"updateExisting\": true}}}, \"auth\": \"aa140eef94c69a0f320fd7bdeeafc2b6\", \"id\": 3}', -32602)",
    "invocation": {
        "module_args": {
            "clear_templates": null,
            "dump_format": "json",
            "http_login_password": null,
            "http_login_user": null,
            "link_templates": null,
            "login_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "login_user": "Admin",
            "macros": null,
            "server_url": "http://localhost:8081",
            "state": "present",
            "template_groups": null,
            "template_json": "{\"********_export\": {\"date\": \"2017-11-29T16:37:24Z\", \"templates\": [{\"templates\": [], \"description\": \"\", \"httptests\": [], \"screens\": [], \"applications\": [], \"discovery_rules\": [], \"groups\": [{\"name\": \"Templates\"}], \"name\": \"Test Template\", \"items\": [], \"macros\": [], \"template\": \"test\"}], \"version\": \"3.2\", \"groups\": [{\"name\": \"Templates\"}]}}",
            "template_name": null,
            "template_xml": null,
            "timeout": 10,
            "validate_certs": true
        }
    },
    "msg": "Unable to import template"
}
JG127 commented 3 years ago

Nobody have had this issue ?

sylflo commented 3 years ago

I have the exact same problem. To bypass it, I tried to call the JSON api of Zabbix directly, like so

 r = requests.post(
                    f'{host}/api_jsonrpc.php',
                    json={
                        "jsonrpc": "2.0",
                        "method": "configuration.import",
                        "auth": token,
                        "id": 1,
                        "params": {
                            "format": "xml",
                            "source": content,
                            "rules": {
                                "groups": {
                                    "createMissing": True,
                                },
                                "templates": {
                                    "createMissing": True,
                                    "updateExisting": True,
                                },
                                "templateLinkage": {
                                    "createMissing": True,
                                },
                            },
                        },
                    },
                )

So it's working however if I start to add some rules like triggers, alerts, ect.... I cannot import my templates at all

D3DeFi commented 3 years ago

Zabbix 5.2 has received and undocumented change (not in Zabbix 5.2 API changes) to rules. Rule templateScreens was renamed to templateDashboards. Fix for this issue was presented in #262 and works when running latest version of this collection from the source.

However, it was not yet released to galaxy.ansible.com and latest 1.1.0 is considered affected by this "change". We will prepare 1.2.0 release of this collection soon so it should work with next ansible release.

I was now also able to reproduce load order of collections. Ansible 2.9 seems to be ignoring collections/ansible_collections/ directory placed to the project root. More info in https://github.com/ansible-collections/community.zabbix/issues/286#issuecomment-751219128

D3DeFi commented 3 years ago

@JG127 with 1.2.0 out, are you still seeing this problem?

D3DeFi commented 3 years ago

this issue is dormant and we are not experiencing the reported problems with Zabbix server 5.2 (Zabbix server 5.4 is a different story and has its own issues opened in this repo), thus I am closing this.

Please feel free to reopen if you are still experiencing the problems with 5.2