community.general.dependent lookup – Composes a list with nested elements of other lists or dicts which can depend on previous loop variables
Note
This lookup plugin is part of the community.general collection (version 9.4.0).
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.general
.
To use it in a playbook, specify: community.general.dependent
.
New in community.general 3.1.0
Synopsis
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.
Terms
Parameter |
Comments |
---|---|
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 ansible.builtin.dict2items filter. If the value is a string, it is evaluated as Jinja2 expressions which can access the previously chosen elements with |
Examples
- 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
Return Value
Key |
Description |
---|---|
A list composed of dictionaries whose keys are the variable names from the input list. Returned: success Sample: |