
    Vh                    r    d dl mZ dZdZd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 G d de      Zy)    )annotationsaj  
    name: subelements
    author: Serge van Ginderachter (!UNKNOWN) <serge@vanginderachter.be>
    version_added: "1.4"
    short_description: traverse nested key from a list of dictionaries
    description:
      - Subelements walks a list of hashes (aka dictionaries) and then traverses a list with a given (nested sub-)key inside of those records.
    options:
      _terms:
         description: tuple of list of dictionaries and dictionary key to extract
         required: True
      skip_missing:
        default: False
        description:
          - Lookup accepts this flag from a dictionary as optional. See Example section for more information.
          - If set to V(True), the lookup plugin will skip the lists items that do not contain the given subkey.
          - If set to V(False), the plugin will yield an error and complain about the missing subkey.
a   
- name: show var structure as it is needed for example to make sense
  hosts: all
  vars:
    users:
      - name: alice
        authorized:
          - /tmp/alice/onekey.pub
          - /tmp/alice/twokey.pub
        mysql:
            password: mysql-password
            hosts:
              - "%"
              - "127.0.0.1"
              - "::1"
              - "localhost"
            privs:
              - "*.*:SELECT"
              - "DB1.*:ALL"
        groups:
          - wheel
      - name: bob
        authorized:
          - /tmp/bob/id_rsa.pub
        mysql:
            password: other-mysql-password
            hosts:
              - "db1"
            privs:
              - "*.*:SELECT"
              - "DB2.*:ALL"
  tasks:
    - name: Set authorized ssh key, extracting just that data from 'users'
      ansible.posix.authorized_key:
        user: "{{ item.0.name }}"
        key: "{{ lookup('file', item.1) }}"
      with_subelements:
         - "{{ users }}"
         - authorized

    - name: Setup MySQL users, given the mysql hosts and privs subkey lists
      community.mysql.mysql_user:
        name: "{{ item.0.name }}"
        password: "{{ item.0.mysql.password }}"
        host: "{{ item.1 }}"
        priv: "{{ item.0.mysql.privs | join('/') }}"
      with_subelements:
        - "{{ users }}"
        - mysql.hosts

    - name: list groups for users that have them, don't error if groups key is missing
      ansible.builtin.debug: var=item
      loop: "{{ q('ansible.builtin.subelements', users, 'groups', {'skip_missing': True}) }}"
z5
_list:
  description: list of subelements extracted
AnsibleError)string_types)boolean)
LookupBase)listify_lookup_plugin_terms)skip_missingc                      e Zd Zd Zy)LookupModulec           	        dd}t        |d   | j                        |d<   t        |t              rdt	        |      cxk  rdk  s	n  |        t        |d   t        t
        f      rt        |d   t              s |d       |d   j                  d      }t        |d   t
              r=|d   j                  d	d
      d
urg S g }|d   D ]  }|j                  |d   |           n|d   }i }t	        |      dk(  r|d   }t        |t
              s!t        d |D              s |dt        z         g }	|D ]
  }
t        |
t
              st        d|
z        |
j                  d	d
      d
ur7t        |j                  dd
      d
      }|
}d
}g }|D ]  }||d   k(  rd}||vr|rt        d|d|d      |s1t        ||   t
              s|r>t        d|d||   d      ||   }Yt        ||   t              st        d|d||   d      |j                  |g       } |D ]  }|	j                  |
|f         |	S )Nc                    t        d| z         )Nz9subelements lookup expects a list of two or three items, r   )msgs    R/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/lookup/subelements.py_raise_terms_errorz,LookupModule.run.<locals>._raise_terms_errorc   s    KcQS S    r   )templar         z>first a dict or a list, second a string pointing to the subkey.skippedFc              3  P   K   | ]  }t        |t              xr |t        v    y w)N)
isinstancer   FLAGS).0keys     r   	<genexpr>z#LookupModule.run.<locals>.<genexpr>   s'     2sfi:c<3P3aUX\aUa3a2ss   $&z4the optional third item must be a dict with flags %sz1subelements lookup expects a dictionary, got '%s'r
   )strictTzcould not find 'z' key in iterated item ''zthe key z$ should point to a dictionary, got 'z should point to a list, got ') )r	   _templarr   listlendictr   splitgetappendallr   r   r   pop)selfterms	variableskwargsr   subelementselementlistr   flagsretitem0r
   subvalue
lastsubkeysublistsubkeyitem1s                    r   runzLookupModule.runa   st   	S /uQxOa %&a3u:.B.B  %(T4L1E!Hl9[_`AhnnS)eAh%Qx||Iu-U:	KQx 2""58C=12  (K u:?!HE%&s2smr2s/sUX]]^   !	+EeT*"#VY^#^__yyE*%7"599^U#CERLHJG% ;[_,!%J)# *^dfn+opp!%hv&6='$".flnvw}n~/  #A  A#+F#3%hv&6=*\bdlmsdt+uvv"*,,vr":);* ! +

E5>*+A!	+F 
r   N)__name__
__module____qualname__r:    r   r   r   r   _   s    Gr   r   N)
__future__r   DOCUMENTATIONEXAMPLESRETURNansible.errorsr   ansible.module_utils.sixr   )ansible.module_utils.parsing.convert_boolr   ansible.plugins.lookupr   ansible.utils.listifyr	   r   r   r>   r   r   <module>rH      sF    #&5n

 ( 1 = - = 	I: Ir   