Ad Widget

Collapse

Issue with "host.update" in Zabbix API - templates_clear removes all templates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Giacomo_Boria
    Junior Member
    • Jan 2025
    • 2

    #1

    Issue with "host.update" in Zabbix API - templates_clear removes all templates

    Hello,
    I’m new to the forum, and I’d like to thank you in advance for your support.

    On my Zabbix server (version 6.0 LTS), I need to modify several hosts to replace an existing template with a new/different one.

    Given the large number of hosts and the need to make additional changes, I decided to use a Python script. I based my script on the official documentation, specifically this page:


    Problem:
    When I execute the host.update request with the templates_clear parameter to unlink and clear a specific template, all templates associated with the host are removed, not just the one specified.

    Here’s the code I’m using:
    Code:
    def remove_template(auth_result, hostid, templateid_to_change):
        auth_token = auth_result["result"]
    
        host_update = {
            "jsonrpc": "2.0",
            "method": "host.update",
            "params": {
                "hostid": hostid,
                "templates_clear": [
                    {
                        "templateid": templateid_to_change
                    }
                ]
            },
            "auth": auth_token,
            "id": 1
        }
    
        response = requests.post(zabbix_api_url, data=json.dumps(host_update), headers=headers, verify=False)
        create_result = response.json()
    
        return create_result
    ​
    Question:
    Has anyone encountered a similar issue? Am I using the API correctly, or am I missing something?

    Thank you very much for your support!​
  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4807

    #2
    Looks ok...

    tested with following, removed only one specified template...
    Code:
    from zabbix_utils import ZabbixAPI
    zapi = ZabbixAPI(url="your api url")
    zapi.login(token="your access token (generate from UI)")
    
    zapi.host.update(
        hostid=<insert your hosts id here>,
        templates_clear=[
            {
                "templateid": <insert your template id here>
            }
        ]
    )
    using native python library .. https://github.com/zabbix/python-zabbix-utils
    Last edited by cyber; 08-01-2025, 12:42.

    Comment


    • Giacomo_Boria
      Giacomo_Boria commented
      Editing a comment
      Thanks for your response.

      I tried the suggested solution, but unfortunately, the result is still the same: when I run the code, all templates associated with the host are cleared, not just the one specified.

      Here is the updated code I’m using now:

      def remove_template( hostid1, old_template):

      api.host.update(
      hostid=hostid1,
      templates_clear=[
      {
      "templateid": old_template
      }
      ]
      )

      Is this behavior expected? Or is it a bug in the API? If anyone has successfully managed to unlink a single template without affecting others, I’d greatly appreciate any insights.

      Thanks again for your help!
Working...