
    Vh2                         d Z dZ	 ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ  e       Z G d de      Zy# e$ r Y Jw xY w)a  
name: ssm_parameter
author:
  - Bill Wang (!UNKNOWN) <ozbillwang(at)gmail.com>
  - Marat Bakeev (!UNKNOWN) <hawara(at)gmail.com>
  - Michael De La Rue (!UNKNOWN) <siblemitcom.mddlr@spamgourmet.com>
short_description: gets the value for a SSM parameter or all parameters under a path
description:
  - Get the value for an Amazon Simple Systems Manager parameter or a hierarchy of parameters.
    The first argument you pass the lookup can either be a parameter name or a hierarchy of
    parameters. Hierarchies start with a forward slash and end with the parameter name. Up to
    5 layers may be specified.
  - If looking up an explicitly listed parameter by name which does not exist then the lookup
    will generate an error. You can use the C(default) filter to give a default value in
    this case but must set the O(on_missing) parameter to V(skip) or V(warn). You must
    also set the second parameter of the C(default) filter to C(true) (see examples below).
  - When looking up a path for parameters under it a dictionary will be returned for each path.
    If there is no parameter under that path then the lookup will generate an error.
  - If the lookup fails due to lack of permissions or due to an AWS client error then the aws_ssm
    will generate an error. If you want to continue in this case then you will have to set up
    two ansible tasks, one which sets a variable and ignores failures and one which uses the value
    of that variable with a default.  See the examples below.
  - Prior to release 6.0.0 this module was known as C(aws_ssm), the usage remains the same.

options:
  decrypt:
    description: A boolean to indicate whether to decrypt the parameter.
    default: true
    type: bool
  bypath:
    description: A boolean to indicate whether the parameter is provided as a hierarchy.
    default: false
    type: bool
  recursive:
    description: A boolean to indicate whether to retrieve all parameters within a hierarchy.
    default: false
    type: bool
  shortnames:
    description:
        - Indicates whether to return the name only without path if using a parameter hierarchy.
        - The O(shortnames) and O(droppath) options are mutually exclusive.
    default: false
    type: bool
  droppath:
    description:
        - Indicates whether to return the parameter name with the searched parameter heirarchy removed.
        - The O(shortnames) and O(droppath) options are mutually exclusive.
    default: false
    type: bool
    version_added: 8.2.0
  on_missing:
    description:
        - Action to take if the SSM parameter is missing.
        - V(error) will raise a fatal error when the SSM parameter is missing.
        - V(skip) will silently ignore the missing SSM parameter.
        - V(warn) will skip over the missing SSM parameter but issue a warning.
    default: "error"
    type: str
    choices: ["error", "skip", "warn"]
    version_added: 2.0.0
  on_denied:
    description:
        - Action to take if access to the SSM parameter is denied.
        - v(error) will raise a fatal error when access to the SSM parameter is denied.
        - v(skip) will silently ignore the denied SSM parameter.
        - v(warn) will skip over the denied SSM parameter but issue a warning.
    default: "error"
    type: string
    choices: ["error", "skip", "warn"]
    version_added: 2.0.0
extends_documentation_fragment:
  - amazon.aws.boto3
  - amazon.aws.common.plugins
  - amazon.aws.region.plugins
a  
# lookup sample:
- name: Lookup ssm parameter store in the current region
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'Hello' ) }}"

- name: Lookup ssm parameter store in specified region
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'Hello', region='us-east-2' ) }}"

- name: Lookup ssm parameter store without decryption
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'Hello', decrypt=False ) }}"

- name: Lookup ssm parameter store using a specified aws profile
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'Hello', profile='myprofile' ) }}"

- name: Lookup ssm parameter store using explicit aws credentials
  ansible.builtin.debug:
    msg: >-
      {{ lookup('amazon.aws.aws_ssm', 'Hello', access_key=my_aws_access_key, secret_key=my_aws_secret_key, session_token=my_session_token ) }}"

- name: Lookup ssm parameter store with all options
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'Hello', decrypt=false, region='us-east-2', profile='myprofile') }}"

- name: Lookup ssm parameter and fail if missing
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'missing-parameter') }}"

- name: Lookup a key which doesn't exist, returning a default ('root')
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'AdminID', on_missing="skip") | default('root', true) }}"

- name: Lookup a key which doesn't exist failing to store it in a fact
  ansible.builtin.set_fact:
    temp_secret: "{{ lookup('amazon.aws.aws_ssm', '/NoAccess/hiddensecret') }}"
  ignore_errors: true

- name: Show fact default to "access failed" if we don't have access
  ansible.builtin.debug: msg="{{ 'the secret was:' ~ temp_secret | default('could not access secret') }}"

- name: Return a dictionary of ssm parameters from a hierarchy path
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', '/PATH/to/params', region='ap-southeast-2', bypath=true, recursive=true ) }}"

- name: Return a dictionary of ssm parameters from a hierarchy path with shortened names (param instead of /PATH/to/params/foo/bar/param)
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', '/PATH/to/params', region='ap-southeast-2', shortnames=true, bypath=true, recursive=true ) }}"

- name: Return a dictionary of ssm parameters from a hierarchy path with the heirarchy path dropped (foo/bar/param instead of /PATH/to/params/foo/bar/param)
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', '/PATH/to/params', region='ap-southeast-2', droppath=true, bypath=true, recursive=true ) }}"

- name: Iterate over a parameter hierarchy (one iteration per parameter)
  ansible.builtin.debug: msg='Key contains {{ item.key }} , with value {{ item.value }}'
  loop: "{{ lookup('amazon.aws.aws_ssm', '/demo/', region='ap-southeast-2', bypath=True) | dict2items }}"

- name: Iterate over multiple paths as dictionaries (one iteration per path)
  ansible.builtin.debug: msg='Path contains {{ item }}'
  loop: "{{ lookup('amazon.aws.aws_ssm', '/demo/', '/demo1/', bypath=True)}}"

- name: Lookup ssm parameter warn if access is denied
  ansible.builtin.debug: msg="{{ lookup('amazon.aws.aws_ssm', 'missing-parameter', on_denied="warn" ) }}"
    N)AnsibleLookupError)	to_native)string_types)Display)is_boto3_error_code)AWSRetry)boto3_tag_list_to_ansible_dict)AWSLookupBasec                   *     e Zd Z fdZd Zd Z xZS )LookupModulec                 h   t        |   ||fi | | j                  d      }| j                  d      }|0t        |t              r|j                         dvrt        d|       |0t        |t              r|j                         dvrt        d|       | j                  d      r| j                  d      rt        d	      g }i }| j                  d
t        j                               }| j                  d      |d<   | j                  d      r| j                  d      |d<   |D ]  }	t        j                  d|	 d| j                          | j                  |||	|j                         |j                               }
| j                  d      r'|
D ]"  }|d   |d   j                  d      dz   d |d<   $ | j                  d      r"|
D ]  }|d   j                  |d   d      |d<    t        j!                  dt#        |
              |j%                  t'        |
dd              n_t        j                  d|        |D ]B  }	|j%                  | j)                  |||	|j                         |j                                      D t        j!                  dt#        |       d       |S )a  
        :arg terms: a list of lookups to run.
            e.g. ['parameter_name', 'parameter_name_too' ]
        :kwarg variables: ansible variables active at the time of the lookup
        :returns: A list of parameter values or a list of dictionaries if bypath=True.
        
on_missing	on_deniedN)errorwarnskipzH"on_missing" must be a string and one of "error", "warn" or "skip", not zG"on_denied" must be a string and one of "error", "warn" or "skip", not 
shortnamesdroppathzPshortnames and droppath are mutually exclusive. They cannot both be set to true.ssmdecryptWithDecryptionbypath	recursive	RecursivezAWS_ssm path lookup term: z in region: Name/   Path zaws_ssm path lookup returned: Value)tag_name_key_nametag_value_key_namezaws_ssm name lookup term: zaws_ssm path lookup returning:  )superrun
get_option
isinstancer   lowerr   clientr   jittered_backoffdisplayvvvregionget_path_parametersrfindreplacevvvvr   appendr	   get_parameter_value)selfterms	variableskwargsr   r   retssm_dictr)   term	paramlistx	__class__s               k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/lookup/ssm_parameter.pyr%   zLookupModule.run   s    	E9//__\2
OOK0	 !:|4
8H8H8JRk8k$Z[eZfg   9l3y7HPi7i$YZcYde  ??<(T__Z-H$%wxxUH$=$=$?@%)__Y%?!" ??8$$(OOK$@H[! 8l4;;-XY 44VXtZM]M]M_ajapapars	 ??<0& I$%fIaiooc.BQ.F.G$H&	I ??:.& L$%fI$5$5hv6F$K&	L =i	>R=STU

29PVkrs!, KK4UG<= t

433FHdJL\L\L^`i`o`o`qrst6y~6FaHI
    c                    ||d<   |j                  d      }	  |j                  di |j                         d   }t              s-|dk(  rt	        d| d      |dk(  r| j                  d|        |S # t        d      $ r< |dk(  rt	        d| d      |dk(  r| j                  d	|        i g}n|d
k(  ri g}Y t        j                  j                  $ r}t	        dt        |             d }~ww xY w)Nr   get_parameters_by_path
ParametersAccessDeniedExceptionr   z$Failed to access SSM parameter path  (AccessDenied)r   z/Skipping, access denied for SSM parameter path r   SSM lookup exception: z"Failed to find SSM parameter path  (ResourceNotFound)z*Skipping, did not find SSM parameter path  )get_paginatorpaginatebuild_full_resultr   r   r   botocore
exceptionsClientErrorr   len)	r4   r)   r9   r:   r   r   	paginatorr;   es	            r>   r.   z LookupModule.get_path_parameters   s&   (()AB		N*	**6X6HHJ<XI 9~W$(+MdVSf)ghhv%		FtfMN# ##:; 	!G#(+OPTvUd)efff$		KD6RSD	f$D	"".. 	N$'=il^%LMM	Ns   #A5 5AC6>C6C11C6c                    ||d<   	  |j                   dddi|}|d   d   S # t        d      $ r0 |dk(  rt        d| d	      |d
k(  r| j                  d|        Y y t        d      $ r0 |dk(  rt        d| d      |d
k(  r| j                  d|        Y y t        j
                  j                  $ r}t        dt        |             d }~ww xY w)Nr   	aws_retryT	Parameterr    ParameterNotFoundr   zFailed to find SSM parameter rF   r   z%Skipping, did not find SSM parameter rC   zFailed to access SSM parameter rD   z*Skipping, access denied for SSM parameter rE   rG   )get_parameterr   r   r   rK   rL   rM   r   )r4   r)   r9   r:   r   r   responserP   s           r>   r3   z LookupModule.get_parameter_value   s   	N+v++GdGhGHK(11"#67 	JW$(+HNa)bccv%		A$HI  ##:; 	OG#(+J4&P_)`aaf$		FtfMN  "".. 	N$'=il^%LMM	Ns!   # ;C :CC8CC)__name__
__module____qualname__r%   r.   r3   __classcell__)r=   s   @r>   r   r      s    BH0r?   r   )DOCUMENTATIONEXAMPLESrK   ImportErroransible.errorsr   ansible.module_utils._textr   ansible.module_utils.sixr   ansible.utils.displayr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr	   :ansible_collections.amazon.aws.plugins.plugin_utils.lookupr
   r+   r   rG   r?   r>   <module>rf      sa   JX7r	 . 0 1 ) \ P f T
)n= n!  		s   A AA