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

Parameter Choices/Defaults Configuration Comments
_raw
list / elements=raw / required
A list where the elements are strings, lists, or dictionaries.
Lists are simply iterated over.
Dictionaries are iterated over and returned as if they would be processed by the ansible.builtin.dict filter.
Strings are evaluated as Jinja2 expressions who can access the previously chosen elements with item.<index>. The result must be a list of a dictionary.

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:

Key Returned Description
_list
list / elements=dictionary
success
A list composed of dictionaries whose keys are indices for the input list.

Sample:
[{'0': 'a', '1': 'test'}, {'0': 'a', '1': 'foo'}, {'0': 'b', '1': 'bar'}]