
    Vh-(                         d dl mZmZmZ eZd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_functiona  
---
module: zabbix_globalmacro
short_description: Create/update/delete Zabbix Global macros
version_added: 1.4.0
description:
   - manages Zabbix Global macros, it can create, update or delete them.
   - For macro_type Secret the value field cannot be validated and will always be overwritten due to the secret nature of the Text.
author:
    - "Cove (@cove)"
    - Dean Hailin Song (!UNKNOWN)
    - Timothy Test (@ttestscripting)
requirements:
    - "python >= 3.9"
options:
    macro_name:
        description:
            - Name of the global macro in zabbix native format C({$MACRO}) or simple format C(MACRO).
        required: true
        type: str
    macro_value:
        description:
            - Value of the global macro.
            - Required if I(state=present).
        type: str
    macro_type:
        description:
            - Type of the global macro Text or Secret Text.
            - Required if I(state=present).
            - text
            - secret - Secret Text Works only with Zabbix >= 5.0 and will default to Text in lower versions
            - vault - Vault Secret Works only with Zabbix >= 5.2 and will default to Text in lower versions
        type: str
        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

notes:
    - This module returns changed=true when I(macro_type=secret).

extends_documentation_fragment:
- community.zabbix.zabbix
af  
# 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 global 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_globalmacro:
    macro_name: EXAMPLE.MACRO
    macro_value: Example value
    macro_type: 0
    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 global macro in Zabbix native format with Secret Type
  # 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_globalmacro:
    macro_name: "{$EXAMPLE.MACRO}"
    macro_value: Example value
    macro_type: 1
    macro_description: Example description
    state: present
- name: Delete existing global macro
  community.zabbix.zabbix_globalmacro:
    macro_name: "{$EXAMPLE.MACRO}"
    state: absent

)AnsibleModule)
ZabbixBaseNc                   $    e Zd Zd Zd Zd Zd Zy)GlobalMacroc                 $   	 | j                   j                  j                  ddi      }|D cg c]  }|d   |k(  s| }}t        |      dkD  r|d   S y c c}w # t        $ r,}| j
                  j                  d|d|       Y d }~y d }~ww xY w)Nglobalmacrotruemacror   zFailed to get global macro : msg)_zapi	usermacrogetlen	Exception_module	fail_json)self
macro_nameall_global_macro_listdglobal_macro_listes         w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_globalmacro.pyget_global_macrozGlobalMacro.get_global_macro   s    	^$(JJ$8$8$<$<mV=T$U!,A ^qQwZS]E] ^ ^$%)(++ !_  	^LL""jZ['\"]]	^s,   +A AAA A 	B#"B

Bc                 n   	 | 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   valuetypedescriptionz"Successfully added global macro %sr#   resultzFailed to create global macro r   r   )r   
check_mode	exit_jsonr   r   createglobalr   r   )r   r   macro_value
macro_typemacro_descriptionr   s         r   create_global_macrozGlobalMacro.create_global_macro   s    	a||&&&&t&4JJ  --
[bl  ~O  /P  QLL""48\_i8i"j 	aLL""Q[]^'_"``	as   A<A? ?	B4"B//B4c                    |d   }	 |d   dk(  s|d   dk(  r@|d   |k(  r8|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globalmacroidr%   02r   r$   r&   Fz"Global macro %s already up to dater'   Tr"   )r1   r   r$   r%   r&   z$Successfully updated global macro %szFailed to update global macro r   r   )r   r*   r)   r   r   updateglobalr   r   )r   global_macro_objr   r,   r-   r.   global_macro_idr   s           r   update_global_macrozGlobalMacro.update_global_macro   s   *?;	a'3.2B62Jc2Q$W-;@PQX@Y]h@h,V4
BGWXeGfj{G{LL**5AehrAr*s||&&&&t&4JJ  --Zd8CZhy/{ |LL""48^ak8k"l 	aLL""Q[]^'_"``	as   CC 	D
"DD
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)	Nr1   Tr"   z$Successfully deleted global macro %sr'   zFailed to delete global macro r   r   )r   r)   r*   r   r   deleteglobalr   r   )r   r5   r   r6   r   s        r   delete_global_macrozGlobalMacro.delete_global_macro   s    *?;	a||&&&&t&4JJ  --.?@LL""48^ak8k"l 	aLL""Q[]^'_"``	as   A8B   	B5	"B00B5N)__name__
__module____qualname__r    r/   r7   r:        r   r
   r
      s    ^aa ar?   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      z{$})joinsplitupper
startswithendswith)r   s    r   normalize_macro_namerI      s    
jXXz//4Q7==?*JZJZ[^J_`a`bJcAdef
%%'
   &J&
s##%
r?   c                  ^   t        j                         } | j                  t        t        dd      t        d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      }t        |j                  d         }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|dk(  rd}n|dk(  rd}n|dk(  rd}t        |      }|r|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)r%   rL   no_logtext)rN   secretvault)r%   defaultchoices )r%   rQ   presentabsentbool)r   r,   r-   r.   stateforcerW   r,   )argument_specrequired_ifsupports_check_moder   r-   r.   rX   r2   rO   1rP   r3   zGlobal Macro %s does not exist)r#   r   z5Global macro %s already exists and force is set to nor'   )zabbix_utilszabbix_common_argument_specupdatedictr   rI   paramsr
   r    r*   r:   r/   r7   )
rY   moduler   r,   r-   r.   rW   rX   global_macro_class_objr5   s
             r   mainrd      s    <<>MUT2eeDAUF<WXE26y9h:OP-  #i-1
 !F &fmmL&ABJ--.K|,J--.K&9:MM'"EMM'"EV
	x	
	w	
(01BB:NU0PS]0]^ #667GT"66z;PZ\mn"667GU`bln  AU3jmw3wxr?   __main__)
__future__r   r   r   r%   __metaclass__DOCUMENTATIONEXAMPLESRETURN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helpersr]   r
   rI   rd   r;   r>   r?   r   <module>rs      sj    A @<|2h
 5 U X X X/a* /ad 4yn zF r?   