
    VhB                         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edk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: zabbix_script
short_description: Create/update/delete Zabbix scripts
version_added: 1.7.0
author:
    - Evgeny Yurchenko (@BGmot)
description:
    - This module allows you to create, update and delete scripts.
requirements:
    - "python >= 3.9"
options:
    name:
        description:
            - Name of the script.
        required: true
        type: str
    script_type:
        description:
            - Script type. Required when state is 'present'.
        type: str
        required: false
        choices: ["script", "ipmi", "ssh", "telnet", "webhook"]
    command:
        description:
            - Command to run. Required when state is 'present'
        type: str
        required: false
    scope:
        description:
            - Script scope.
        type: str
        required: false
        choices: ["action_operation", "manual_host_action", "manual_event_action"]
        default: "action_operation"
    execute_on:
        description:
            - Where to run the script.
            - Used if type is C(script).
        type: str
        required: false
        choices: ["zabbix_agent", "zabbix_server", "zabbix_server_proxy"]
        default: "zabbix_server_proxy"
    menu_path:
        description:
            - Folders separated by slash that form a menu like navigation in frontend when clicked on host or event.
            - Used if scope is C(manual_host_action) or C(manual_event_action).
        type: str
        required: false
    authtype:
        description:
            - Authentication method used for SSH script type.
            - Used if type is C(ssh).
        type: str
        required: false
        choices: ["password", "public_key"]
    username:
        description:
            - User name used for authentication.
            - Used if type is C(ssh) or C(telnet)
        type: str
        required: false
    password:
        description:
            - Password used for SSH scripts with password authentication and Telnet scripts.
            - Used if type is C(ssh) and authtype is C(password) or type is C(telnet).
        type: str
        required: false
    publickey:
        description:
            - Name of the public key file used for SSH scripts with public key authentication.
            - Used if type is C(ssh) and authtype is C(public_key).
        type: str
        required: false
    privatekey:
        description:
            - Name of the private key file used for SSH scripts with public key authentication.
            - Used if type is C(ssh) and authtype is C(public_key).
        type: str
        required: false
    port:
        description:
            - Port number used for SSH and Telnet scripts.
            - Used if type is C(ssh) or C(telnet).
        type: str
        required: false
    host_group:
        description:
            - host group name that the script can be run on. If set to "all", the script will be available on all host groups.
        type: str
        required: false
        default: "all"
    user_group:
        description:
            - user group name that will be allowed to run the script. If set to "all", the script will be available for all user groups.
            - Used if scope is C(manual_host_action) or C(manual_event_action).
        type: str
        required: false
        default: "all"
    host_access:
        description:
            - Host permissions needed to run the script.
            - Used if scope is C(manual_host_action) or C(manual_event_action).
        type: str
        required: false
        choices: ["read", "write"]
        default: "read"
    confirmation:
        description:
            - Confirmation pop up text. The pop up will appear when trying to run the script from the Zabbix frontend.
            - Used if scope is C(manual_host_action) or C(manual_event_action).
        type: str
        required: false
    script_timeout:
        description:
            - Webhook script execution timeout in seconds. Time suffixes are supported, e.g. 30s, 1m.
            - Required if type is C(webhook).
            - "Possible values: 1-60s."
        type: str
        default: "30s"
        required: false
    parameters:
        description:
            - Array of webhook input parameters.
            - Used if type is C(webhook).
        type: list
        elements: dict
        suboptions:
            name:
                description:
                    - Parameter name. Required when 'parameters' is specified for a 'webhook' script.
                type: str
                required: false
            value:
                description:
                    - Parameter value. Supports macros.
                type: str
                required: false
                default: ""
    description:
        description:
            - Description of the script.
        type: str
        required: false
    state:
        description:
            - State of the script.
        type: str
        required: false
        choices: ["present", "absent"]
        default: "present"
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: test - Create new action operation script to execute webhook
  # 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_script:
    name: Test action operation script
    scope: action_operation
    script_type: webhook
    command: "return 0"
    description: "Test action operation script"
    state: present

)AnsibleModule)
ZabbixBaseNc                   *    e Zd Zd Zd Zd Zd Zd Zy)Scriptc                     g }| j                   j                  j                  dd|ii      }|D ]  }|j                  |d           |S )Nfilternamescriptid)_zapiscriptgetappend)selfscript_name
script_idsscriptsr   s        r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_script.pyget_script_idszScript.get_script_ids   sS    
**##''FK3H(IJ 	2FfZ01	2    c                     | j                   j                  r| j                   j                  d       | j                  j                  j                  | j                  |||||||||	|
|||||||||             y NTchanged)_module
check_mode	exit_jsonr   r   creategenerate_script_config)r   r   script_typecommandscope
execute_on	menu_pathauthtypeusernamepassword	publickey
privatekeyport
host_group
user_grouphost_accessconfirmationscript_timeout
parametersdescriptions                       r   create_scriptzScript.create_script   s    <<""LL""4"0

  !<!<T;PWY^`jlu!)8Xy*VZ\fhrt  BN!/["J 	Kr   c                     | j                   j                  r| j                   j                  d       | j                  j                  j                  |       y r   )r   r   r    r   r   delete)r   r   s     r   delete_scriptzScript.delete_script   s<    <<""LL""4"0

  ,r   c           	      D   |dk(  rd}nS| j                   j                  j                  dd|ii      }|s | j                  j	                  dd|z         |d   d	   }|dk(  rd}nS| j                   j
                  j                  dd|ii      }|s | j                  j	                  dd
|z         |d   d   }|t        t        j                  g d|            |t        t        j                  g d|            |d}|||d<   |dk(  r(|d}t        t        j                  g d|            |d<   |dv rC|d|d<   n||d<   ||d<   t        t        j                  g d|            |d<   |d|d<   n||d<   |dk(  r3t        t        j                  ddg|            |d<   |dk(  r
|
|d<   ||d<   |d v r ||d!<   |dk(  r|dk(  s|d"k(  r|	|d<   |||d#<   |d$k(  rG||d%<   |r@|D ]6  }d|j                         vs|d   | j                  j	                  d&'       8 ||d(<   |S ))Nall0r   r   FzHost group '%s' not foundr   msgr   groupidzUser group '%s' not foundusrgrpid)r   ipmisshtelnet webhook)rC   action_operationmanual_host_actionrC   manual_event_action)r   typer$   r%   r>   r4   r   zabbix_server_proxyzabbix_agentzabbix_serverrI   r&   )rF   rG   rC   r'   )rC   rC   readwriter0   r1   rA   r*   
public_keyr(   r+   r,   )rA   rB   r)   rB   r-   rD   timeoutzMWhen providing parameters to a webhook script, the 'name' option is required.)r=   r3   )
r   	hostgroupr   r   	fail_json	usergroupstrzabbix_utilshelper_to_numeric_valuekeys)r   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r>   groupsr?   user_groupsrequest	parameters                             r   r"   zScript.generate_script_config   s   GZZ))--x&*9M.NOF&&u:UXb:b&cQi	*GH**..22Hvz>R3STK&&u:UXb:b&c"1~j1H << > () * == ?'
 )./ 0 !
& "%0GM"("!2
$'(L(L N' )3)4 %5GL!
 AA ')$'0$"*GJ%()M)M O &	*' &(GM"
 #*,'*6'%"%l&J&JL'') #*GJ <''0$(2%++"*GJu$Z)?KS[D[&.
#"&)#!/GI!+ DIY^^%5569J9R..  4C.  DD )3%r   c                 <   | j                  ||||||||	|
||||||||||      }| j                  j                  j                  dd|ii      d   }i }t	        j
                  t	        j                  |||            }|s | j                  j                  dd|z         | j                  j                  r| j                  j                  d       |d	   |d	<   | j                  j                  j                  |       | j                  j                  dd
|z         y )Nr   r   r   FzScript %s up to dater<   Tr   r   zScript %s updated)r"   r   r   r   rU   helper_cleanup_datahelper_compare_dictionariesr   r    r   update)r   	script_idr   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   generated_configlive_configchange_parameters
differences                            r   update_scriptzScript.update_script?  s3   66t['SXZdfoqy  |D7?JX\^hjt  wB7C^U_aln jj''++X~,FGJ!55l6^6^_oq|  P  7Q  R
LL""56Lt6S"T<<""LL""4"0'2:'>$

  !12t1Dt1KLr   N)__name__
__module____qualname__r   r5   r8   r"   re    r   r   r
   r
      s    K-
WrMr   r
   c                     t        j                         } | j                  t        d8i dt        dd      dt        dg d      dt        d	      d
t        dg dd      dt        dg dd      dt        d	      dt        dddg      dt        d	      dt        dd      dt        d	      dt        dd      dt        d	      dt        dd      dt        dd      dt        ddd gd      d!t        d	      d"t        dd#      d$t        d%d&t        t        d	      t        dd'      (      )      d*t        d	      d+t        dd,d,d-g.             g d/}t	        | |d0      }|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
                  d+   }t        |      }|j                  |      }|d-k(  r@|s|j                  d1d2|z  3       |j                  |       |j                  dd4|z  5       y |d,k(  rd|s:|j                  |||||||	|
|||||||||||       |j                  dd6|z  3       y |j                  |d7   |||||||	|
|||||||||||       y y )9Nr   rT   T)rH   requiredr#   )r   r@   rA   rB   rD   )rH   choicesr$   )rH   r%   )rE   rF   rG   rE   )rH   rl   defaultr&   rJ   rI   r'   r(   r*   rO   r)   )rH   no_logr+   r,   r-   r.   r:   )rH   rm   r/   r0   rM   rN   r1   r2   30sr3   listdictrC   )r   value)rH   elementsoptionsr4   statepresentabsent)rH   rm   rl   ))ru   rv   )r#   r$   )r#   rA   )r(   r)   )r(   r*   )r*   )r(   rO   )r+   r,   )r#   rB   )r)   r*   )argument_specrequired_ifsupports_check_modeFzScript not found, no change: %sr<   z!Successfully deleted script(s) %s)r   resultzScript %s createdr   ri   )rU   zabbix_common_argument_specr_   rq   r   paramsr
   r   r    r8   r5   re   )rx   ry   moduler   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   ru   r   r   s                            r   mainr   S  s    <<>M ,ut,,BD,
 % , U&(, L)+, E", .0,$ 5!%,& 5.',( E"),* U40+,, u-,. UE2/,0 UE21,2 W%3,: u%;,< 6=,> u%r2
?,N e$O,P )+Q, ,\K # F == D--.KmmI&GMM'"E|,Jk*I}}Z(H}}Z(H}}Z(Hk*I|,J== D|,J|,J--.K==0L]]#34N|,J--.KMM'"EF^F&&t,J U0QTX0XYZ(.QTX.XY	)	  {GUJPY[cemow!*Jj*Vacoq  BL  NYZT/BT/IJ  Ak7ES]_hjrt|!)9j$
T^`kmy!/[J 
r   __main__)
__future__r   r   r   rH   __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helpersrU   r
   r   rf   ri   r   r   <module>r      si    A @Yv@
 5 U X X X@MZ @MFeJP zF r   