
    VhQ                      d dl mZ d dlZd dlZdZej
                  ek  ru e ej                   edddj                   e
ee             ddj                  ej                  j                                	                    ej                  d
       d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl Z d dl!Z!d dl"m#Z# 	 d dl$Z$dZ%	 d dl'm(Z(m)Z*  e+e(d      xr  e*jX                         Z-dZ/	 d dl0m1Z1 dZ/ e2d      Z3d
dl4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z;m<Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZD d dlEZEd ZF eF       ZGd dlHmIZImJZJmKZKmLZLmMZMmNZNmOZO d dlPmQZQ d dlRmSZS d dlTmUZVmWZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^ d dl_m`Z`maZambZb d dlcmdZdmeZemfZfmgZgmhZhmiZi d dljmkZkmlZlmmZm d dlnmoZompZp d dlqmrZs d dltmuZumvZvmwZwmxZx d dlymzZzm{Z{m|Z|m}Z} e~eIeLfZ ej                   d ej                        Ze
Zda e ed!"       ed#"       ed#"       ed#"       ed#"       ed#"       ed#"       ed#d$g%       ed&dedd'gf(      )	      Z ej                   d*      Z ej                   d+      Z ej                   d,      Z ej                   d-      Zd. Zd/ Zd0 Zrd8d1Zd2 Zd9d3Z G d4 d5e      Zd6 Zd7 Zy# e&$ r dZ%Y w xY w# e&e.f$ r dZ-Y w xY w# e&$ r Y w xY w):    )annotationsN)      Tz2ansible-core requires a minimum of Python version .z. Current version:  )failedmsg   )reduceF)journaldaemonsendv)selinux)	to_nativeto_bytesto_text)jsonifycontainer_to_bytescontainer_to_text)ModuleArgumentSpecValidator)lenient_lowercasebytes_to_humanhuman_to_bytesSIZE_RANGESc                     i } t         j                  D ]#  }t        t         |d      }|s	  |        || |<   % | S # t        $ r Y 3w xY w)zSReturn a dictionary of available hash function names and their associated function.N)hashlibalgorithms_availablegetattr	Exception)
algorithmsalgorithm_namealgorithm_funcs      J/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/basic.py_get_available_hash_algorithmsr$   \   sb    J!66 < .$?<   .<
>*<   s   ;	AA)KeysViewMappingMutableMappingSequenceMutableSequenceSet
MutableSet)get_best_parsable_locale)get_bin_path)
_PERM_BITS_DEFAULT_PERMis_executableformat_attributesget_flags_from_attributesFILE_ATTRIBUTESS_IXANYS_IRWU_RWG_RWO)get_distributionget_distribution_versionget_platform_subclass)env_fallbackremove_valuessanitize_keysDEFAULT_TYPE_VALIDATORS	PASS_VARS
PASS_BOOLS)AnsibleFallbackNotFoundAnsibleValidationErrorMultipleUnsupportedError)check_missing_parameters	safe_eval)get_all_subclasses)BOOLEANSBOOLEANS_FALSEBOOLEANS_TRUEboolean)	deprecateget_deprecation_messagesget_warning_messageswarnzB^(?:.+[-_\s])?pass(?:[-_\s]?(?:word|phrase|wrd|wd)?)(?:[-_\s].+)?$raw)typestrattr)rN   aliasesboolANSIBLE_UNSAFE_WRITES)rN   defaultfallback)	modeownergroupseuserseroleselevelsetype
attributesunsafe_writesz^[-]{0,2}pass[-]?(word|wd)?z[+=-]z^[ugo]+$z^[rwxXstugo]*$c                 *    t        j                         S )a+  
    **Deprecated** Use :py:func:`platform.system` directly.

    :returns: Name of the platform the module is running on in a native string

    Returns a native string that labels the platform ("Linux", "Solaris", etc). Currently, this is
    the result of calling :py:func:`platform.system`.
    )platformsystem     r#   get_platformrd      s     ??rc   c                6    t        |       }t        | |  |      S )zV**Deprecated**: Use ansible.module_utils.common.sys_info.get_platform_subclass instead)r8   super__new__)clsargskwargsplatform_clss       r#   load_platform_subclassrl      s    (-Ll+L99rc   c                *    t        t        |             S )zQ**Deprecated**: Use ansible.module_utils.common._utils.get_all_subclasses instead)list_get_all_subclasses)rh   s    r#   rD   rD      s    #C())rc   c                `   t        |       } g }t        |       }|}d}|r	 | j                  dd|      }d}|}|s.	 | j                  dd|      }	 | j                  d|dz   |      }|s.|rA|j	                  d| ||        |j	                  dd       |j	                  d| ||dz           |}|rd	j                  |      }|rt        ||      }|S # t        $ r |j	                  d| d|        Y Aw xY w# t        $ r d}Y w xY w# t        $ r! |dk(  r|j	                  d| d|        Y |}Y w xY w)
z; Remove strings that look like passwords from log messages r
   @r   Nz://:r   ********r   )r   lenrindex
ValueErrorinsertindexjoinr:   )datano_log_valuesoutputbegin
prev_beginsependsep_search_ends           r#   heuristic_log_sanitizer      sn    T?DFIEJ
C
	++c1e,C E1n=jjeai5 * MM!T#j12MM!Z(MM!T%a01JM P WWV_Fv}5MO  	MM!T!E]+	     	A: MM!T!J%78!&	s:   C C2 D C/.C/2D ?D #D-(D-,D-c                    t         t         } nt        t        j                        dkD  rt        j
                  j                  t        j                  d         r>t        t        j                  d   d      }|j                         } |j                          nMt        j                  d   j                  dd      } n(t        j                  j                  j                         } | a 	 t        j                  | j                  d            }	 d   S # t         $ r# t#        d       t        j$                  d       Y 1w xY w# t&        $ r# t#        d	       t        j$                  d       Y yw xY w)
a1   read the modules parameters and store them globally.

    This function may be needed for certain very dynamic custom modules which
    want to process the parameters that are being handed the module.  Since
    this is so closely tied to the implementation of modules we cannot
    guarantee API stability for it (it may change between versions) however we
    will try not to break it gratuitously.  It is certainly more future-proof
    to call this function and consume its outputs than to implement the logic
    inside it as a copy in your own code.
    Nr
   rbutf-8surrogateescapeerrorsz
{"msg": "Error: Module unable to decode stdin/parameters as valid JSON. Unable to parse what parameters were passed", "failed": true}ANSIBLE_MODULE_ARGSz
{"msg": "Error: Module unable to locate ANSIBLE_MODULE_ARGS in JSON data from stdin. Unable to figure out what parameters were passed", "failed": true})_ANSIBLE_ARGSrt   sysargvospathisfileopenreadcloseencodestdinbufferjsonloadsdecoderv   printexitKeyError)r   fdparamss      r#   _load_paramsr   *  s       sxx=1ww~~chhqk*#((1+t,
!++G<M+N YY%%**,FFMM'23+,,    X  	Y   	   	!s$   ($D E )D>=D>)E-,E-c                    t        j                         }d| d|dt        j                  d}|r|d|z  z  }|r|d|z  z  }|dz  }|S )Nz.Failed to import the required Python library (z) on z
's Python r   z This is required %s.z See %s for more info.z Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter)r`   noder   
executable)libraryreasonurlhostnamer	   s        r#   missing_required_libr   Z  s_    }}HT[]egjgugu
vC&//
'#-- N OC Jrc   c                     e Zd Z	 	 	 	 d@dZed        Zd ZdAdZdBdZd Z	d Z
d	 ZdCd
Zd ZdDdZd Zd Zd ZdBdZdEdZdEdZdEdZdEdZdDdZed        Zed        ZedBd       ZdEdZd ZdEdZdEdZ d Z!d Z"dFdZ#dGd Z$d! Z%d" Z&d# Z'dBd$Z(d% Z)d& Z*dHd'Z+d( Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3dBd0Z4d1 Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:d7 Z;dId8Z<d9 Z=d: Z>	 	 	 dJd;Z?d< Z@d= ZAeAZBdKd>ZCeDZDed?        ZEy)LAnsibleModuleNc                   t         j                  j                  t              | _        || _        || _        d| _        || _        || _	        || _
        || _        || _        |	| _        |
| _        g | _        d| _        d| _        d| _        d| _        d| _        d| _        i | _        i | _        d| _        i | _        g | _        t7               | _        d| _        |r:t<        j?                         D ]#  \  }}|| j
                  vs|| j
                  |<   % tA               | _!        | jE                          | jG                          | jI                          tK        | j
                  | j                  | j                  | j                  | j                  | j                        | _&        | jL                  jO                  | jP                        | _)        | jP                  jU                  | jR                  jV                         | jB                  jU                  | jR                  jX                         | j2                  jU                  | jR                  jZ                         	 | jR                  j\                  d   }t_        |t`              r| jb                  rd}|r`| jR                  j\                  jf                  }t_        |t`              rdji                  | j                  d|      }| jk                  |	       | j                  r,| j                  s | jm                  d
d| j                  z         tn        | _8        | j                  s| js                          d| _:        d| _;        d| _<        | j{                          y# td        $ r d}Y w xY w)a7  
        Common code for quickly building an ansible module in Python
        (although you can write modules with anything that can return JSON).

        See :ref:`developing_modules_general` for a general introduction
        and :ref:`developing_program_flow_modules` for more detailed explanation.
        FNLOG_USERr   r   z1Unsupported parameters for ({name}) {kind}: {msg}module)namekindr	   r	   Tz.remote module (%s) does not support check mode)skippedr	   )>r   r   basename__file___nameargument_specsupports_check_mode
check_modebypass_checksno_logmutually_exclusiverequired_togetherrequired_one_ofrequired_ifrequired_bycleanup_files_debug_diff_socket_path_shell_syslog_facility
_verbosityrun_command_environ_update_clean_string_conversion_actionrQ   _legal_inputsrn   _options_context_tmpdirFILE_COMMON_ARGUMENTSitemssetr{   _check_localer   _set_internal_propertiesr   	validatorvalidater   validation_resultupdatevalidated_parameters_no_log_values_aliasesr   
isinstancerA   _ignore_unknown_opts
IndexErrorr	   format	fail_json	exit_jsonr<    _CHECK_ARGUMENT_TYPES_DISPATCHER_log_invocation_selinux_enabled_selinux_mls_enabled_selinux_initial_context_set_cwd)selfr   r   r   r   r   r   add_file_common_argsr   r   r   kverrorr	   s                  r#   __init__zAnsibleModule.__init__i  s    WW%%h/
*#6 *"4!2.&&
  * +-')+& $-335 .1D...,-D&&q).
 !U 	%%'4T5G5G595L5L595K5K595I5I595E5E595E5E7 "&!8!8!E411FFG!!$"8"8"G"GHD22;;<	**11!4E%!12t7P7P
 ((//33C%!12IPPVZV`V`gouxPyNNsN#??4#;#;NN4-]`d`j`j-jNk 1H-{{  " !%$(!(,% 	5  	E	s   7M; ;N	N	c                >   | j                   d }| j                  Ft        j                  j	                  t        j                  j                  | j                              }|Kt        j                  j                  |      s,	 t        j                  |d       | j                  d|z         dt        j                         z  }	 t        j                  ||      }| j                   s$t#        j$                  t&        j(                         | _         | j                   S # t        t        f$ r,}| j                  d|dt        |             d }Y d }~d }~ww xY w# t        t        f$ r.}| j                  d|d	|d
t        |             Y d }~d }~ww xY w)Ni  )rV   zModule remote_tmp %s did not exist and was created with a mode of 0700, this may cause issues when running as another user. To avoid this, create the remote_tmp dir with the correct permissions manuallyzUnable to use z1 as temporary directory, failing back to system: zansible-moduletmp-%s-)prefixdirz/Failed to create remote module tmp path at dir z with prefix : r   )r   _remote_tmpr   r   
expanduser
expandvarsexistsmakedirsrL   OSErrorIOErrorr   timetempfilemkdtempr   _keep_remote_filesatexitregistershutilrmtree)r   basedirebasefiletmpdirs        r#   r   zAnsibleModule.tmpdir  s`   
 <<G+'',,RWW-?-?@P@P-QR"277>>'+BLKKe4 II A DK	K L /<H!))wG **v6!DL||/  ) #II>EyQR|U V"G# W% 079Q<Q   s0   D! E !E0"EEF.$FFc                B    t        |       | j                  d|z         y )Nz[WARNING] %s)rL   log)r   warnings     r#   rL   zAnsibleModule.warn  s    W')*rc   c                    ||t        d      t        ||||       || j                  d|d|       y | j                  d|d|       y )Nz=implementation error -- version and date must not both be setversiondatecollection_namez[DEPRECATION WARNING]  )AssertionErrorrI   r   )r   r	   r  r  r  s        r#   rI   zAnsibleModule.deprecate  sQ    4#3 !`aa#wT?S HHc4@AHHc7CDrc   c                   |"|j                  d|j                  dd            }|i S t        j                  j                  t        j                  j	                  |            }t        |d      }|j                  dd      rIt        j                  j                  |      r*t        j                  j                  |      }t        |      }|j                  dd      }|j                  d	d      }|j                  d
d      }|j                  dd      }|j                  dd      }|j                  dd      }	|j                  dd      }
|||	g}| j                         r|j                  |
       | j                  |      }t        t        |            D ]  }|||   dk(  s||   ||<    |j                  dd      }t        |||||||	|
||
      S )a  
        many modules deal with files, this encapsulates common
        options that the file module accepts such that it is directly
        available to all modules and they can share code.

        Allows to overwrite the path/dest module argument by providing path.
        Nr   destsurrogate_or_strictr   followFrV   rW   rX   rY   rZ   r\   r[   _defaultr]   )
r   rV   rW   rX   rY   rZ   r\   r[   	secontextr]   )getr   r   r   r   r   islinkrealpathr   selinux_mls_enabledappendselinux_default_contextrangert   dict)r   r   r   b_pathrV   rW   rX   rY   rZ   r\   r[   r  default_secontextir]   s                  r#   load_file_common_argumentsz(AnsibleModule.load_file_common_arguments  s    <::ffjj&>?D<I77%%bgg&8&8&>?D$'<= ::h&277>>&+AWW%%f-FV$Dzz&$'

7D)

7D) Hd+Hd+Hd+**Y-VV,	##%W% 88>s,-. 	4A}1!;03	!	4 ZZd3
DU&yZ
 	
rc   c                z    | j                   $t        xr t        j                         dk(  | _         | j                   S Nr
   )r   HAVE_SELINUXr   is_selinux_mls_enabledr   s    r#   r  z!AnsibleModule.selinux_mls_enabled=  s6    $$,(4(^9W9W9Y]^9^D%(((rc   c                z    | j                   $t        xr t        j                         dk(  | _         | j                   S r  )r   r  r   is_selinux_enabledr  s    r#   selinux_enabledzAnsibleModule.selinux_enabledC  s6      ($0$VW5O5O5QUV5VD!$$$rc   c                    | j                   4g d| _         | j                         r| j                   j                  d        | j                   S )NNNN)r   r  r  r  s    r#   selinux_initial_contextz%AnsibleModule.selinux_initial_contextJ  sB    ((0,>D)'')--44T:,,,rc   c                    | j                         }| j                         s|S 	 t        j                  t	        |d      |      }|d   dk(  r|S |d   j                  dd      }|S # t
        $ r |cY S w xY w)Nr  r   r   r
   rr   r   )r%  r"  r   matchpathconr   r   split)r   r   rV   contextrets        r#   r  z%AnsibleModule.selinux_default_contextS  s    ..0##%N	&&y>S'TVZ[C q6R<N a&,,sA&  	N	s   !A& &A43A4c                   | j                         }| j                         s|S 	 t        j                  t	        |d            }d   dk(  r|S |d   j                  d	d
      }|S # t
        $ rQ}|j                  t        j                  k(  r| j                  |d|z         n| j                  |d       Y d }~vd }~ww xY w)Nr  r   zpath %s does not existr   r	   z"failed to retrieve selinux contextr   r'  r
   rr   r   )
r%  r"  r   lgetfilecon_rawr   r   errnoENOENTr   r)  )r   r   r*  r+  r   s        r#   selinux_contextzAnsibleModule.selinux_contextb  s    ..0##%N	T)))DAV*WXC q6R<N a&,,sA&  	Tww%,,&D.F.MND.RS		Ts    A% %	B?.AB::B?c                    t        |d      }|r<t        j                  j                  t        j                  j	                  |            }t        j
                  |      }|j                  }|j                  }||fS )Nr  r   )r   r   r   r   r   lstatst_uidst_gid)r   r   expandr  stuidgids          r#   user_and_groupzAnsibleModule.user_and_groupt  s_    $'<=WW''(:(:6(BCFXXfiiiiSzrc   c           	        t         j                  j                  t        t         j                  j	                  t         j                  j                  |            d            }t         j                  j                  |      s?t         j                  j                  |      }t         j                  j                  |      s?t        |d      S )z
            Takes a path and returns its mount point

        :param path: a string type with a filesystem path
        :returns: the path to the mount point as a text type
        r  r   )	r   r   r  r   r   r   ismountdirnamer   )r   r   r  s      r#   find_mount_pointzAnsibleModule.find_mount_point}  s     !!(277+=+=bgg>P>PQU>V+W`u"vw''//&)WW__V,F ''//&) v&;<<rc   c                h   	 t        dd      }|j                         }|j                          | j	                  |      }|D ]`  }|j                  dd      \  }}}}	}
t        |      t        |      k(  s3| j                  D ]  }||v s| j                  |      }d|fc c S  b y# t        $ r Y yw xY w)z
        Returns a tuple containing (True, selinux_context) if the given path is on a
        NFS or other 'special' fs  mount point, otherwise the return will be (False, None).
        z/proc/mountsrFNr     T)	r   	readlinesr   r   r>  r)  r   _selinux_special_fsr1  )r   r   f
mount_datapath_mount_pointlinedevicemount_pointfstypeoptionsrestfsspecial_contexts                r#   is_special_selinux_pathz%AnsibleModule.is_special_selinux_path  s    
	!^S)AJGGI  006 	7D;?::c1;M8V[&'4()Xk-BB22 7BV|*.*>*>?O*P $o667	7   	! 	!s   ,B% %	B10B1c                n    | j                         s|S | j                  |      }| j                  ||d      S NF)r"  r  set_context_if_different)r   r   changedr*  s       r#   set_default_selinux_contextz)AnsibleModule.set_default_selinux_context  s8    ##%N..t4,,T7EBBrc   c                   | j                         s|S | j                  |      ry| j                  |      }t        |      }| j	                  |      \  }}|r|}nOt        t        |            D ]8  }	t        |      |	kD  s||	   ||	   ||	   k7  r	||	   ||	<   +||	   1||	   ||	<   : ||k7  rz|"d|vri |d<   ||d   d<   d|vri |d<   ||d   d<   	 | j                  ryt        j                  t        |      dj                  |            }

dk7  r| j                  |d	
       d}|S # t        $ r,}| j                  |dt        |      z  |||       Y d }~Ld }~ww xY w)NTbeforer  afterrr   zinvalid selinux context: %s)r   r	   new_contextcur_context	input_wasr   zset selinux context failedr-  )r"  check_file_absent_if_check_moder1  rn   rP  r  rt   r   r   lsetfileconr   ry   r   r   )r   r   r*  rT  diffrZ  rY  is_special_se
sp_contextr  rcr   s               r#   rS  z&AnsibleModule.set_context_if_different  s   ##%N//5**40;' '+&B&B4&H#
$K3{+, 8w<!#qz-'!*A2N)0A +)4QA8 +%4'%'DN.9X{+$&$&DM-8Wk*d??((4#((;:OP QwD.JKG  dD.KiXYl.Z+6K[b  d dds   D*  .D* *	E3"EEc                   ||S t        |d      }|r<t        j                  j                  t        j                  j	                  |            }| j                  |      ry| j                  ||      \  }}	 t        |      }	|	k7  rK|"d|vri |d<   ||d   d<   d|vri |d<   |	|d   d<   | j                  ry	 t        j                   ||	d	       d}|S # t        $ rT 	 t        j                  |      j                  }	n0# t        $ r$ t        |      }| j                  |d|z         Y nw xY wY w xY w# t"        t$        f$ r4}
t        |      }| j                  |d
t        |
      z         Y d }
~
d }
~
ww xY w)Nr  r   Tz'chown failed: failed to look up user %sr-  rW  rW   rX  r'  zchown failed: %s)r   r   r   r   r   r\  r:  intrv   pwdgetpwnampw_uidr   r   r   r   lchownr   r   )r   r   rW   rT  r^  r6  r  orig_uidorig_gidr8  r   s              r#   set_owner_if_differentz$AnsibleModule.set_owner_if_different  s   =N$'<=WW''(:(:6(BCF//7!00@(	ae*C s?4'%'DN*2Xw'$&$&DM),Wg&Q		&#r* G1  	aall5)00 avD.WZ_._`a	a( W% QvD.@GAJ.OPPQsN   8C :D5 	D2C?>D2?*D,)D2+D,,D21D25E8*E33E8c                   ||S t        |d      }|r<t        j                  j                  t        j                  j	                  |            }| j                  |      ry| j                  ||      \  }}	 t        |      }	|	k7  rK|"d|vri |d<   ||d   d<   d|vri |d<   |	|d   d<   | j                  ry	 t        j                   |d	|	       d}|S # t        $ rT 	 t        j                  |      j                  }	n0# t        $ r$ t        |      }| j                  |d|z         Y nw xY wY w xY w# t"        $ r! t        |      }| j                  |d
       Y w xY w)Nr  r   Tz(chgrp failed: failed to look up group %sr-  rW  rX   rX  r'  zchgrp failed)r   r   r   r   r   r\  r:  rc  rv   grpgetgrnamgr_gidr   r   r   r   rg  r   )
r   r   rX   rT  r^  r6  r  rh  ri  r9  s
             r#   set_group_if_differentz$AnsibleModule.set_group_if_different  su   =N$'<=WW''(:(:6(BCF//7!00@(	be*C s?4'%'DN*2Xw'$&$&DM),Wg&>		&"c* G1  	bbll5)00 bvD.X[`.`ab	b(  >vDn=>sH   8C :D5 	D2C?>D2?*D,)D2+D,,D21D25'EEc           	     :   ||S t        |d      }|r<t        j                  j                  t        j                  j	                  |            }| j                  |      ryt        j                  |      }t        |t              s	 t        |d      }t        j                  |j                         }	|	|k7  r\|(d|vri |d<   d	|	z  |d   d
<   d|vri |d<   d	|z  |d   d
<   | j"                  ry	 t%        t        d      rt        j&                  ||       nt        j                  j)                  |      st        j*                  ||       nt        j                  |      }
t        j*                  ||       t        j                  |      }|
j                   |j                   k7  r3t        j*                  |t        j                  |
j                                t        j                  |      }t        j                  |j                         }||	k7  rd}|S # t        $ r 	 | j                  ||      }n># t        $ r2}t        |      }| j                  |dt        |             Y d }~nd }~ww xY w|t        j                  |      k7  rt        |      }| j                  |d|       Y w xY w# t,        $ r}t        j                  j)                  |      r<|j.                  t.        j0                  t.        j2                  t.        j4                  fv rn.|j.                  t.        j6                  t.        j8                  fv rn Y d }~nd }~wt        $ rF}t        |      }| j                  |dt        |      t;        j<                                Y d }~d }~ww xY w)Nr  r   Tr   z&mode must be in octal or symbolic form)r   r	   detailsz6Invalid mode supplied, only permission info is allowedrW  0%03orV   rX  lchmodzchmod failedr   r	   rq  	exception)r   r   r   r   r   r\  r3  r   rc  r   _symbolic_mode_to_octalr   r   r   statS_IMODEst_moder   hasattrrs  r  chmodr   r/  EACCESEPERMEROFSr0  ELOOP	traceback
format_exc)r   r   rV   rT  r^  r6  r  	path_statr   	prev_modeunderlying_statnew_underlying_statnew_modes                r#   set_mode_if_differentz#AnsibleModule.set_mode_if_different(  s   <N$'<=WW''(:(:6(BCF//7HHV$	$$z4| LL!2!23	4'%'DN)09)<Xv&$&$&DM(/$Wf%A2x(IIfd+77>>&1.
 +-''&/..0ggfo+*226I6Q6QQHHVT\\/:Q:Q-RS" (I||I$5$56H9$E  z977	4HD  9"6?DNN'O+4Q< # 9 99 4<<--"6?DNN2jtxNyzZ  
77>>&)aggLLKKKK: /
 WWu{{ ;; AvDniPQl)2)=)=)?  A AAsb   H 1C)J0 	J-!H43J-4	I/=(I*%J-*I//:J-,J-0	N9B	MN;NNc                $   ||S t        |d      }|r<t        j                  j                  t        j                  j	                  |            }| j                  |      ry| j                  |d      }d}|j                  d      r
|d   }|d	d  }|r|j                  d
d      |k7  s|dk(  r| j                  d      }	|	r|	|||g}	d}|5d|vri |d<   |j                  d
      |d   d<   d|vri |d<   |||d   d<   | j                  s1	 | j                  |	      \  }
}}|
dk7  s|rt        d||z   z        	 |S |S # t        $ rD}| j                  t        |      dt        |      t!        j"                                Y d }~|S d }~ww xY w)Nr  r   TFinclude_version=)-+r   r
   
attr_flagsr   r  chattrrW  r]   rX  z"Error while setting attributes: %szchattr failedrt  )r   r   r   r   r   r\  get_file_attributes
startswithr  r-   r   run_commandr   r   r   r   r  r  )r   r   r]   rT  r^  r6  r  existingattr_modattrcmdra  outerrr   s                 r#   set_attributes_if_differentz)AnsibleModule.set_attributes_if_different}  s   N$'<=WW''(:(:6(BCF//7++FE+J  ,!!}H#ABJ8<<b9ZG8WZ?''1G"h
$CVL#t+)+X3;<<3MDN<0d*(*W<Dj2QDM,/_'+'7'7'@C7c"+,PTWZ]T],^"__ '*
 w % _GFO/8|yG[G[G] ' _ __s   -E 	F9F

Fc                h   i }| j                  dd      }|r|rdnd}|||g}	 | j                  |      \  }}}|dk(  rc|j                         }	d}
|rd}
|	d   j                         |d<   |	|
   j	                  dd	      j                         |d
<   t        |d
         |d<   |S |S # t        $ r Y |S w xY w)NlsattrFz-vdz-dr   r
   r  r  r   r  r]   )r-   r  r)  stripreplacer1   r   )r   r   r  r|   r  flagsra  r  r  resattr_flags_idxs              r#   r  z!AnsibleModule.get_file_attributes  s    ##He4,E$Et,G#//8C7))+C%&N&)*,/FLLNy)+.~+>+F+FsB+O+U+U+WF<(+<VL=Q+RF<( v  s   A=B$ $	B10B1c           
         t        j                  |j                        }|j                  d      D ]  }t        j                  |      }t        j                  |      }|j                  d      }|dk(  }|dk(  s|dk(  rd}t        j                  |      st        d|z        t        |      D ][  \  }	}
t        j                  |
      st        d|z        |D ].  }| j                  |||
||      }| j                  |||	   ||      }0 ]  |S )z
        This enables symbolic chmod string parsing as stated in the chmod man-page

        This includes things like: "u=rw-x+X,g=r-x+X,o=r-x+X"
        ,r   r   augoz$bad symbolic permission for mode: %s)rw  rx  ry  r)  MODE_OPERATOR_REfindallpopUSERS_REmatchrv   	enumeratePERMS_RE#_get_octal_mode_from_symbolic_perms_apply_operation_to_mode)rh   r  symbolic_moder  rV   permlistopersusers	use_umaskidxpermsusermode_to_applys                r#   rv  z%AnsibleModule._symbolic_mode_to_octal  s,    <<	 1 12 "'', 	gD (--d3H %,,T2E
 LLOE"I|u{ >>%( !G$!NOO (1 g
U~~e,$%Kd%RSS! gD$'$K$KIW[]bdmow$xM";;D%*m]efHgg/	g@ rc   c                P   |dk(  r| dk(  r"t         j                  t         j                  z  }nM| dk(  r"t         j                  t         j                  z  }n&| dk(  r!t         j
                  t         j                  z  }t        z  }||z  |z  }|S |dk(  r||z  }|S |dk(  r|||z  z
  }S )Nr  ugor  r  )rw  S_IRWXUS_ISUIDS_IRWXGS_ISGIDS_IRWXOS_ISVTX	PERM_BITS)r  operatorr  current_modemaskinverse_maskr  s          r#   r  z&AnsibleModule._apply_operation_to_mode  s    s?s{||dll2||dll2||dll2  )+L$|3}DH
 	 _#m3H  _#|m'CDHrc   c                z   |t        j                  | j                        }t        j                  | j                        }|t        z  dkD  }|xs |}t        j                  d      }t        j                  |       |t        z  }	|r7dt         j                  idt         j                  idt         j                  id}
nddiddiddid}
|r|	t         j                  z  nt         j                  |r|	t         j                  z  nt         j                  |r|	t         j                  z  nt         j                  t         j                  d|t         j                  z  |t         j                  z  dz  |t         j                   z  dz  d|r|	t         j"                  z  nt         j"                  |r|	t         j$                  z  nt         j$                  |r|	t         j                  z  nt         j                  t         j&                  d|t         j                  z  dz	  |t         j                  z  |t         j                   z  dz  d|r|	t         j(                  z  nt         j(                  |r|	t         j*                  z  nt         j*                  |r|	t         j                  z  nt         j                  dt         j,                  |t         j                  z  dz	  |t         j                  z  dz	  |t         j                   z  dd|
j/                         D ]  \  }}|   j1                  |        fd}t3        ||d      S )Nr   X)r  r  r  r      )r@  wxstr  r  r  c                    |    |   z  S Nrb   )rV   permr  user_perms_to_modess     r#   	or_reducezDAnsibleModule._get_octal_mode_from_symbolic_perms.<locals>.or_reduce<  s    -d3D999rc   )rw  rx  ry  S_ISDIRr4   r   umaskr  S_IXUSRS_IXGRPS_IXOTHS_IRUSRS_IWUSRr  r  r  r  S_IRGRPS_IWGRPr  S_IROTHS_IWOTHr  r   r   r   )r  r  r  r  r  is_directoryhas_x_permissionsapply_X_permissionr  	rev_umaskX_permskeyvaluer  r  s    `            @r#   r  z1AnsibleModule._get_octal_mode_from_symbolic_perms  s|   Y%6%67I||I$5$56&0A5)>->
 
I%	 4<<(4<<(4<<(G 1X1X1XG 2;Y-1:Y-1:Y-\\-$,,.14$,,.146 2;Y-1:Y-1:Y-\\$,,.14-$,,.146 2;Y-1:Y-1:Y-\\$,,.14$,,.14-/'
> "--/ 	3JC$++E2	3	: i**rc   c                   | j                  |d   |d   ||      }| j                  |d   |d   |||      }| j                  |d   |d   |||      }| j                  |d   |d   |||      }| j	                  |d   |d   |||      }|S )Nr   r  rW   rX   rV   r]   )rS  rj  ro  r  r  r   	file_argsrT  r^  r6  s        r#   set_fs_attributes_if_differentz,AnsibleModule.set_fs_attributes_if_differentA  s    //fy5w
 --fy17D&
 --fy17D&
 ,,fy0'4
 22fy6v
 rc   c                ^    | j                   xr  t        j                  j                  |       S r  )r   r   r   r   )r   	file_paths     r#   r\  z-AnsibleModule.check_file_absent_if_check_modeT  s!    @rww~~i'@#@@rc   c                *    | j                  ||||      S r  r  r  s        r#   %set_directory_attributes_if_differentz3AnsibleModule.set_directory_attributes_if_differentW      229gtVTTrc   c                *    | j                  ||||      S r  r  r  s        r#    set_file_attributes_if_differentz.AnsibleModule.set_file_attributes_if_differentZ  r  rc   c                   |j                  d|j                  dd            }||S t        |d      }t        j                  j	                  |      r[| j                  |      \  }}||d<   ||d<   	 t        j                  |      d   }	 t        j                  |      d   }||d	<   ||d
<   t        j                  |      }dt        j                  |t        j                           z  |d<   t        j                  j!                  |      rd|d<   nRt        j                  j#                  |      rd|d<   n-t        j                  |      j$                  dkD  rd|d<   nd|d<   | j'                         r#dj)                  | j+                  |            |d<   |t        j,                     |d<   |S # t        $ r t        |      }Y =w xY w# t        $ r t        |      }Y ?w xY w)z
        for results that are files, supplement the info about the file
        in the return path with stats about the file path.
        r   r
  Nr  r   r8  r9  r   rW   rX   rr  rV   linkstate	directoryr
   hardfilerr   r  size)r  r   r   r   r   r:  rd  getpwuidr   rO   rl  getgrgidr3  rw  rx  ST_MODEr  isdirst_nlinkr"  ry   r1  ST_SIZE)	r   rj   r   r  r8  r9  r  rX   r7  s	            r#   add_path_infozAnsibleModule.add_path_info]  s    zz&&**VT":;<M$'<=77>>&!,,T2JS#F5MF5M ||C(+!S)!, #F7O#F7O&!B$t||Bt||4D'EEF6Nww~~f%"(wv&"-w))A-"(w"(w##%&)hht/C/CD/I&J{#-F6N-   3x   !C!s$   3F1 G 1G	G	G$#G$c                   	 t        j                  t         j                  d       y# t         j                  $ rk t	        |       }t        j                  t         j                  |       |t
        j                  d<   |t
        j                  d<   |t
        j                  d<   Y yt        $ r;}| j                  dt        |      z  t        j                                Y d}~yd}~ww xY w)z
        Uses the locale module to test the currently set locale
        (per the LANG and LC_CTYPE environment settings)
        r   LANGLC_ALLLC_MESSAGESzLAn unknown error was encountered while attempting to validate the locale: %sr	   ru  N)locale	setlocaler  Errorr,   r   environr   r   r   r  r  )r   best_localer   s      r#   r   zAnsibleModule._check_locale  s    
	K V]]B/|| 
	4 348K V]]K8!,BJJv#.BJJx (3BJJ}% 	KNNm$Q< (3<3G3G3I  K K	Ks   $' A;C'$C',1C""C'c           	        || j                   }|| j                  }t        D ]  }d|z  }||v ri|t        v r*t	        | t        |   d   | j                  ||                nt	        | t        |   d   ||          || j                  v sg| j                  |= ut        | t        |   d         rt	        | t        |   d   t        |   d           y )Nz_ansible_%sr   r
   )r   r   r=   r>   setattrrH   rz  )r   r   module_parametersr   	param_keys        r#   r   z&AnsibleModule._set_internal_properties  s      ..M$ $ 	DA%)I--
?D)A,q/4<<@QR[@\3]^D)A,q/3DY3OP +I. tYq\!_5D)A,q/9Q<?C	Drc   c                    t        |||      S r  )rC   )r   r  localsinclude_exceptionss       r#   rC   zAnsibleModule.safe_eval  s    (:;;rc   c                "    t               | _        y)z read the input and set the params attribute.

        This method is for backwards compatibility.  The guts of the function
        were moved out in 2.1 so that custom modules could read the parameters.
        N)r   r   r  s    r#   r   zAnsibleModule._load_params  s     #nrc   c                   t         r~	 d| j                  z  }t        t        | j                  t        j
                        }t        j                  t        |      d|       t        j                  t        j                  |       y y # t        t        f$ r<}| j                  dt        |      z  t        j                         |       Y d }~y d }~ww xY w)N
ansible-%sr   zFailed to log to syslog (%s). To proceed anyway, disable syslog logging by setting no_target_syslog to True in your Ansible config.)r	   ru  
msg_to_log)
HAS_SYSLOGr   r   syslogr   r   openlogrO   LOG_INFO	TypeErrorrv   r   r   r  r  )r   r	   r   facilityr   s        r#   _log_to_syslogzAnsibleModule._log_to_syslog  s    %

2"64+@+@&//Rs6{Ax8foos3  z* :<EaLI (224"   s   A<B C2CCc                F    | j                   r| j                  d|z         y y )Nz
[debug] %s)r   r   )r   r	   s     r#   debugzAnsibleModule.debug  s    ;;HH\C'( rc   c                   | j                   s|
t               }d| j                  z  }t        |t              r|j                  dd      }t        |t        t        f      st        dt        |      z        t        |t              r|j                  dd      }n|}| j                  rdj                  | j                  |g      }t        || j                        }t        rdt        j                  j!                  t"              fg}|D ]<  }|j%                         t        ||         }}|dv rd|z  }|j'                  ||f       > 	 t(        rSt+        t,        | j.                  t,        j0                        d	z	  }	t3        j4                  d|d||	d
t        |       n&t3        j4                  dd|d|it        |       y y | j9                  |       y y # t6        $ r | j9                  |       Y y w xY w)Nr  r   r  zmsg should be a string (got %s)r  MODULE)	PRIORITYMESSAGE
MESSAGE_ID	CODE_FILE	CODE_LINE	CODE_FUNCSYSLOG_FACILITYSYSLOG_IDENTIFIER
SYSLOG_PIDz_%sr   )r  r#  r  rb   )r   r  r   r   bytesr   rO   r  rN   _target_log_infory   r:   r{   has_journalr   r   r   r   upperr  r  r   r  r   r   r   sendr   r  )
r   r	   log_argsr   journal_msgjournal_argsargr   r  r  s
             r#   r   zAnsibleModule.log  s   {{6!DJJ.F&%(w	: cE3<0 ADI MNN #u%!jj)< "$$!hh(=(={'KL (T5G5GHK!)277+;+;H+E FG# 	7C#&99;HSM0B%D     %t| ''u6	75! $+6+/+@+@+1??$<?@$A   ;-M5=;'+L'9;   ;-M ;'+L'9;	; ##K0u l  5''45s   
A>G G:9G:c           	        t               }| j                  D ]   }| j                  j                  ||      }| j                  j                  |i       }|j                  dd      }|/t
        j                  |      rd||<   | j                  d|z         | j                  |      rd||<   | j                  |   }t        |t        t        f      st        |      }n!t        |t              r|j                  d      }t        || j                        ||<    |j                         D cg c]  \  }}t!        |      dt!        |        }	}}|	rdd	j#                  |	      z  }	nd
}	| j%                  |	|       yc c}}w )z! log that ansible ran the module r   NNOT_LOGGING_PASSWORDz Module did not set no_log for %sNOT_LOGGING_PARAMETERr   r  zInvoked with %sr  Invoked)r+  )r  r   rQ   r  r   PASSWORD_MATCHsearchrL   rH   r   rO   r&  r   r   r{   r   r   ry   r   )
r   r+  paramcanonarg_optsr   	param_valr.  valr	   s
             r#   r   zAnsibleModule._log_invocation  sV    6[[ 	XELL$$UE2E))--eR8H\\(D1F ~."7"7">"8		<uDEf%"9 KK.	!)c5\: #II	3/ ) 0 0 9I"8DDVDV"W#	X& KS..JZ[hc3)C.)C.9[[#chhsm3CCx( \s   .#Fc                   	 t        j                         }t        j                  |t         j                  t         j                  z        s
t               |S # t
        $ r | j                  t         j                  j                  d      t        j                         fD ]b  }	 t        j                  |t         j                  t         j                  z        rt        j                  |       |c cY S U# t
        $ r Y `w xY w Y y w xY w)Nz$HOME)r   getcwdaccessF_OKR_OKr   r   r   r   r   
gettempdirchdir)r   cwds     r#   r   zAnsibleModule._set_cwd4  s    	))+C99S"''BGG"34k!J 		 RWW%7%7%@(BUBUBWX yybgg&78"
 9 !  		s>   AA A
D"AC3-D2D3	C?<D>C??DDc                    d}	 t        ||      }|S # t        $ r(}|r| j                  t        |             Y d}~|S d}~ww xY w)a  
        Find system executable in PATH.

        :param arg: The executable to find.
        :param required: if the executable is not found and required is ``True``, fail_json
        :param opt_dirs: optional list of directories to search in addition to ``PATH``
        :returns: if found return full path; otherwise return original arg, unless 'warning' then return None
        :raises: Sysexit: if arg is not found and required=True (via fail_json)
        N)r.  opt_dirsr   )r-   rv   r   r   )r   r.  requiredrC  bin_pathr   s         r#   r-   zAnsibleModule.get_bin_pathH  sN     	/#h?H
 	  	/71:.		/s    	A?Ac                    ||S 	 t        |      S # t        $ r%}| j                  t        |             Y d}~yd}~ww xY w)z!Convert the argument to a booleanNr   )rH   r  r   r   )r   r.  r   s      r#   rH   zAnsibleModule.boolean\  s@    ;J	-3< 	-NNy|N,,	-s   
 	?:?c                |    	 t        |      S # t        $ r%}| j                  t        |             Y d }~y d }~ww xY wNr   )r   UnicodeErrorr   r   )r   rz   r   s      r#   r   zAnsibleModule.jsonifyf  s6    	+4=  	+NNwqzN**	+s   
 	;6;c                ,    t        j                  |      S r  )r   r   )r   rz   s     r#   	from_jsonzAnsibleModule.from_jsonl  s    zz$rc   c                X    || j                   vr| j                   j                  |       y y r  )r   r  r   r   s     r#   add_cleanup_filezAnsibleModule.add_cleanup_fileo  s)    t)))%%d+ *rc   c                H    | j                   D ]  }| j                  |        y r  )r   cleanuprM  s     r#   do_cleanup_fileszAnsibleModule.do_cleanup_filess  s#    && 	DLL	rc   c           	        | j                  |       d|vrd| j                  i|d<   d|v rCt        |d   t              r|d   D ]  }| j	                  |        n| j	                  |d          t               }|r||d<   d|v rt        |d   t              r|d   D ]  }t        |t              r(t        |      dk(  r| j                  |d   |d          ;t        |t              rF| j                  |d	   |j                  d
      |j                  d      |j                  d             | j                  |        n| j                  |d          t               }|r||d<   i }|j                         D ]  \  }}|t        |t              s|||<    t        || j                        }|j!                  |       t#        d| j%                  |      z         y )N
invocationmodule_argswarningsdeprecations   r   r
   )r  r	   r  r  r  r  z
%s)r  r   r   rn   rL   rK   SEQUENCETYPErt   rI   r&   r  rJ   r   rR   r:   r{   r   r   r   )	r   rj   r  rU  drV  	preservedr   r   s	            r#   _return_formattedzAnsibleModule._return_formattedw  s   6"v%$14;;#?F< &,d3
+ !AIIaL! 		&,-')!)F:V#&0$7/ *A!!\2s1v{qtQqT:#Aw/qxy9IPQPUPUV\P]78uu=N7O ' Q q)* vn56/1%1F>" 	LLN 	!DAqyJq$/ 	!	!
 vt'9'9: 	i ft||F++,rc   c                p    | j                          | j                  |       t        j                  d       y)z' return from the module, without error r   N)rQ  r[  r   r   )r   rj   s     r#   r   zAnsibleModule.exit_json  s)     	v&rc   c                h   d|d<   ||d<   d|vrnt        j                         d   rW| j                  s| j                  dk\  r<dj	                  t        j                  t        j                         d               |d<   | j                          | j                  |       t        j                  d       y	)
z/ return from the module, with an error message Tr   r	   ru  rW  r   r   r
   N)
r   exc_infor   r   ry   r  	format_tbrQ  r[  r   )r   r	   rj   s      r#   r   zAnsibleModule.fail_json  s      xu f$):tbcOc"$'')*=*=cllnQ>O*P"QF;v&rc   c                    |sy 	 t        | j                  |       y # t        $ r%}| j                  t	        |             Y d }~y d }~ww xY wrH  )rB   r   r  r   r   )r   required_paramsr   s      r#   fail_on_missing_paramsz$AnsibleModule.fail_on_missing_params  s@    	-$T[[/B 	-NNy|N,,	-s    	A
AA
c                ~   t        |d      }t        j                  j                  |      syt        j                  j	                  |      r| j                  d|z         t        |d      r|}n	 t        |          }d}t        t        j                  j                  |      d      }|j                  |      }|r%j                  |       |j                  |      }|r%|j                          j                         S # t        $ r1 | j                  d|d|d	d
j                  t                     Y w xY w)zh Return hex digest of local file for a digest_method specified by name, or None if file is not present. r  r   Nz+attempted to take checksum of directory: %sr   	hexdigestzCould not hash file 'z' with algorithm 'z'. Available algorithms: z, i   r   )r   r   r   r   r  r   rz  AVAILABLE_HASH_ALGORITHMSr   ry   r   r  r   r   r   rd  )r   filename	algorithm
b_filenamedigest_method	blocksizeinfileblocks           r#   digest_from_filezAnsibleModule.digest_from_file  s   h/DE
ww~~j)77==$NNLxWNX 9k*%M` 9) D F
 	bgg&&z2D9I&  'KK	*E  	&&((  `$,iC\9]$_ ``s   2D 7D<;D<c                L    dt         vrt        d      | j                  |d      S )a   Return MD5 hex digest of local file using digest_from_file().

        Do not use this function unless you have no other choice for:
            1) Optional backwards compatibility
            2) Compatibility with a third party protocol

        This function will not work on systems complying with FIPS-140-2.

        Most uses of this function can use the module.sha1 function instead.
        md5z1MD5 not available.  Possibly running in FIPS mode)re  rv   rm  r   rf  s     r#   ro  zAnsibleModule.md5  s,     11PQQ$$Xu55rc   c                &    | j                  |d      S )z@ Return SHA1 hex digest of local file using digest_from_file(). sha1rm  rp  s     r#   rr  zAnsibleModule.sha1  s    $$Xv66rc   c                &    | j                  |d      S )zC Return SHA-256 hex digest of local file using digest_from_file(). sha256rs  rp  s     r#   ru  zAnsibleModule.sha256  s    $$Xx88rc   c                   d}t         j                  j                  |      rlt        j                  dt        j
                  t        j                                     }|dt        j                         d|}	 | j                  ||       |S |S # t        j                  t        f$ r/}| j                  d|d|dt        |             Y d}~|S d}~ww xY w)	z[make a date-marked backup of the specified file, return True or False on success or failurer   z%Y-%m-%d@%H:%M:%S~r   zCould not make backup of  to r   r   N)r   r   r   r   strftime	localtimegetpidpreserved_copyr   r  r   r   r   )r   fn
backupdestextr   s        r#   backup_localzAnsibleModule.backup_local  s     
77>>"-- 4dnnTYY[6QRC')299;<Jm##B
3 z LL'* mbR\^ghi^j#kllms   :B C)$CCc           	         t         j                  j                  |      r	 t        j                  |       y y # t        $ r8}t
        j                  j                  d|dt        |             Y d }~y d }~ww xY w)Nzcould not cleanup r   )	r   r   r   unlinkr   r   stderrwriter   )r   tmpfiler   s      r#   rP  zAnsibleModule.cleanup
  s\    77>>'"W		'" #  W

  w	RS!UVVWs   8 	A9.A44A9c                   t        j                  ||       | j                         r$| j                  |      }| j	                  ||d       	 t        j                  |      }t        j                  |      }|r]|j                  |j                  k7  s|j                  |j                  k7  r+t        j                  ||j                  |j                         | j                  |d      }|j                  dd      }| j                  ||d       y# t        $ r(}|j                  t        j                  k7  r Y d}~ed}~ww xY w)z=Copy a file with preserved ownership, permissions and contextFNr  r  r   T)r   copy2r"  r1  rS  r   rw  r4  r5  chownr   r/  r}  r  r  r  )r   srcr
  r*  	dest_stattmp_statr   current_attribss           r#   r{  zAnsibleModule.preserved_copy  s    	S$ !**3/G))$?	Iwwt}Hhoo1A1AAX__XaXhXhEhy//1A1AB 2232N)--lB?((E  	ww%++% &	s   B	D 	D?D::D?c                (   d}d}t        |d      }t        |d      }t        j                  j                  |      r|r	 t        j                  |      }t        j
                  ||j                  |j                         t        j                  ||       t        j                  |t        j                         t        j                         f       | j                         r3| j!                  |      }n!| j                         r| j#                  |      }t        j                  j                  |       }
	 t        j$                  ||       |
rt        jP                  d      }t        jP                  |       t        jR                  |tT        | z         t        j                  t        j                  j9                  |            }	 |jV                  t        jX                  z  r4t        j
                  |t        jZ                         |j                         n;t        j
                  |t        jZ                         t        j\                                | j                         r| jK                  ||d       yy# t        $ r)}	|	j                  t        j                  k7  r Y d}	~	d}	~	ww xY w# t&        t        f$ r}	|	j                  t        j                  t        j(                  t        j*                  t        j,                  t        j.                  fvr9| j1                  d|d|dt3        |	      t5        j6                                n"t        j                  j9                  |      }t        j                  j;                  |      }d}d}	 t=        j>                  d	||
      \  }}nJ# t        t&        f$ r8}	dt        j                  j9                  |      dt3        |	      }Y d}	~	nd}	~	ww xY w|r}|r| jA                  ||       nh| j1                  |t5        j6                                nB# |r;|r| jA                  ||       w | j1                  |t5        j6                                w w xY w|rt        |d      }	 	 t        jB                         	 t        jD                  |||rt        jF                  nt        jH                         n># t        $ r2 |rt        jF                  ||       nt        jH                  ||       Y nw xY w| j                         r| jK                  ||d       	 t        j                  |      }|r|r]|j                  |j                  k7  s|j                  |j                  k7  r+t        j
                  ||j                  |j                         t        j                  |t        j                         t        j                         f       n4# t        $ r(}	|	j                  t        j                  k7  r Y d}	~	nd}	~	ww xY w	 t        j$                  ||       n# t        jL                  t        t&        f$ rv}	|r0|	j                  t        j.                  k(  r| jA                  ||       n:| j1                  d|d|d|dt3        |	      t5        j6                                Y d}	~	nd}	~	ww xY wnw# t        jL                  t        t&        f$ rV}	|r| jA                  ||       n7| j1                  d|d|dt3        |	      t5        j6                                Y d}	~	nd}	~	ww xY w| jO                  |       n# | jO                  |       w xY wY d}	~	.d}	~	ww xY w# t        $ r Y 8w xY w)a  atomically move src to dest, copying attributes from dest, returns true on success
        it uses os.rename to ensure this as it is an atomic operation, rest of the function is
        to work around limitations, corner cases and ensure selinux context is saved if possibleNr  r   )timeszCould not replace file: rw  r   r  s   .ansible_tmp)r   r   suffixzThe destination directory (z2) is not writable by the current user. Error was: )copy_functionFzUnable to make z	 into to z, failed final rename from zFailed to replace file: r   )/r   r   r   r   rw  r  r4  r5  r   copystatutimer   r   r/  r}  r"  r1  r  renamer   EXDEVr|  ETXTBSYEBUSYr   r   r  r  r=  r   r   mkstemp_unsafe_writesr   movecopyr  rS  r  rP  r  r{  r5   ry  r  geteuidgetegid)r   r  r
  r^   keep_dest_attrsr*  r  b_srcb_destr   creating
b_dest_dirb_suffix	error_msgtmp_dest_nametmp_dest_fdb_tmp_dest_namer  r  dest_dir_stats                       r#   atomic_movezAnsibleModule.atomic_move4  s    	%:;$'<=77>>&!oGGFO		 0 0)2B2BC.tyy{DIIK&@A ##%..t4##%66t<ww~~f--G	6IIeV$N  HHQKEHHUOHHV^uf45GGBGGOOF$;<M ((4<<7HHVRZZ\=3G3GHHHVRZZ\2::<@ !))$? "K  77ekk) * ! D	6wwu{{EKKu}}V[VaVabb SRVXabcXd#eqz  rF  rF  rH  I
  WW__V4
77++F3 	 $	\191A1A^hqy1z.K) \wyw~w~  xG  xG  HL  xM  OX  YZ  O[  !\I\ !( //v> NNyIDXDXDZN[	 !( //v> NNyIDXDXDZN[	 ! !&.}EZ&[O)6&YHH[1I &E?apRXR]R]v|  wC  wC  !D#* I $3$*KK$G$*LL$HI  $335 $ = =$3We!E*+-77?+C#2'0hooIYIY6Y]e]l]lpy  qA  qA  ^A(*)BRBRT]TdTd(e$&HH_TYY[RVR[R[R]D^$_#* *#$77ekk#9$) $:*E "		/6 B$*LL'7#C E#0QWW5K$($7$7$P$(NN8;T?T]^_T`8bmv  nB  nB  nD %3 %E	E !'gw? Y, $ 3 3E6 B $]`bfhqrsht3u  BK  BV  BV  BX  !Y	Y _5_5ID	6d   s8  BI= 8J2 B^ =	J/J**J/2^C]<N32P:3O:.O50P:5O::P:==]<:?Q99]<[$8S[8T[T&[?B3W32[3	X$<X[X$$[(X?>[?[A,[	[[[] ]5A]] ]] ]< ]33]<<^	^^c                   	 d x}}	 t        |d      }t        |d      }t        j                  ||       |r|j                          |r|j                          y y # |r|j                          |r|j                          w w xY w# t        j                  t
        t        f$ rA}| j                  d|d|dt        |      t        j                                Y d }~y d }~ww xY w)Nwbr   zCould not write data to file (z) from (z): r  )r   r   copyfileobjr   r  r   r   r   r   r  r  )r   r  r
  out_destin_srcr   s         r#   r  zAnsibleModule._unsafe_writes  s    	= $$Hv#d+c4""684NN$LLN  NN$LLN gw/ 	=NNTXZ]_hij_kl%.%9%9%;  = =	=s-   B .A $B 'BB C$#7CC$c                $   | j                   s|}t        |t              rt        |      }t        |t        t        f      rt        j                  |      }g }d}d |D        D ]  }|rd}|j                  d       t        j                  |      r0|j                  d      }|dkD  r|j                  d|d | z         \d}t        || j                        }|j                  |        dj                  d	 |D              | _         | j                   S )
NFc              3  2   K   | ]  }t        |        y wr  )r   ).0r  s     r#   	<genexpr>z,AnsibleModule._clean_args.<locals>.<genexpr>  s     <	!<s   rs   r  r'  z%s=********Tr  c              3  F   K   | ]  }t        j                  |        y wr  )shlexquote)r  r.  s     r#   r  z,AnsibleModule._clean_args.<locals>.<genexpr>  s     "J5;;s#3"Js   !)r   r   r&  r   rO   r  r)  r  PASSWD_ARG_REr  findr   r{   ry   )r   ri   to_clean_args
clean_args	is_passwdr.  sep_idxs          r#   _clean_argszAnsibleModule._clean_args  s    {{ !M$& '$e- %M :JI<m< ' %I%%j1 &&s+!hhsmG|"))-#hw-*GH $(	,S$2D2DE!!#&' (("Jz"JJDK{{rc   c                L   d| _         t        |t        t        t        f      sd}| j                  d||       d}|	rt        |t              r>dj                  |D cg c]"  }t        t        j                  |      d      $ c}      }nt        |d      }|rt        |d      }|d	|g}n| j                  d
vrt        | j                  d      d	|g}nd}nt        |t        t        f      r t        j                  t        |d            }|rX|D cg c]L  }|t        t        j                  j                  t        j                  j!                  |            d      N }}n|D cg c]  }|t        |d       }}d}|
rBt        |
t              rt        |
d      }
	 t#        j$                  |
t"        j&                        }d}d}d}t        j*                  j-                         }|j/                  | j0                  xs i        |j/                  |xs i        |r$|j3                  dd      }|r|d||d<   n||d<   d|v rf|d   j                  d      D cg c](  }|r$|j5                  d      s|j5                  d      s|* }}|rt7        |      rdj                  |      |d<   |rt8        j:                  }fd}t=        ||||t8        j:                  t8        j:                  ||      }|r||d<   |rt        t        j                  j?                  t        j                  j                  |            d      }t        j                  jA                  |      r||d<   n|s| j                  d|z         	 | jB                  r#| jE                  d| jG                  |      z          t9        jH                  |fi |}|r ||       d} d}!tK        tL        d      rtM        jN                         }"ntM        jP                         }"|r"|s|dz  }t        |t              rt        |      }|"jS                  |jT                  tL        jV                         |"jS                  |jX                  tL        jV                         t        jZ                  dk(  rt]        j\                  |jT                  j_                         t\        j`                  t]        j\                  |jT                  j_                         t\        jb                        t        jd                  z         t]        j\                  |jX                  j_                         t\        j`                  t]        j\                  |jX                  j_                         t\        jb                        t        jd                  z         |r5|jf                  ji                  |       |jf                  jk                          	 |"jm                  d       }#d}$|#D ]~  \  }%}&|%jn                  jq                  d!      }'|'s|"js                  |%jn                         ?|%jn                  |jT                  k(  r| |'z  } d}$`|%jn                  |jX                  k(  sz|!|'z  }! |r*|$r(|ju                  |       r|s|rtw        | ||"      } d| d#fS |#r|"jy                         s|j{                         n3|"jy                         s!|j{                         |j}                          n|jT                  jk                          |jX                  jk                          |"jk                          |j~                  }|dk7  rK|rIt        !j                         | j                        }| j                  | jG                  |      | |!|(       ||tw         ||"      tw        !||"      fS | !fS c c}w c c}w c c}w # t"        j(                  $ r | j                  d       Y .w xY wc c}w # t        t        f$ rw}(| jE                  d$| jG                  |      d%tw        |(             |r9| j                  |(j                  ddtw        |(      | jG                  |      &       n|(Y d}(~(5d}(~(wt        $ r}(| jE                  d$| jG                  |      d%tw        t        j                                      |rB| j                  dddtw        |(      t        j                         | jG                  |      '       n|(Y d}(~(d}(~(ww xY w))a$  
        Execute a command, returns rc, stdout, and stderr.

        The mechanism of this method for reading stdout and stderr differs from
        that of CPython subprocess.Popen.communicate, in that this method will
        stop reading once the spawned command has exited and stdout and stderr
        have been consumed, as opposed to waiting until stdout/stderr are
        closed. This can be an important distinction, when taken into account
        that a forked or backgrounded process may hold stdout or stderr open
        for longer than the spawned command.

        :arg args: is the command to run
            * If args is a list, the command will be run with shell=False.
            * If args is a string and use_unsafe_shell=False it will split args to a list and run with shell=False
            * If args is a string and use_unsafe_shell=True it runs with shell=True.
        :kw check_rc: Whether to call fail_json in case of non zero RC.
            Default False
        :kw close_fds: See documentation for subprocess.Popen(). Default True
        :kw executable: See documentation for subprocess.Popen(). Default None
        :kw data: If given, information to write to the stdin of the command
        :kw binary_data: If False, append a newline to the data.  Default False
        :kw path_prefix: If given, additional path to find the command in.
            This adds to the PATH environment variable so helper commands in
            the same directory can also be found
        :kw cwd: If given, working directory to run the command inside
        :kw use_unsafe_shell: See `args` parameter.  Default False
        :kw prompt_regex: Regex string (not a compiled regex) which can be
            used to detect prompts in the stdout which would otherwise cause
            the execution to hang (especially if no input data is specified)
        :kw environ_update: dictionary to *update* environ variables with
        :kw umask: Umask to be used when running the command. Default None
        :kw encoding: Since we return native strings, on python3 we need to
            know the encoding to use to transform from bytes to text.  If you
            want to always get bytes back, use encoding=None.  The default is
            "utf-8".  This does not affect transformation of strings given as
            args.
        :kw errors: Since we return native strings, on python3 we need to
            transform stdout and stderr from bytes to text.  If the bytes are
            undecodable in the ``encoding`` specified, then use this error
            handler to deal with them.  The default is ``surrogate_or_strict``
            which means that the bytes will be decoded using the
            surrogateescape error handler if available (available on all
            python3 versions we support) otherwise a UnicodeError traceback
            will be raised.  This does not affect transformations of strings
            given as args.
        :kw expand_user_and_vars: When ``use_unsafe_shell=False`` this argument
            dictates whether ``~`` is expanded in paths and environment variables
            are expanded before running the command. When ``True`` a string such as
            ``$SHELL`` will be expanded regardless of escaping. When ``False`` and
            ``use_unsafe_shell=False`` no path or variable expansion will be done.
        :kw pass_fds: When running on Python 3 this argument
            dictates which file descriptors should be passed
            to an underlying ``Popen`` constructor. On Python 2, this will
            set ``close_fds`` to False.
        :kw before_communicate_callback: This function will be called
            after ``Popen`` object will be created
            but before communicating to the process.
            (``Popen`` object will be passed to callback as a first argument)
        :kw ignore_invalid_cwd: This flag indicates whether an invalid ``cwd``
            (non-existent or not a directory) should be ignored or should raise
            an exception.
        :kw handle_exceptions: This flag indicates whether an exception will
            be handled inline and issue a failed_json or if the caller should
            handle it.
        :returns: A 3-tuple of return code (integer), stdout (native string),
            and stderr (native string).  On python2, stdout and stderr are both
            byte strings.  On python3, stdout and stderr are text strings converted
            according to the encoding and errors parameters.  If you want byte
            strings on python3, use encoding=None to turn decoding to text off.
        Nz5Argument 'args' to run_command must be list or stringi  )ra  cmdr	   F    r  r   s   -c)Nz/bin/shTr   z6invalid prompt regular expression given to run_commandr   r   PATHr   rr   
PYTHONPATHz/ansible_modlib.zipz
/debug_dirc                 6     rt        j                          y y r  )r   r  )r  s   r#   preexecz*AnsibleModule.run_command.<locals>.preexecq  s     rc   )r   shell	close_fdsr   stdoutr  
preexec_fnenvpass_fdsrA  z)Provided cwd is not a valid directory: %szExecuting: rc   PollSelector
posixr
   i   )encodingr   zQA prompt was encountered while running a command, but no input data was specifiedzError Executing CMD:z Exception:)ra  r  r  r	   r  )ra  r  r  r	   ru  r  )r  ra  r  r  r	   )Ir   r   rn   r&  rO   r   ry   r   r  r  r   r)  r   r   r   r   r   recompile	MULTILINEr   r  r  r   r   r  endswithany
subprocessPIPEr  abspathr  r   r   r  Popenrz  	selectorsr  SelectSelectorr   r  
EVENT_READr  r   fcntlfilenoF_SETFLF_GETFL
O_NONBLOCKr   r  r   selectfileobjr   
unregisterr4  r   get_mappollwait
returncoder   r   r/  r   r  r  r   rstripr{   ))r   ri   check_rcr  r   rz   binary_datapath_prefixrA  use_unsafe_shellprompt_regexenviron_updater  r  r   expand_user_and_varsr  before_communicate_callbackignore_invalid_cwdhandle_exceptionsr	   r  r  	prompt_rera  st_inr  r   pypathsr  rj   r  r  r  selectoreventsstdout_changedr  eventb_chunkr   s)               `                            r#   r  zAnsibleModule.run_command  s   T $uc 23ICNNctN5 $%yybf!g]^(5;;q>BW"X!gh-BC %j9NO
"E40$55 5JKUTXY $-{{748I#JK $sw  Jno{|  |I!3!3BGG4F4Fq4I!JShi  J  JKOaaSTS`+@Aaa	,,'=NO]JJ|R\\B	 jjoo

4228b9

>'R(7762&D)4d;F)F 3"%l"3"9"9#"> 5Q::&;<::l3  5G 5 3w<$'HHW$5L!OOE	  !????	
 !)F: 277??277+=+=c+BCLabCww}}S! #u'#NQT#TUZ	{{)9)9$)??@""4262C*+C0FF y.1$113$335"DLDdC(#D>Dcjj)*>*>?cjj)*>*>?ww'!CJJ--/CJJL]L]L_afanan@ortrr@  ACJJ--/CJJL]L]L_afanan@ortrr@  A		%		!
 "+!&"( *JC!kk..u5G" ++CKK8

2'))-

2')* I4D4DV4LUY!*6HV!T)|}} h&6&6&8chhj>T
 "))+
0BHHJ I L JJJJNNB 7x($:L:LMCNNt//5"VTZ`cNd	&8FKfxGI I FF##{ "h& Ja 88 ]#[\]25d ! 	HHd>N>Nt>TV_`aVbcd !''#cyQR|Y]YiYijnYop q  	HHd>N>Nt>TV_`i`t`t`vVwxy #c#9Q<[d[o[o[qw{  xH  xH  IM  xN  O O	sk   '__	A	_	._6_)$_ %-_?K.` 72` *B&` %_<;_<d#A,bd#Bdd#c                    t         j                  j                  t         j                  j                  |            }t	        |d      }|j                  |       |j                          y )Nr  )r   r   r   r   r   r  r   )r   rf  rO   fhs       r#   append_to_filezAnsibleModule.append_to_file  sE    77%%bgg&8&8&BC(C 


rc   c                    t        |      S r  )r   )r   r  s     r#   r   zAnsibleModule.bytes_to_human  s    d##rc   c                    t        ||      S r  )r   )r   numberisbitss      r#   r   zAnsibleModule.human_to_bytes  s    ff--rc   c                    	 t        j                   | d      }|S # t        $ r( 	 t        j                  }Y |S # t        $ r d}Y Y |S w xY ww xY w)Ni  i(#  )r  r   r  PIPE_BUF)r   buffer_sizes     r#   get_buffer_sizezAnsibleModule.get_buffer_size  sb    	#++b$/K   	##$oo   #"#		#s$    	A7AAAA)	FFNNNFFNNr$  r  )r   )T)NTNNrR  rA  )FT)FTNNFNNFNNNr   r  TNNTT)F)F__name__
__module____qualname__r   propertyr   rL   rI   r  r  r"  r%  r  r1  r:  r>  rP  rU  rS  rj  ro  r  r  r  classmethodrv  staticmethodr  r  r  r\  r  r  r  r   r   rC   r   r  r  r   r   r   r-   rH   r   rK  rN  rQ  r[  r   r   rb  rm  ro  rr  ru  r  rP  r{  r  r  r  r  r  r   pretty_bytesr   r0   r  rb   rc   r#   r   r   h  s   BG<@<AJNeN $ $L+	E/
n)%-$=0C+Z'R'RSj)V( * *X  $ B+ B+H&AUU&PK0D.<% )<1|)B((-+ ,0-d-)8679 W!FFu@n=$> FJ zO LPR$h$ "L. "M rc   r   c                     t         j                  j                  t         j                  j                  t                    S r  )r   r   r=  r  r   rb   rc   r#   get_module_pathr    s$    77??277++H566rc   c           	        | dk(  r	ddl m} |}n| dv r	ddlm} |}n| dk(  rddl}|}n| dk(  rddl}|}n| d	k(  rddl}|}n| d
k(  r	ddlm} |}nq| dk(  r	ddlm	} |}nc| dv r ddl
}	t        |	j                  d      |       }n?| dk(  rt        }n3| dk(  rt        j                  }nt!        d| dt"         dt$        d      t'        d|  dt"         dd       |S )z(Inject import-time deprecation warnings.get_exceptionr   )r  >   literal_eval_literal_eval)r  datetimeNsignaltypeschain)r  repeat)r  >   bPY2PY3	iteritems	test_typebinary_typestring_typesinteger_typeszansible.module_utils.sixmapshlex_quotezcannot import name z from 'z' ()zImporting 'z' from 'z' is deprecated.z2.21)r	   r  )ansible.module_utils.pycompat24r  astr  r  r  r  	itertoolsr  r  	importlibr   import_moduler  r  r  AttributeErrorr  r   rI   )
importable_namer  
importabler  r  r  r  r  r  r#  s
             r#   __getattr__r(    s   /)A"
	=	=$!
	J	&
	H	$
	G	#
	G	##
	H	$$
	  
 	##$>?

 
E	!
	M	)[[
!/!5 6Jc(a1
 	

 /*(8*<LM rc   r  r  )
__future__r   r   r   _PY_MINversion_infor   dumpsr  ry   r  rO   r  
splitlinesr   __main__r   r/  rl  r  r  r   rd  r`   r  r  r  r  r   rw  r  r   r   r  	functoolsr   r  r  ImportErrorsystemdr   r   systemd_daemonrz  bootedr(  r%  r  ansible.module_utils.compatr   rN   NoneType_textr   r   r   +ansible.module_utils.common.text.convertersr   r   json_dict_unicode_to_bytesr   json_dict_bytes_to_unicode$ansible.module_utils.common.arg_specr   +ansible.module_utils.common.text.formattersr   r   r   r   r   r$   re  .ansible.module_utils.six.moves.collections_abcr%   r&   r'   r(   r)   r*   r+   "ansible.module_utils.common.localer,   #ansible.module_utils.common.processr-    ansible.module_utils.common.filer.   r  r/   DEFAULT_PERMr0   r1   r2   r3   r4   r5   $ansible.module_utils.common.sys_infor6   r7   r8   &ansible.module_utils.common.parametersr9   r:   r;   r<   r=   r>   ansible.module_utils.errorsr?   r@   rA   &ansible.module_utils.common.validationrB   rC   "ansible.module_utils.common._utilsrD   ro   )ansible.module_utils.parsing.convert_boolrE   rF   rG   rH   $ansible.module_utils.common.warningsrI   rJ   rK   rL   	frozensetrX  r  Ir3  imapr   r   r  r  r  r  rd   rl   r   r   r   objectr   r  r(  rb   rc   r#   <module>rL     s7  
 #  
 g	*$**T@#cSZJ[A\@]]pqsqxqxy|  zE  zE  zP  zP  zR  rS  qT  U   CHHQK    
   	 
  	          J9 '7+G0E0E0E0GK
 	3L
 : / /  M  & ;<    H <	 	 	 
  r q Y f f  (H,acecgcgh
   
5	
E

E
UUeU1FE\LcKd<ef  

9: 2::h' 2::k"2::'(	$:*>B-`jF jZ57.O?  J 	^$ K  		s6   (K /$K K% KK	K"!K"%K.-K.