
    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d
 Zedk(  r e        yy)    )absolute_importdivisionprint_functionaM  
---
module: zabbix_hostmacro
short_description: Create/update/delete Zabbix host macros
description:
   - manages Zabbix host macros, it can create, update or delete them.
author:
    - "Cove (@cove)"
    - Dean Hailin Song (!UNKNOWN)
requirements:
    - "python >= 3.9"
options:
    host_name:
        description:
            - Name of the host.
        required: true
        type: str
    macro_name:
        description:
            - Name of the host macro in zabbix native format C({$MACRO}) or simple format C(MACRO).
        required: true
        type: str
    macro_value:
        description:
            - Value of the host macro.
            - Required if I(state=present).
        type: str
    macro_type:
        type: str
        description:
            - Type of the host macro.
            - text (default)
        required: false
        choices: ["text", "secret", "vault"]
        default: "text"
    macro_description:
        description:
            - Text Description of the global macro.
        type: str
        default: ""
    state:
        description:
            - State of the macro.
            - On C(present), it will create if macro does not exist or update the macro if the associated data is different.
            - On C(absent) will remove a macro if it exists.
        required: false
        choices: ["present", "absent"]
        type: str
        default: "present"
    force:
        description:
            - Only updates an existing macro if set to C(yes).
        default: "yes"
        type: bool

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 new host macro or update an existing macro's value
  # 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_hostmacro:
    host_name: ExampleHost
    macro_name: EXAMPLE.MACRO
    macro_value: Example value
    macro_description: Example description
    state: present

# Values with curly brackets need to be quoted otherwise they will be interpreted as a dictionary
- name: Create new host macro in Zabbix native format
  # 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_hostmacro:
    host_name: ExampleHost
    macro_name: "{$EXAMPLE.MACRO}"
    macro_value: Example value
    macro_description: Example description
    state: present

- name: Delete existing host macro
  # 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_hostmacro:
    host_name: ExampleHost
    macro_name: "{$EXAMPLE.MACRO}"
    state: absent
)AnsibleModule)
ZabbixBaseNc                   *    e Zd Zd Zd Zd Zd Zd Zy)	HostMacroc                 :   	 | j                   j                  j                  dd|id      }t        |      dk  r | j                  j                  d|z         y |d   d   }|S # t        $ r-}| j                  j                  d	|d
|d       Y d }~y d }~ww xY w)Nextendhost)outputfilter   zHost not found: %smsgr   hostidzFailed to get the host z id: .)_zapir   getlen_module	fail_json	Exception)self	host_name	host_listhost_ides        u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_hostmacro.pyget_host_idzHostMacro.get_host_id   s    	]

++xFT]K^,_`I9~!&&+?)+K&L#A,x0 	]LL""iYZ'["\\	]s   AA$ 	A$ $	B-#BBc                     	 | j                   j                  j                  dd|gd|id      }t        |      dkD  r|d   S y # t        $ r,}| j
                  j                  d|d|       Y d }~y d }~ww xY w)Nr   macro)r   selectStepshostidsr   r   zFailed to get host macro : r   )r   	usermacror   r   r   r   r   )r   
macro_namer   host_macro_listr   s        r   get_host_macrozHostMacro.get_host_macro   s    	\"jj2266#H'_fhr^stvO?#a'&q)) 	\LL""JXY'Z"[[	\s   ?A 	A8"A33A8c                 p   	 | j                   j                  r| j                   j                  d       | j                  j                  j                  |||||d       | j                   j                  dd|z         y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)	NTchanged)r   r"   valuetypedescriptionz Successfully added host macro %sr,   resultzFailed to create host macro r%   r   )r   
check_mode	exit_jsonr   r&   creater   r   )r   r'   macro_value
macro_typemacro_descriptionr   r   s          r   create_host_macrozHostMacro.create_host_macro   s    	_||&&&&t&4JJ  ''7ZZeoy  K\  )]  ^LL""48Z]g8g"h 	_LL""z[\']"^^	_s   A=B   	B5	"B00B5c                    |d   }|d   |k(  r=|d   dk(  r5|dk(  r0|d   |k(  r(|d   |k(  r | j                   j                  dd|z  	       	 | j                   j                  r| j                   j                  d
       | j                  j                  j                  ||||d       | j                   j                  d
d|z  	       y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)Nhostmacroidr"   r.   0r-   r/   Fz Host macro %s already up to dater0   Tr+   )r:   r-   r.   r/   z"Successfully updated host macro %szFailed to update host macro r%   r   )r   r3   r2   r   r&   updater   r   )r   host_macro_objr'   r5   r6   r7   host_macro_idr   s           r   update_host_macrozHostMacro.update_host_macro   s   &}5'"j0v&#-*2CW^H_cnHn&}59JJ&&u=_bl=l&m	_||&&&&t&4JJ  ''P[eo  AR  )S  TLL""48\_i8i"j 	_LL""z[\']"^^	_s   A<C	 		C>"C99C>c                 p   |d   }	 | j                   j                  r| j                   j                  d       | j                  j                  j                  |g       | j                   j                  dd|z         y # t        $ r,}| j                   j                  d|d|       Y d }~y d }~ww xY w)	Nr:   Tr+   z"Successfully deleted host macro %sr0   zFailed to delete host macro r%   r   )r   r2   r3   r   r&   deleter   r   )r   r=   r'   r>   r   s        r   delete_host_macrozHostMacro.delete_host_macro   s    &}5	_||&&&&t&4JJ  ''8LL""48\_i8i"j 	_LL""z[\']"^^	_s   A8B   	B5	"B00B5N)__name__
__module____qualname__r    r)   r8   r?   rB        r   r	   r	      s     	]\__ _rG   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)r'   s    r   normalize_macro_namerP      s    
jXXz//4Q7==?*JZJZ[^J_`a`bJcAdef
%%'
   &J&
s##%
rG   c                     t        j                         } | j                  t        t        dd      t        dd      t        dd      t        ddg d      t        dd	      t        dd
d
dg      t        dd	                   t	        | dd
dgggd      }|j
                  d   }t        |j
                  d         }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   dk(  rd}n|j
                  d   dk(  rd}nd}t        |      }	|r#|	j                  |      }
|	j                  ||
      }|dk(  r,s|j                  dd|z         y |	j                  ||       y s|	j                  ||||
       y |r|	j                  |||||       y |j                  dd|z         y )NstrT)r.   requiredFtext)rT   secretvault)r.   defaultchoices )r.   rW   presentabsentbool)r   r'   r5   r6   r7   stateforcer]   r5   )argument_specrequired_ifsupports_check_moder   r'   r7   r^   r6   rU   1rV   2r;   zHost Macro %s does not exist)r,   r   z3Host macro %s already exists and force is set to nor0   )zabbix_utilszabbix_common_argument_specr<   dictr   paramsrP   r	   r    r)   r3   rB   r8   r?   )r_   moduler   r'   r5   r7   r]   r^   r6   host_macro_class_objr   r=   s               r   mainrj      s    <<>MED1UT2ee4UF<WXE26y9h:OP-  #i-1
 !F k*I%fmmL&ABJ--.K&9:MM'"EMM'"E}}\"h.
	|	$	/

$V,&229=-<<ZQU0NQ[0[\ !22>:N 22:{JXikrs 22>:{\fhyzU3hku3uvrG   __main__)
__future__r   r   r   r.   __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helpersrd   r	   rP   rj   rC   rF   rG   r   <module>rx      sc    A @:x>B 5 U X X X;_
 ;_| 4wn zF rG   