
    Vh#                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ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Z ee       ed      k  Zd Z G d de      Zy# e$ r dZY )w xY w)    )absolute_importdivisionprint_functiona  
name: dependent
short_description: Composes a list with nested elements of other lists or dicts which can depend on previous loop variables
author: Felix Fontein (@felixfontein)
version_added: 3.1.0
description:
  - Takes the input lists and returns a list with elements that are lists, dictionaries, or template expressions which evaluate
    to lists or dicts, composed of the elements of the input evaluated lists and dictionaries.
options:
  _terms:
    description:
      - A list where the elements are one-element dictionaries, mapping a name to a string, list, or dictionary. The name
        is the index that is used in the result object. The value is iterated over as described below.
      - If the value is a list, it is simply iterated over.
      - If the value is a dictionary, it is iterated over and returned as if they would be processed by the P(ansible.builtin.dict2items#filter)
        filter.
      - If the value is a string, it is evaluated as Jinja2 expressions which can access the previously chosen elements with
        C(item.<index_name>). The result must be a list or a dictionary.
    type: list
    elements: dict
    required: true
ap	  
- name: Install/remove public keys for active admin users
  ansible.posix.authorized_key:
    user: "{{ item.admin.key }}"
    key: "{{ lookup('file', item.key.public_key) }}"
    state: "{{ 'present' if item.key.active else 'absent' }}"
  when: item.admin.value.active
  with_community.general.dependent:
    - admin: admin_user_data
    - key: admin_ssh_keys[item.admin.key]
  loop_control:
    # Makes the output readable, so that it doesn't contain the whole subdictionaries and lists
    label: "{{ [item.admin.key, 'active' if item.key.active else 'inactive', item.key.public_key] }}"
  vars:
    admin_user_data:
      admin1:
        name: Alice
        active: true
      admin2:
        name: Bob
        active: true
    admin_ssh_keys:
      admin1:
        - private_key: keys/private_key_admin1.pem
          public_key: keys/private_key_admin1.pub
          active: true
      admin2:
        - private_key: keys/private_key_admin2.pem
          public_key: keys/private_key_admin2.pub
          active: true
        - private_key: keys/private_key_admin2-old.pem
          public_key: keys/private_key_admin2-old.pub
          active: false

- name: Update DNS records
  community.aws.route53:
    zone: "{{ item.zone.key }}"
    record: "{{ item.prefix.key ~ '.' if item.prefix.key else '' }}{{ item.zone.key }}"
    type: "{{ item.entry.key }}"
    ttl: "{{ item.entry.value.ttl | default(3600) }}"
    value: "{{ item.entry.value.value }}"
    state: "{{ 'absent' if (item.entry.value.absent | default(False)) else 'present' }}"
    overwrite: true
  loop_control:
    # Makes the output readable, so that it doesn't contain the whole subdictionaries and lists
    label: |-
      {{ [item.zone.key, item.prefix.key, item.entry.key,
          item.entry.value.ttl | default(3600),
          item.entry.value.absent | default(False), item.entry.value.value] }}
  with_community.general.dependent:
    - zone: dns_setup
    - prefix: item.zone.value
    - entry: item.prefix.value
  vars:
    dns_setup:
      example.com:
        '':
          A:
            value:
              - 1.2.3.4
          AAAA:
            value:
              - "2a01:1:2:3::1"
        'test._domainkey':
          TXT:
            ttl: 300
            value:
              - '"k=rsa; t=s; p=MIGfMA..."'
      example.org:
        'www':
          A:
            value:
              - 1.2.3.4
              - 5.6.7.8
z
_list:
  description:
    - A list composed of dictionaries whose keys are the variable names from the input list.
  type: list
  elements: dict
  sample:
    - key1: a
      key2: test
    - key1: a
      key2: foo
    - key1: b
      key2: bar
)AnsibleLookupError)MappingSequence)string_types)
LookupBase)__version__)Templar)LooseVersion)trust_as_templateTFz2.14.0c                 H    t         rt        | t              rt        |       S | S N)HAS_DATATAGGING
isinstancestr_trust_as_template)values    n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/dependent.py
_make_safer      s    :eS1!%((L    c                        e Zd Zd Zd ZddZy)LookupModulec                     |xs i |_         dj                  d|d      }t        r|j                  |d      S t	        |d      r|j                  t        |            S |j                  |      S )zEvaluate expression with templar.

        ``expression`` is the expression to evaluate.
        ``variables`` are the variables to use.
        z	{0}{1}{2}z{{z}}F)cacheevaluate_expression)available_variablesformat_TEMPLAR_HAS_TEMPLATE_CACHEtemplatehasattrr   r   )self
expressiontemplar	variablesquoted_expressions        r   
__evaluatezLookupModule.__evaluate   sr     '0o2#'..tZF&##$5U#CC712..z*/EFF 122r   c           
         |t        |      k(  r |j                  |j                                y||   \  }}}	|8|j                         }
|j                         |
d<   	 | j                  |||
      }	t        |	t              rOt        |	j                               D ]2  \  }}t        d|fd|fg      ||<   | j                  |||d	z   |||       4 yt        |	t              r&|	D ]   }|||<   | j                  |||d	z   |||       " yt        d
|d|dt        |	             # t        $ r}t        d| d|d|      d}~ww xY w)a{  Fills ``result`` list with evaluated items.

        ``result`` is a list where the resulting items are placed.
        ``terms`` is the parsed list of terms
        ``index`` is the current index to be processed in the list.
        ``current`` is a dictionary where the first ``index`` values are filled in.
        ``variables`` are the variables currently available.
        Nitem)r&   zCaught "z" while evaluating z with item == keyr      z3Did not obtain dictionary or list while evaluating z, but )lenappendcopy_LookupModule__evaluate	Exceptionr   r   r   sorteditemsdict_LookupModule__processr   type)r#   resulttermsindexcurrentr%   r&   r+   r$   valuesvarseidxvalelts                  r   	__processzLookupModule.__process   s}    CJMM',,.)"',Z!>>#D"<<>DLWWM
 fg&"6<<>2 VS#eS\GS>$BCvueai'9UV ) V"vueai'9UV %EcWN[bZeeklpqwlxkyz| |  W(qc!4SG>'UW WWs   D$ $	E-EENc                    | j                  ||       g }t        |      dkD  rXt        | j                  j                        }g }t               }t        |      D ]  \  }}	t        |	t              st        d| dt        |	             t        |	      dk7  rt        d| dt        |	       d      t        |	j                               d   \  }
}|
|v rt        d	|
d
      |j                  |
       t        |t              r|j                  |
|df       t        |t         t        f      r|j                  |
d|f       t        d|
d| dt        |              | j#                  ||di ||       |S )zGenerate list.)var_optionsdirectr   )loaderz
Parameter z must be a dictionary, got r,   z' must be a one-element dictionary, got z	 elementszThe variable z appears more than onceNz (index zA) must have a value of type string, dictionary or list, got type )set_optionsr-   r   _templar_loaderset	enumerater   r   r   r6   listr3   addr	   r.   r   r5   )r#   r8   r&   kwargsr7   r%   datavars_so_farr9   termkvs               r   runzLookupModule.run   s   Yv>u:>T]]%:%:;GD%K(/ Et!$0,$UG+FtDzlSU Ut9>,$UG+RSVW[S\R]]fgi iDJJL)!,1#,'u,CDF F"a.KKAt-Hg#67KKD!-,$QE%8yz~  @A  {B  zC  DE E#E& NN64BCr   r   )__name__
__module____qualname__r0   r5   rS    r   r   r   r      s    3$|Lr   r   N) 
__future__r   r   r   r6   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.errorsr   /ansible.module_utils.common._collections_compatr   r   ansible.module_utils.sixr	   ansible.plugins.lookupr
   ansible.releaser   ansible_versionansible.templater   Bansible_collections.community.general.plugins.module_utils.versionr   r   r   r   ImportErrorr    r   r   rW   r   r   <module>rf      s    C B.JX
 . M 1 - : $ [HO +?;l8>TT S: S  Os    A( (A21A2