community.general.dependent 查询 — 组成一个包含其他列表或字典嵌套元素的列表,这些元素可能依赖于之前的循环变量

注意

此查询插件是 community.general 集合 (版本 10.1.0) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.general

要在剧本中使用它,请指定: community.general.dependent

community.general 3.1.0 中的新增功能

概要

  • 接收输入列表并返回一个元素为列表、字典或模板表达式的列表,这些表达式计算结果为列表或字典,由输入计算出的列表和字典的元素组成。

术语

参数

注释

术语

列表 / 元素=字典 / 必需

一个列表,其元素是一个元素的字典,将名称映射到字符串、列表或字典。名称是在结果对象中使用的索引。值按如下所述进行迭代。

如果值是列表,则只需对其进行迭代。

如果值是字典,则对其进行迭代并返回,就好像它们是由 ansible.builtin.dict2items 过滤器处理的一样。

如果值是字符串,则将其评估为 Jinja2 表达式,这些表达式可以使用 item.<index_name> 访问先前选择的元素。结果必须是列表或字典。

示例

- 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

返回值

描述

返回值

列表 / 元素=字典

一个由字典组成的列表,其键是从输入列表中获取的变量名。

返回:成功

示例: [{"key1": "a", "key2": "test"}, {"key1": "a", "key2": "foo"}, {"key1": "b", "key2": "bar"}]

作者

  • Felix Fontein (@felixfontein)

提示

每个条目类型的配置条目具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖较高的变量。