
    Vh                         d dl mZmZmZ eZdZdZd dlm	Z	 d dl
mZ d dlmc mc mc mc mZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: zabbix_templategroup
short_description: Create/delete Zabbix template groups
description:
   - Create template groups if they do not exist.
   - Delete existing template groups if they exist.
author:
    - "Cove (@cove)"
    - "Tony Minfei Ding (!UNKNOWN)"
    - "Harrison Gu (@harrisongu)"
requirements:
    - "python >= 2.6"
options:
    state:
        description:
            - Create or delete template group.
        required: false
        type: str
        default: "present"
        choices: [ "present", "absent" ]
    template_groups:
        description:
            - List of template groups to create or delete.
        required: true
        type: list
        elements: str
        aliases: [ "template_group" ]

extends_documentation_fragment:
- community.zabbix.zabbix

notes:
    - Too many concurrent updates to the same group may cause Zabbix to return errors, see examples for a workaround if needed.
a  
# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

# Base create template groups example
- name: Create template groups
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_templategroup:
    state: present
    template_groups:
      - Example group1
      - Example group2

# Limit the Zabbix group creations to one template since Zabbix can return an error when doing concurrent updates
- name: Create template groups
  # set task level variables as we change ansible_connection plugin here
  vars:
      ansible_network_os: community.zabbix.zabbix
      ansible_connection: httpapi
      ansible_httpapi_port: 443
      ansible_httpapi_use_ssl: true
      ansible_httpapi_validate_certs: false
      ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
      ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_templategroup:
    state: present
    template_groups:
      - Example group1
      - Example group2
  when: inventory_hostname==groups['group_name'][0]
)AnsibleModule)
ZabbixBaseNc                       e Zd Zd Zd Zd Zy)TemplateGroupc                    	 g }|D ]  }| j                   j                  j                  dd|ii      }|r/| j                  j                  r| j                  j                  d       | j                   j                  j                  d|i       |j                  |        |S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)NfilternameTchangedz&Failed to create template group(s): %smsg)
_zapitemplategroupget_module
check_mode	exit_jsoncreateappend	Exception	fail_json)selfgroup_namesgroup_add_list
group_nameresultes         y/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_templategroup.pycreate_template_groupz#TemplateGroup.create_template_grouph   s    	UN) 6
1155x&*AU6VW||....t.<JJ,,33VZ4HI"))*56 "! 	ULL""'ORS'S"TT	Us   2B# A-B# #	C,CCc                    	 | j                   j                  r| j                   j                  d       | j                  j                  j                  |       y # t        $ r)}| j                   j                  d|z         Y d }~y d }~ww xY w)NTr   z1Failed to delete template group(s), Exception: %sr   )r   r   r   r   r   deleter   r   )r   	group_idsr    s      r!   delete_template_groupz#TemplateGroup.delete_template_groupw   ss    	`||&&&&t&4JJ$$++I6 	`LL""'Z]^'^"__	`s   AA 	B#BBc                     g }| j                   j                  j                  dd|id      }|D ]  }|d   }|j                  |        ||fS )Nextendr   )outputr   groupid)r   r   r   r   )r   template_groupsr%   
group_listgroupgroup_ids         r!   get_group_idszTemplateGroup.get_group_ids   sa    	ZZ--11XRXZiQj2kl
 	'EY'HX&	' *$$    N)__name__
__module____qualname__r"   r&   r/    r0   r!   r	   r	   f   s    U`%r0   r	   c                     t        j                         } | j                  t        t        dddgd      t        ddddg      	             t	        | d
      }|j
                  d   }|j
                  d   }t        |      }g }g }|r|j                  |      \  }}|dk(  rj|rTg }|j                  |       |D ]  }|j                  |d           |j                  dddj                  |      z         y |j                  dd       y |j                  |      }	t        |	      dkD  r|j                  dd|	z         y |j                  d       y )NlistTtemplate_groupstr)typerequiredaliaseselementspresentabsent)r9   defaultchoices)r+   state)argument_specsupports_check_moder+   rA   r   z+Successfully deleted template group(s): %s.,)r   r   FzNo template group(s) to delete.r   z*Successfully created template group(s): %sr   )zabbix_utilszabbix_common_argument_specupdatedictr   paramsr	   r/   r&   r   r   joinr"   len)
rB   moduler+   rA   templateGroupr%   r,   delete_group_namesr-   r   s
             r!   mainrO      sq    <<>M&4BRAS^cdy9h:OP  # F
 mm$56OMM'"E!&)MIJ - ; ;O L	:!#//	:# 9"))%-89T$QTWT\T\]oTp$p  r U3TU '<<_M~"T2^ao2opU+r0   __main__)
__future__r   r   r   r9   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersrE   r	   rO   r1   r4   r0   r!   <module>r]      s[    A @"H/d 5 U X X X!%J !%H&,R zF r0   