a )g@sxddlmZmZmZeZdZdZdZddl m Z ddl m Z ddl mZddlmZdd lmZd ZGd d d eZd S))absolute_importdivisionprint_functionaj name: subelements author: Serge van Ginderachter (!UNKNOWN) 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 C(True), the lookup plugin will skip the lists items that do not contain the given subkey. - If set to C(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@seZdZddZdS) LookupModulecKsNddd}t|d|jd|d<t|trDdt|krBdksJn|t|dttfrjt|dtsr|d |dd }t|dtr|dd d d urgSg}|dD]}| |d|qn|d}i}t|dkr|d}t|tst d d|Ds|dt g} |D]&} t| ts>t d| | d d d urTq t |dd d d} | } d } g}|D]}||dkrd} || vr| rqxnt d|| f| st| |ts| r֐qxnt d|| |fn| |} n2t| |tst d|| |fn | |g}qx|D]}| | |fq0q | S)NcSstd|dS)Nz9subelements lookup expects a list of two or three items, r)msgrF/usr/lib/python3.9/site-packages/ansible/plugins/lookup/subelements.py_raise_terms_errordsz,LookupModule.run.._raise_terms_errorr)Ztemplarz>first a dict or a list, second a string pointing to the subkey.ZskippedFcss |]}t|to|tvVqdS)N) isinstancerFLAGS).0keyrrr z#LookupModule.run..z4the optional third item must be a dict with flags %sz1subelements lookup expects a dictionary, got '%s'r )strictTz-could not find '%s' key in iterated item '%s'z1the key %s should point to a dictionary, got '%s'z+the key %s should point to a list, got '%s')r )r Z_templarrlistlendictrsplitgetappendallrrrpop)selfZtermsZ variableskwargsrZ subelementsZ elementlistrflagsretZitem0r ZsubvalueZ lastsubkeyZsublistZsubkeyZitem1rrrrunbs` $          zLookupModule.runN)__name__ __module__ __qualname__r*rrrrr `sr N)Z __future__rrrtypeZ __metaclass__Z DOCUMENTATIONZEXAMPLESZRETURNZansible.errorsrZansible.module_utils.sixrZ)ansible.module_utils.parsing.convert_boolrZansible.plugins.lookupr Zansible.utils.listifyr rr rrrrs7