
    Vhb                         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_group
short_description: Create/delete Zabbix host groups
description:
   - Create host groups if they do not exist.
   - Delete existing host groups if they exist.
author:
    - "Cove (@cove)"
    - "Tony Minfei Ding (!UNKNOWN)"
    - "Harrison Gu (@harrisongu)"
requirements:
    - "python >= 2.6"
options:
    state:
        description:
            - Create or delete host group.
        required: false
        type: str
        default: "present"
        choices: [ "present", "absent" ]
    host_groups:
        description:
            - List of host groups to create or delete.
        required: true
        type: list
        elements: str
        aliases: [ "host_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.
ax  
# 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 host groups example
- name: Create host 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_group:
    state: present
    host_groups:
      - Example group1
      - Example group2

# Limit the Zabbix group creations to one host since Zabbix can return an error when doing concurrent updates
- name: Create host 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_group:
    state: present
    host_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)	HostGroupc                    	 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 host group(s): %smsg)
_zapi	hostgroupget_module
check_mode	exit_jsoncreateappend	Exception	fail_json)selfgroup_namesgroup_add_list
group_nameresultes         q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_group.pycreate_host_groupzHostGroup.create_host_groupi   s    	QN) 6
--118fj=Q2RS||....t.<JJ((//0DE"))*56 "! 	QLL""'Ka'O"PP	Q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   z-Failed to delete host group(s), Exception: %sr   )r   r   r   r   r   deleter   r   )r   	group_idsr    s      r!   delete_host_groupzHostGroup.delete_host_groupx   ss    	\||&&&&t&4JJ  ''	2 	\LL""'VYZ'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   host_groupsr%   
group_listgroupgroup_ids         r!   get_group_idszHostGroup.get_group_ids   s`    	ZZ))--fVaMb.cd
 	'EY'HX&	' *$$    N)__name__
__module____qualname__r"   r&   r/    r0   r!   r	   r	   g   s    Q\%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
host_groupstr)typerequiredaliaseselementspresentabsent)r9   defaultchoices)r+   state)argument_specsupports_check_moder+   rA   r   z'Successfully deleted host group(s): %s.,)r   r   FzNo host group(s) to delete.r   z&Successfully created host group(s): %sr   )zabbix_utilszabbix_common_argument_specupdatedictr   paramsr	   r/   r&   r   r   joinr"   len)
rB   moduler+   rA   	hostGroupr%   r,   delete_group_namesr-   r   s
             r!   mainrO      sn    <<>Mftl^V[\y9h:OP  # F
 --.KMM'"E&!IIJ ) 7 7 D	:!#''	2# 9"))%-89T$MPSPXPXYkPl$l  n U3PQ #44[A~"T2Z]k2kl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!%
 !%H&,R zF r0   