felixfontein.tools.dependent – Composes a list with nested elements of other lists or dicts which can depend on previous indices¶
Note
This plugin is part of the felixfontein.tools collection (version 1.4.1).
To install it use: ansible-galaxy collection install felixfontein.tools
.
To use it in a playbook, specify: felixfontein.tools.dependent
.
New in version 1.0.0: of felixfontein.tools
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.
Parameters¶
Examples¶
- name: Install/remove public keys for active admin users
ansible.posix.authorized_key:
user: "{{ item.0.key }}"
key: "{{ lookup('file', item.1.public_key) }}"
state: "{{ 'present' if item.1.active else 'absent' }}"
when: item.0.value.active
loop: "{{ lookup('felixfontein.tools.dependent', admin_user_data, 'admin_ssh_keys[item.0.key]') }}"
# Alternatively, you could also use the old with_* syntax:
# with_felixfontein.tools.dependent:
# - admin_user_data
# - "admin_ssh_keys[item.0.key]"
loop_control:
# Makes the output readable, so that it doesn't contain the whole subdictionaries and lists
label: "{{ [item.0.key, 'active' if item.1.active else 'inactive', item.1.public_key] }}"
vars:
admin_user_data:
admin1:
name: Alice
active: yes
admin2:
name: Bob
active: yes
admin_ssh_keys:
admin1:
- private_key: keys/private_key_admin1.pem
public_key: keys/private_key_admin1.pub
active: yes
admin2:
- private_key: keys/private_key_admin2.pem
public_key: keys/private_key_admin2.pub
active: yes
- private_key: keys/private_key_admin2-old.pem
public_key: keys/private_key_admin2-old.pub
active: no
- name: Update DNS records
community.aws.route53:
zone: "{{ item.0.key }}"
record: "{{ item.1.key ~ '.' if item.1.key else '' }}{{ item.0.key }}"
type: "{{ item.2.key }}"
ttl: "{{ 3600 if item.2.value.ttl is undefined else item.2.value.ttl }}"
value: "{{ item.2.value.value }}"
state: "{{ 'absent' if (item.2.value.absent if item.2.value.absent is defined else False) else 'present' }}"
overwrite: true
loop_control:
# Makes the output readable, so that it doesn't contain the whole subdictionaries and lists
label: "{{ [item.0.key, item.1.key, item.2.key, item.2.value.get('ttl', 3600), item.2.value.get('absent', False), item.2.value.value] }}"
loop: "{{ lookup('felixfontein.tools.dependent', dns_setup, 'item.0.value', 'item.1.value') }}"
# Alternatively, you can also do:
# loop: "{{ lookup('felixfontein.tools.dependent', 'dns_setup', 'item.0.value', 'item.1.value') }}"
# the dependent lookup will evaluate 'dns_setup'.
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 Values¶
Common return values are documented here, the following are the fields unique to this lookup: