community.general.dict 过滤器 – 将元组列表转换为字典
注意
此过滤器插件是 community.general 集合(版本 10.1.0)的一部分。
如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。
要在 playbook 中使用它,请指定:community.general.dict。
community.general 3.0.0 中的新功能
概要
- 将元组列表转换为字典。这是 - dict函数的过滤器版本。
输入
这描述了过滤器的输入,即 | community.general.dict 之前的值。
| 参数 | 注释 | 
|---|---|
| 一个元组列表(恰好包含两个元素)。 | 
示例
- name: Convert list of tuples into dictionary
  ansible.builtin.set_fact:
    dictionary: "{{ [[1, 2], ['a', 'b']] | community.general.dict }}"
    # Result is {1: 2, 'a': 'b'}
- name: Create a list of dictionaries with map and the community.general.dict filter
  ansible.builtin.debug:
    msg: >-
      {{ values | map('zip', ['k1', 'k2', 'k3'])
                | map('map', 'reverse')
                | map('community.general.dict') }}
  vars:
    values:
      - - foo
        - 23
        - a
      - - bar
        - 42
        - b
  # Produces the following list of dictionaries:
  #   {
  #     "k1": "foo",
  #     "k2": 23,
  #     "k3": "a"
  #   },
  #   {
  #     "k1": "bar",
  #     "k2": 42,
  #     "k3": "b"
  #   }
返回值
| 键 | 描述 | 
|---|---|
| 一个包含提供的键值对的字典。 返回: 成功 | 
