
    VhC                         d dl mZmZmZ eZdZdZd dlZd dl	m
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d Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona5  
---
module: zabbix_host
short_description: Create/update/delete Zabbix hosts
description:
   - This module allows you to create, modify and delete Zabbix host entries and associated group and template data.
author:
    - "Cove (@cove)"
    - Tony Minfei Ding (!UNKNOWN)
    - Harrison Gu (@harrisongu)
    - Werner Dijkerman (@dj-wasabi)
    - Eike Frost (@eikef)
requirements:
    - "python >= 3.9"
options:
    host_name:
        description:
            - Name of the host in Zabbix.
            - I(host_name) is the unique identifier used and cannot be updated using this module.
        required: true
        type: str
    visible_name:
        description:
            - Visible name of the host in Zabbix.
        type: str
    description:
        description:
            - Description of the host in Zabbix.
        type: str
    host_groups:
        description:
            - List of host groups the host is part of.
            - Make sure the Zabbix user used for Ansible can read these groups.
        type: list
        elements: str
    link_templates:
        description:
            - List of templates linked to the host.
        type: list
        elements: str
    inventory_mode:
        description:
            - Configure the inventory mode.
        choices: ["automatic", "manual", "disabled"]
        type: str
    inventory_zabbix:
        description:
            - Add Facts for a zabbix inventory (e.g. Tag) (see example below).
            - Please review the interface documentation for more information on the supported properties
            - U(https://www.zabbix.com/documentation/current/en/manual/api/reference/host/object#host-inventory)
        type: dict
    status:
        description:
            - Monitoring status of the host.
        choices: ["enabled", "disabled"]
        default: "enabled"
        type: str
    state:
        description:
            - State of the host.
            - On C(present), it will create if host does not exist or update the host if the associated data is different.
            - On C(absent) will remove a host if it exists.
        choices: ["present", "absent"]
        default: "present"
        type: str
    monitored_by:
        description:
           - Parameter introduced in Zabbix 7.0.
           - Source that is used to monitor the host.
        choices: ["zabbix_server", "proxy", "proxy_group"]
        type: str
    proxy:
        description:
            - The name of the Zabbix proxy to be used.
            - Required if C(monitored_by) is "proxy"
        type: str
    proxy_group:
        description:
            - Parameter introduced in Zabbix 7.0.
            - Proxy group that is used to monitor the host.
            - Required if C(monitored_by) is "proxy_group"
        type: str
    interfaces:
        type: list
        elements: dict
        description:
            - List of interfaces to be created for the host (see example below).
            - For more information, review host interface documentation at
            - U(https://www.zabbix.com/documentation/current/en/manual/api/reference/hostinterface/object#host-interface)
        default: []
        suboptions:
            type:
                type: str
                description:
                    - Interface type to add
                    - Numerical values are also accepted for interface type
                    - 1 = agent
                    - 2 = snmp
                    - 3 = ipmi
                    - 4 = jmx
                choices: ["agent", "1", "snmp", "2", "ipmi", "3", "jmx", "4"]
                required: true
            main:
                type: int
                description:
                    - Whether the interface is used as default.
                    - If multiple interfaces with the same type are provided, only one can be default.
                    - 0 (not default), 1 (default)
                default: 0
                choices: [0, 1]
            useip:
                type: int
                description:
                    - Connect to host interface with IP address instead of DNS name.
                    - 0 (don't use ip), 1 (use ip)
                default: 0
                choices: [0, 1]
            ip:
                type: str
                description:
                    - IP address used by host interface.
                    - Required if I(useip=1).
            dns:
                type: str
                description:
                    - DNS name of the host interface.
                    - Required if I(useip=0).
            port:
                type: str
                description:
                    - Port used by host interface.
                    - If not specified, default port for each type of interface is used
                    - 10050 if I(type="agent")
                    - 161 if I(type="snmp")
                    - 623 if I(type="ipmi")
                    - 12345 if I(type="jmx")
            details:
                type: dict
                description:
                    - Additional details for SNMP host interfaces.
                    - Required when I(type="snmp").
                default: {}
                suboptions:
                    version:
                        type: int
                        description:
                            - SNMP version.
                            - 1 (SNMPv1), 2 (SNMPv2c), 3 (SNMPv3)
                        choices: [1, 2, 3]
                        default: 2
                    bulk:
                        type: int
                        description:
                            - Whether to use bulk SNMP requests.
                            - 0 (don't use bulk requests), 1 (use bulk requests)
                        choices: [0, 1]
                        default: 1
                    community:
                        type: str
                        description:
                            - SNMPv1 and SNMPv2 community string.
                            - Required when I(version=1) or I(version=2).
                        default: ""
                    securityname:
                        type: str
                        description:
                            - SNMPv3 security name.
                        default: ""
                    contextname:
                        type: str
                        description:
                            - SNMPv3 context name.
                        default: ""
                    securitylevel:
                        type: int
                        description:
                            - SNMPv3 security level.
                            - 0 (noAuthNoPriv), 1 (authNoPriv), 2 (authPriv).
                        choices: [0, 1, 2]
                        default: 0
                    authprotocol:
                        type: int
                        description:
                            - SNMPv3 authentication protocol.
                            - Used when I(securitylevel=1)(authNoPriv) or I(securitylevel=2)(AuthPriv).
                            - Variants 2,3,4,5 are supported only on Zabbix 5.4 or greater
                            - 0 (MD5), 1 (SHA1), 2 (SHA224), 3 (SHA256), 4 (SHA384), 5 (SHA512)
                        default: 0
                        choices: [0, 1, 2, 3, 4, 5]
                    authpassphrase:
                        type: str
                        description:
                            - SNMPv3 authentication passphrase.
                            - Used when I(securitylevel=1)(authNoPriv) or I(securitylevel=2)(AuthPriv).
                        default: ""
                    privprotocol:
                        type: int
                        description:
                            - SNMPv3 privacy protocol.
                            - Used when I(securitylevel=2)(authPriv).
                            - Variants 2,3,4,5 are supported only on Zabbix 5.4 or greater
                            - 0 (DES), 1 (AES128), 2 (AES192), 3 (AES256), 4 (AES192C), 5 (AES256C)
                        default: 0
                        choices: [0, 1, 2, 3, 4, 5]
                    privpassphrase:
                        type: str
                        description:
                            - SNMPv3 privacy passphrase.
                            - Used when I(securitylevel=2)(AuthPriv).
                        default: ""
    tls_connect:
        description:
            - Specifies what encryption to use for outgoing connections.
            - Possible values, 1 (no encryption), 2 (PSK), 4 (certificate).
        type: int
    tls_accept:
        description:
            - Specifies what types of connections are allowed for incoming connections.
            - The tls_accept parameter accepts values of 1 to 7
            - Possible values, 1 (no encryption), 2 (PSK), 4 (certificate).
            - Values can be combined.
        type: int
    tls_psk_identity:
        description:
            - It is a unique name by which this specific PSK is referred to by Zabbix components
            - Do not put sensitive information in the PSK identity string, it is transmitted over the network unencrypted.
            - Works only with >= Zabbix 3.0
            - Using this parameter with Zabbix >= 5.4 makes this module non-idempotent.
        type: str
    tls_psk:
        description:
            - PSK value is a hard to guess string of hexadecimal digits.
            - The preshared key, at least 32 hex digits. Required if either I(tls_connect) or I(tls_accept) has PSK enabled.
            - Using this parameter makes this module non-idempotent.
        type: str
    ca_cert:
        description:
            - Required certificate issuer.
        aliases: [ tls_issuer ]
        type: str
    tls_subject:
        description:
            - Required certificate subject.
        type: str
    ipmi_authtype:
        description:
            - IPMI authentication algorithm.
            - Please review the Host object documentation for more information on the supported properties
            - "https://www.zabbix.com/documentation/3.4/manual/api/reference/host/object"
            - Possible values are, C(0) (none), C(1) (MD2), C(2) (MD5), C(4) (straight), C(5) (OEM), C(6) (RMCP+),
              with -1 being the API default.
            - Please note that the Zabbix API will treat absent settings as default when updating
              any of the I(ipmi_)-options; this means that if you attempt to set any of the four
              options individually, the rest will be reset to default values.
        type: int
    ipmi_privilege:
        description:
            - IPMI privilege level.
            - Please review the Host object documentation for more information on the supported properties
            - "https://www.zabbix.com/documentation/3.4/manual/api/reference/host/object"
            - Possible values are C(1) (callback), C(2) (user), C(3) (operator), C(4) (admin), C(5) (OEM), with C(2)
              being the API default.
            - also see the last note in the I(ipmi_authtype) documentation
        type: int
    ipmi_username:
        description:
            - IPMI username.
            - also see the last note in the I(ipmi_authtype) documentation
        type: str
    ipmi_password:
        description:
            - IPMI password.
            - also see the last note in the I(ipmi_authtype) documentation
        type: str
    force:
        description:
            - Overwrite the host configuration, even if already present.
        type: bool
        default: "yes"
    macros:
        description:
            - List of user macros to assign to the zabbix host.
            - Providing I(macros=[]) with I(force=yes) will clean all of the existing user macros from the host.
        type: list
        elements: dict
        suboptions:
            macro:
                description:
                    - Name of the user macro.
                    - Can be in zabbix native format "{$MACRO}" or short format "MACRO".
                type: str
                required: true
            value:
                description:
                    - Value of the user macro.
                type: str
                required: true
            description:
                description:
                    - Description of the user macro.
                type: str
                required: false
                default: ""
            type:
                description:
                    - Type of the macro.
                    - Since value is not returned by API for secret macros, there is no reliable way to
                      detect changes in the content of secret macro value.
                    - To update secret macro value, please update description alongside it so it passes
                      the check.
                choices: [text, secret]
                type: str
                required: false
                default: text
        aliases: [ user_macros ]
    tags:
        description:
            - List of host tags to assign to the zabbix host.
            - Providing I(tags=[]) with I(force=yes) will clean all of the tags from the host.
        type: list
        elements: dict
        suboptions:
            tag:
                description:
                    - Name of the host tag.
                type: str
                required: true
            value:
                description:
                    - Value of the host tag.
                type: str
                default: ""
        aliases: [ host_tags ]

extends_documentation_fragment:
- community.zabbix.zabbix

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

- name: Create a new host or rewrite an existing host's info (Zabbix <= 7.0)
# Set task level following variables for Zabbix Server host in task
  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
  become: false
  delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example
  community.zabbix.zabbix_host:
    host_name: ExampleHost
    visible_name: ExampleName
    description: My ExampleHost Description
    host_groups:
      - Example group1
      - Example group2
    link_templates:
      - Example template1
      - Example template2
    status: enabled
    state: present
    inventory_mode: manual
    inventory_zabbix:
      tag: "{{ your_tag }}"
      alias: "{{ your_alias }}"
      notes: "Special Informations: {{ your_informations | default('None') }}"
      location: "{{ your_location }}"
      site_rack: "{{ your_site_rack }}"
      os: "{{ your_os }}"
      hardware: "{{ your_hardware }}"
    ipmi_authtype: 2
    ipmi_privilege: 4
    ipmi_username: username
    ipmi_password: password
    interfaces:
      - type: 1
        main: 1
        useip: 1
        ip: 10.xx.xx.xx
        dns: ""
        port: "10050"
      - type: 4
        main: 1
        useip: 1
        ip: 10.xx.xx.xx
        dns: ""
        port: "12345"
    proxy: a.zabbix.proxy
    macros:
      - macro: "{$EXAMPLEMACRO}"
        value: ExampleMacroValue
      - macro: EXAMPLEMACRO2
        value: ExampleMacroValue2
        description: Example desc that work only with Zabbix 4.4 and higher
    tags:
      - tag: ExampleHostsTag
      - tag: ExampleHostsTag2
        value: ExampleTagValue

- name: Create a new host or update it - monitored by Zabbix Proxy (Zabbix >= 7.0)
# Set task level following variables for Zabbix Server host in task
  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
  become: false
  delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example
  community.zabbix.zabbix_host:
    host_name: ExampleHost
    host_groups:
      - Example group1
    status: enabled
    state: present
    monitored_by: proxy
    proxy: a.zabbix.proxy

- name: Create a new host or update it - monitored by Zabbix Proxy Group (Zabbix >= 7.0)
# Set task level following variables for Zabbix Server host in task
  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
  become: false
  delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example
  community.zabbix.zabbix_host:
    host_name: ExampleHost
    host_groups:
      - Example group1
    status: enabled
    state: present
    monitored_by: proxy_group
    proxy: a.zabbix.proxy.group

- name: Update an existing host's TLS settings
# Set current task level variables for Zabbix Server host in task
  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 # you can use task level ansible_host or delegate_to like in previous example
  become: false
  community.zabbix.zabbix_host:
    host_name: ExampleHost
    visible_name: ExampleName
    host_groups:
      - Example group1
    tls_psk_identity: test
    tls_connect: 2
    tls_psk: 123456789abcdef123456789abcdef12
    force: false
N)AnsibleModule)
ZabbixBase)LooseVersionc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)Hostc                 X    | j                   j                  j                  dd|ii      }|S )Nfilterhost)_zapir   get)self	host_nameresults      p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_host.pyis_host_existzHost.is_host_exist  s)    $$h0C%DE    c                     |D ]M  }| j                   j                  j                  dd|ii      }|r/| j                  j	                  d|z         O y)Nr   namezHostgroup not found: %smsgT)r   	hostgroupr   _module	fail_json)r   group_names
group_namer   s       r   check_host_group_existzHost.check_host_group_exist  sZ    % 	SJZZ))--x&*9M.NOF&&+Dz+Q&R	S r   c                     g }|t        |      dk(  r|S |D ]t  }| j                  j                  j                  dgd|id      }t        |      dk  r | j                  j                  d|z         \|d   d   }|j                  |       v |S )Nr   
templateidr   outputr      zTemplate not found: %sr   )lenr   templater   r   r   append)r   template_listtemplate_idsr&   template_ids        r   get_template_idszHost.get_template_ids   s     C$6!$;% 	1H JJ//33~Z`bjYk4lmM=!A%&&+Ch+N&O+A.|<##K0	1 r   c                    	 | j                   j                  r| j                   j                  d       ||||d}t        | j                        t        d      k  r|r)||d<   n#|dk(  r|rd|d<   ||d<   n|d	k(  r|r
d	|d<   ||d
<   |r||d<   |r||d<   |	r|	|d<   |
|
|d<   |||d<   |||d<   |||d<   |r||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |||d<   | j
                  j                  j                  |      }t        |      dk\  r|d   d   S y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)NTchanged)r   
interfacesgroupsstatus7.0proxy_hostidr$   monitored_byproxyid   proxy_groupidr   tls_connect
tls_accepttls_psk_identitytls_psk
tls_issuertls_subjectdescriptionipmi_authtypeipmi_privilegeipmi_usernameipmi_passwordmacrostagshostidsr   zFailed to create host : r   )r   
check_mode	exit_jsonr   _zbx_api_versionr   r   creater%   	Exceptionr   )r   r   	group_idsr1   r/   proxy_idvisible_namer>   r8   r9   r:   r;   r<   r=   r?   r@   rA   rB   rC   rD   r4   proxy_group_id
parameters	host_listes                            r   add_hostzHost.add_host  s   1	X||&&&&t&4"+:QZflmJD112\%5HH19J~.1$56
>208
9-!Q&%56
>26D
?3%1
6",7
=)+5
<(+1A
-."(/
9%%+5
<(&,7
=),7
=)(.;
?+)/=
+,(.;
?+(.;
?+!'-
8$%)
6"

..z:I9~" +A.. # 	XLL""TU'V"WW	Xs   D!D% %	E."EEc                    	 | j                   j                  r| j                   j                  d       |r||d}n|||d}t        | j                        t        d      k  r|dk\  rB||d   k7  r:||d<   n4|dk(  r|dk\  r%||d	   k7  r||d	<   n|d
k(  r|dk\  r||d   k7  r||d<   ||d<   |||d   k7  r||d<   |
|
|d   k7  r|
|d<   |||d   k7  r||d<   |r||d<   |r||d<   |||d   k7  r||d<   |||d   k7  r||d<   |	|	|d   k7  r|	|d<   |||d   k7  r||d<   |||d   k7  r||d<   |||d   k7  r||d<   |||d   k7  r||d<   |r||d<   |||d<   |||d<   | j
                  j                  j                  |       y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)NTr-   )hostidr1   )rU   r0   r1   r2   r   r3   r$   r5   r6   r7   r4   r   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r/   rC   rD   zFailed to update host rF   r   )
r   rG   rH   r   rI   r   r   updaterK   r   )r   r   rL   r1   host_idr/   exist_interface_listrM   rN   r>   r8   r9   r:   r;   r<   r=   r?   r@   rA   rB   rC   rD   discovered_hostzabbix_host_objr4   rO   rP   rR   s                               r   update_hostzHost.update_hostC  s}   5	X||&&&&t&4(/6B
 )09PVW
 5 56e9LL A(on6U*U5=
>2#q($Mh/):T.T4<Jy1%**a/NoVeFf4f:HJ71=J~. ,QWAX1X)5Jv&+}?]0]0;J}-*z_\=Z/Z/9J|,#5EJ12,3Jy)*z_\=Z/Z/9J|,+}?]0]0;J}-+}?]0]0;J}-!--?SbCc2c2?J/".>_UeEf3f3AJ/0!--?SbCc2c2?J/!--?SbCc2c2?J//9J|,!'-
8$%)
6"JJOO"":. 	XLL""TU'V"WW	Xs   FF
 
	F?"F::F?c                 &   	 | j                   j                  r| j                   j                  d       | j                  j                  j                  |g       y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)NTr-   zFailed to delete host rF   r   )r   rG   rH   r   r   deleterK   r   )r   rW   r   rR   s       r   delete_hostzHost.delete_host~  sr    	X||&&&&t&4JJOO""G9- 	XLL""TU'V"WW	Xs   AA 	B$"BBc                     dddddgd|gid}| j                   j                  j                  |      }t        |      dk  r | j                  j                  d|z         y |d	   S )
Nextendtagvaluer   )r#   selectInventoryselectMacros
selectTagsr   r$   zHost not found: %sr   r   )r   r   r   r%   r   r   )r   r   paramsrQ   s       r   get_host_by_host_namezHost.get_host_by_host_name  sq    '$ '*
 JJOO''/	y>ALL""';i'G"HQ<r   c                 p   t        | j                        t        d      k  r,| j                  j                  j	                  dd|gid      }n+| j                  j                  j	                  dd|gid      }t        |      dk  r | j                  j                  d|z         y t        |d	   d
         S )Nr2   r`   r   r"   r   r$   zProxy not found: %sr   r   r5   )	r   rI   r   proxyr   r%   r   r   int)r   
proxy_name
proxy_lists      r   get_proxyid_by_proxy_namezHost.get_proxyid_by_proxy_name  s    --.e1DD))--fWaVbMc.deJ))--fWaVbMc.deJz?QLL""'<z'I"Jz!}Y/00r   c                     | j                   j                  j                  dd|id      }t        |      dk  r | j                  j                  d|z         y t        |d   d         S )	Nr`   r   r"   r$   zProxy group not found: %sr   r   r7   )r   
proxygroupr   r%   r   r   rj   )r   proxy_group_nameproxy_group_lists      r   get_proxy_group_id_by_namezHost.get_proxy_group_id_by_name  si    ::0044U[]mTn5op 1$LL""'BEU'U"V'*?;<<r   c                 |    | j                  |      r+| j                  j                  j                  dgd|id      S y )Ngroupidr   r"   )r   r   r   r   )r   r   s     r   get_group_ids_by_group_namesz!Host.get_group_ids_by_group_names  s=    &&{3::''++	{vWbNc,dee 4r   c                 T    | j                   j                  j                  dg|d      S )Nrt   r#   rE   )r   r   r   )r   rW   s     r   get_group_ids_by_host_idzHost.get_group_ids_by_host_id  s$    zz##''I;7(STTr   c                     g }| j                   j                  j                  d|d      }|D ]  }|j                  |d           |S )Nr`   rw   r!   )r   r&   r   r'   )r   rW   r)   r(   r&   s        r   get_host_templates_by_host_idz"Host.get_host_templates_by_host_id  sO    

++//8PW0XY% 	8H 67	8r   c                 x   d}ddddd}ddd	d
d}|D ]!  }|d   t        |j                               v r||d      |d<   nt        |d         |d<   |d   dk(  r|j                  dd      }dD ]  }||vs||   d||<    d|vs|d   |j                  |d   d      |d<   d|v r|d= |d   dk(  r~|d   s+| j                  j                  dj                  |             |d   }|d   dk  s|j                  dd      r| j                  j                  dj                  |             i |d<   $ ||fS )a  Ensures interfaces object is properly formatted before submitting it to API.

        Args:
            interfaces (list): list of dictionaries for each interface present on the host.

        Returns:
            (interfaces, ip) - where interfaces is original list reformated into a valid format
                and ip is any IP address found on interface of type agent (printing purposes only).
         r$   r6         )agentsnmpipmijmx1005016162312345)r$   r6   r}   r~   typeip)r   dnsportbulkdetailsz0Option 'details' required for SNMP interface {0}r   version	communityFzBOption 'community' is required in 'details' for SNMP interface {0})listkeysrj   r   r   r   format)r   r/   r   interface_typestype_to_port	interfacekey	i_detailss           r   construct_host_interfaceszHost.construct_host_interfaces  s    $%q!AF"u7C# 	*I D)=)=)?$@@$3If4E$F	&!$'	&(9$:	&! A%]]4,$ (i'9S>+A%'IcN( Y&)F*;*C$0$4$4Yv5F$K	&!"f%  A% +LL**/a/h/hir/s*t%i0	Y'!+IMM+u4ULL**`gghqr + t (*	)$=	*@ Br   c           	          t        |      t        |      k7  ry|D ]@  }d}|D ]7  }i }t        j                  t        j                  |||             |i k(  s5d} @ B |rsyy)NTF)r%   zabbix_utilshelper_cleanup_datahelper_compare_dictionaries)r   exist_interfacesr/   ifacefounde_int	diff_dicts          r   check_interface_propertieszHost.check_interface_properties  s{     C
O3 	EE) 	001Y1YZ_afhq1rs? E	 er   c                     |d   S )Nr1    )r   r   s     r   get_host_status_by_hostzHost.get_host_status_by_host  s    H~r   c                 f   t        | j                  |      d       }t        |d       |k7  ry| j                  |      }t        |      t        |      k7  ry| j	                  ||      ry| j                  |      }t        t        |            t        |      k7  ryt        | j                        t        d      k  rt        |d         t        |      k7  rIyt        |d         |k7  ryt        |d         t        |      k7  ryt        |d	         t        |      k7  ry|	r	|d
   |	k7  ry|
r	|d   |
k7  ry|r!t        |d         | j                  |      k7  ry|r2t        j                  |d         }|j                  |       ||d   k7  ry|d|v rt        |d         |k7  ry||y|d|v r	|d   |k7  ry|d|v r	|d   |k7  ry|d|v rt        |d         |k7  ry|t        |d         |k7  ry|t        |d         |k7  ry|	|d   |k7  ry|	|d   |k7  ry|id|v ret        j                  |      } |d   D ]&  }!|!j                  dd       |!j                  dd       ( g }"t        j                   | |d   |"       |"g k7  ry|%d|v r!t        |d       t        |d   d       k7  ryy)Nc                     | d   S Nrt   r   ks    r   <lambda>z+Host.check_all_properties.<locals>.<lambda>  s    YZ[dYe r   r   c                     | d   S r   r   r   s    r   r   z+Host.check_all_properties.<locals>.<lambda>  s
    1Y< r   Tr2   r3   r4   r5   r7   r   r>   inventory_mode	inventoryr9   r<   r=   r8   r?   r@   rA   rB   rC   rU   FhostmacroidrD   c                     | d   S Nra   r   r   s    r   r   z+Host.check_all_properties.<locals>.<lambda>i  s
    !E( r   c                     | d   S r   r   r   s    r   r   z+Host.check_all_properties.<locals>.<lambda>i  s    Z[\aZb r   )sortedrx   r   rj   r   rz   setr   r   rI   inventory_mode_numericcopydeepcopyrV   popr   helper_compare_lists)#r   rW   rL   r1   r/   r)   r   r   rM   rN   r>   r   r   inventory_zabbixr9   r:   r;   r<   r=   r8   r?   r@   rA   rB   rC   rD   r4   rO   exist_host_groupsexist_statusexist_template_idsproposed_inventoryt_macrosmacrodiffs#                                      r   check_all_propertieszHost.check_all_properties  s(    #4#@#@#IOef)!78<MM 33D9v;#l++ **+;ZH "??HtL!"c*<&==--.e1DD4'(CM94'(L84	?#s8}44()S-@@ F||+ M"k14()*d.I.I..YY!%tK/@!A%%&67!T+%66!ld&:4%&*4 '7+>!ld&:L!Z/"}'<M"k1"}'<4&';6$4()]:%4()*n<$O$5$O$5 (d"2}}V,Hh 0		(E*		-/0 D--hXMrz$d 23vd6lPb7ccr   c                 6   | j                  |      }t        |      }t        |      }t        |      }g }|D ]  }|j                  d|i        |j	                  |      }t        |      }	g }
|	D ]  }|
j                  d|i        |||
d}	 | 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)Nr!   )rU   	templatestemplates_clearTr-   z#Failed to link template to host: %sr   )rz   r   r   r'   
differencer   rG   rH   r   r   rV   rK   r   )r   rW   template_id_listexist_template_id_listr   r)   template_id_list_tr   templates_clear_listtemplates_clear_list_request_strrR   s                r   link_or_clear_templatezHost.link_or_clear_templateo  s    !%!C!CG!L !78+,-! 	8A$$lA%67	8 -77E#O4 "% 	<A!((,):;	< ")7H]rs	R||&&&&t&4JJOO"";/ 	RLL""'Lq'P"QQ	Rs   AC& &	D/DDc                 f    |dk(  rt        d      S |dk(  rt        d      S |dk(  rt        d      S |S )N	automaticr$   manualr   disabledrj   )r   r   s     r   r   zHost.inventory_mode_numeric  s;    [(q6Mx'q6Mz)r7Nr   c                 P   |sy | j                  |      }||d}	 | 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)N)rU   r   Tr-   z(Failed to set inventory_mode to host: %sr   )	r   r   rG   rH   r   r   rV   rK   r   )r   rW   r   r   rR   s        r   update_inventory_modezHost.update_inventory_mode  s     44^D ")NK	W||&&&&t&4JJOO"";/ 	WLL""'QTU'U"VV	Ws   AA3 3	B%<B  B%c                 .   |sy ||d}	 | 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)N)rU   r   Tr-   z#Failed to set inventory to host: %sr   )r   rG   rH   r   r   rV   rK   r   )r   rW   r   r   rR   s        r   update_inventory_zabbixzHost.update_inventory_zabbix  s    !(yA	R||&&&&t&4JJOO"";/ 	RLL""'Lq'P"QQ	Rs   AA" "	B+BBN)__name__
__module____qualname__r   r   r+   rS   r[   r^   rg   rm   rr   ru   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r
   r
     ss    
4Xl9XvX $1=f
U. b(gTR4W"Rr   r
   c                    g }dddddd}ddddddddddd
}g d}| D ]  }|}t        t        |j                               t        |      z
        }t        j                  ||	      d   }|j                         }|j                  |       |j                         |d
<   d
|v r|d
   j                  |d
          |j                  |        |S )N0r|   )mainuseipr   r   r   r6   r$   r   
r   r   r   securitynamecontextnamesecuritylevelauthprotocolauthpassphraseprivprotocolprivpassphrase)rU   r   r   r   r   r   r   r   interface_refitemsinterfaceid)del_keysr   )r   r   r   r   helper_normalize_datar   rV   r'   )	r   new_exist_interfacesdefault_interfacedefault_interface_detailsallowed_fieldsr   normalized_interfacedenied_fieldsnew_interfaces	            r   %update_exist_interfaces_with_defaultsr     s    !N & 3	(S!12S5HHI+AA 

  *..012#<#A#A#Ci 	!)$++Ii,@A##M23  r   c                 0   d| v rUdj                  | j                  d      d   j                         dj                  | j                  d      dd        g      } n| j                         } | j                  d      sd| z   } | j	                  d      s| dz   } | S )N:r   r$   z{$})joinsplitupper
startswithendswith)
macro_names    r   normalize_macro_namer     s    
jXXz//4Q7==?*JZJZ[^J_`a`bJcAdef
%%'
   &J&
s##%
r   c            (         t        j                         } | j                  t        dqi dt        dd      dt        ddd      d	t        ddd      d
t        ddddg      dt        ddddg      dt        ddg d      dt        dd       dt        dd       dt        ddd       dt        ddd d      dt        dd      dt        dd      dt        dd      dt        ddd       d!t        ddd"g#      d$t        dd      d%t        d&d      d't        dd&g t        t        ddg d(      t        dd)d*gd)+      t        dd)d*gd)+      t        d,      t        d,      t        d,      t        d&i t        t        dg d-d.+      t        dd)d*gd*+      t        dd/      t        dd/      t        dd/      t        dg d0d)+      t        dg d1d)+      t        dd/d2      t        dg d1d)+      t        dd/d2      3
      4      5      d6d)d7ggd6d*d8ggg9      d:t        d;d      d<t        ddg d=d >      d?t        dd      d@t        dd      dAt        dd      dBt        dd      dCt        dd&dDgt        t        dd      t        dd      t        dd/      t        ddEdEdFg      G      H      dIt        dd&dJgt        t        dd      t        dd/      K      H             t	        | g dLg dMgdN      }|j
                  d   }|j
                  dA   }|j
                  dB   }|j
                  d   }|j
                  d	   }|j
                  d   }|j
                  d   }|j
                  d   }	|j
                  d   }
|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d!   }|j
                  d$   }|j
                  d%   }|j
                  d
   }|j
                  d   }|j
                  d'   }|j
                  d:   }|j
                  d?   }|j
                  dC   }|j
                  dI   }|j
                  d<   }|j
                  d@   }|dk(  rd*nd)}|dOk(  rd)}n|d?k(  rd*}n|d@k(  rd.}t        |      }g }|r|j                  |      }g }|2t        |      d*k\  r|j                  |      }n|j                  dPQ       |j                  |      \  }}|r9|D ]4  } t        | dR         | dR<   dS| v s| dS   dEk(  rdT| dS<   '| dS   dFk(  s0dU| dS<   6 |r|j                  |      }!nd)}!|r|j                  |      }"nd)}"|j                  |      }#|#r|j!                  |      }$|$dV   }%|$dW   dXk(  }&|>t#        |j$                        t#        dY      k  rt'        |$dZ         }!nt'        |$d[         }!t#        |j$                        t#        dY      k\  r |t'        |$d<         }|t'        |$d\         }"|dk(  r)|j)                  |%|       |j+                  dd]|z  ^       y |s|j-                  |%      }|j.                  j0                  j3                  d_|%d`      }'|'j5                  da b       t7        |'      }'t9        t;        j<                  |'            D ]  \  }(})t?        |)jA                               D ]f  }*|*dcv rt'        |)|*         |'|(   |*<   |*ddk(  s"|)|*   s	i |'|(   |*<   0|)|*   jA                         D ]!  }+|+dev st'        |)|*   |+         |'|(   |*   |+<   # h  t;        j<                  |      },g }-t9        t;        j<                  |'            D ]  \  }(})|)df   }.|)dV   }/|,s|jC                  |)       $d}0t9        |,      D ][  \  }1}2i }3t        jD                  t        jF                  |2|)|3             |3i k(  s8d}0|-jC                  |2       |.||1   df<   |/||1   dV<    n |0r|s|jC                  |)       t9        |,      D ]8  \  }1}2|)dS   |2dS   k(  s|2|-vs|-jC                  |2       |.||1   df<   |/||1   dV<      |r|&tI        tK        ||jM                  |%      z               }|s|j-                  |%      D ]  }4|4|vs|jC                  |4        |dC|$jA                         v r|$dC   }5|5D ]  } | jO                  dgd        | jO                  dVd        | jO                  dhd        d}0t9        |      D ]'  \  }1}6i }3t        jF                  |6| |3       |3i k(  s%d}0 n |0r| |1<   x|jC                  |         |IdI|$jA                         v r7|D 7cg c]  }7|7di   	 }8}7|$dI   }9|9D ]  }:|:di   |8vs|jC                  |:        |jQ                  |%|||||'|$|!|||||||||||||	|
|||||"      r}|jS                  ||||%||'|!||||||||||	|
||||&|$||"       |jU                  |%|       |jW                  |%|       |jY                  |%|       |j+                  ddj|dk|dl|dm^       y |j+                  dn       y d}&|dk(  r|j+                  dn       |s|j                  do|z  Q       |j[                  |||||!||||||||||	|
|||||"      }%|jU                  |%|       |jW                  |%|       |jY                  |%|       |j+                  ddp|dk|dl|dm^       y c c}7w )rNr   strT)r   requiredhost_groupsr   F)r   r  elementslink_templatesr1   enabledr   )r   defaultchoicesstatepresentabsentr   )r   r   r   )r   r  r  r?   rj   )r   r  r@   rA   )r   r  r  rB   )r   r  r  no_logr8   r9   r:   r;   )r   r  r  ca_certr<   )r   r  aliasesr=   r   dictr/   )r   1r   2r   3r   4r   r$   )r   r  r  )r   )r$   r6   r}   r6   r|   )r   r$   r6   )r   r$   r6   r}   r~      )r   r  r  r   )r   r  options)r   r   r   r   r   r   r   r   r   r   )r   r  r  r  required_ifforceboolr4   )zabbix_serverri   proxy_group)r   r  r  r  ri   r  rN   r>   rC   user_macrostextsecret)r   rb   r>   r   )r   r  r  r  rD   	host_tags)ra   rb   )r4   ri   )ri   )r4   r  )r  )argument_specr  supports_check_moder  z#host_groups must be not empty list.r   r   r   r   r  rU   flagsr  r2   r3   r5   r7   zSuccessfully delete host %s)r.   r   r`   rw   c                     t        | d         S )Nr   r   )xs    r   r   zmain.<locals>.<lambda>  s    Am4D0E r   r   )r   r   r   r   r   )r   r   r   r   r   r   r   r   ra   zSuccessfully updated host z (z) and linked with template ''r-   z2Specify at least one group for creating host '%s'.zSuccessfully added host r   ).r   zabbix_common_argument_specrV   r  r   rf   r
   r+   r%   ru   r   r   r   rm   rr   r   rg   r   rI   rj   r^   rH   rx   r   hostinterfacer   sortr   	enumerater   r   tupler   r'   r   r   r   r   rz   r   r   r[   r   r   r   rS   );r  moduler   rN   r>   r  r  r   r?   r@   rA   rB   r8   r9   r:   r;   r<   r=   r   r1   r  r/   r  ri   rC   rD   r4   r  r   r)   rL   r   r   rM   rO   r   rZ   rW   rY   r   idxr   r   d_keyinterfaces_copyfound_in_interfacesr   rU   r   idx1r   r   group_idexisting_macros
prov_macror   provided_tagsexisting_tagsra   s;                                                              r   r   r     s#    <<>M MED1MfuuEM %%HM 	Iz;RS	M
 y9h:OPM @cdM t4M 5M tDM tDQM ee4M UU3M 559M %%=M %%,HM  ee4!M" 6E:#M$ ut=qruq!fa@1vqAU#e$u%  $%A N!uq!faH"&E2">%)ub%A$(eR$@&*yRS&T%)u>PZ[%\'+4'P%)u>PZ[%\'+4'P2 !eW%!dV$9 
%Mf -gMh uu>gquviMj .kMl ee4mMn uu5oMp ee4qMr "O55 eR8ufvx>PQ		

sMH  Med3r2	
IM M^ #1=
 !F k*I==0L--.K--.K]]#34N]]#34NMM/2M]]#34NMM/2MMM/2M--.K|,J}}%78mmI&Gy)J--.K}}%78]]8$FMM'"E|,JMM'"EMM'"E]]8$F== D==0L--.K J&QAF &		 		&<DL,,^<I{q 99+FI!FG33J?NJ 	(E1%.AE'N=F*$'E&M6]h.$'E&M	( 11%888E &&y1M44Y?!(+)'2c9 =D112\%5HH~>?y9:--.,u2EE#"?>#BC"!$__%E!FHWi0T2OR[2[\ !99'B	  $zz77;;x\c<de!!&E!FDEUV #,DMM:J,K"L cY !12 
cC??5835H(-c2	)(~9;,S1#6)23)<)<)> c#(,p#pHKIVYN[`LaHb$4S$9#$>u$Ec
cc #mmJ7O"$"+DMM:J,K"L "&Y'6"8,&%%i0 #,_#= KD% "I 44\5]5]^cenpy5z{ B $+2259:E
4(75;
4(2  ")))4 ,5_+E &KD%(0E&MAeSfFf 3 : :5 ABM
4 0 ?=C
4 0 : %&;"&H N2#Ct7Y7YZa7b(b$cd $ = =g F 3Hy0!((23
 %(o6J6J6L*L&5h&?O!0 1		-6		(D1		+t4 %09&0A &,D*(*I(DDZQVXab(B(, %& !+0F4L"MM%01" #/2F2F2H(H7;$<!QuX$<M$<$3F$;M, -u:]: KK,-
 ((Y
LJZ\kmu +y.JZ\fhx  {B[-!=&$n	^   y&':GWYacoj:JGU_al!>=-QWY]_np .	2 ++G\B**7NC,,W6FG  +4b.)J ! K    /  HU+!UXa!ab --y&*hk[fhrgz;~_l64~G
 	##G\:""7N;$$W.>?r>/+ 	,g %=s   ;m__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr   ansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   #ansible.module_utils.compat.versionr   Aansible_collections.community.zabbix.plugins.module_utils.helpersr   zabbixpluginsmodule_utilshelpersr   r
   r   r   r   r   r   r   r   <module>rA     ss    A @Qf
FR  4 U < X X X}R: }RB1 h Q,h
 zF r   