根据现有密钥为列表中的每个字典设置新密钥
set new key for each dict in list, based on existing key
说我有:
[ {a: 'a'}, {a: 'b'}]
我想要:
[ {a: 'a', b: 'ab'}, {a: 'b', b: 'bb'}]
我如何在 Ansible 中执行此操作?希望它会像
- set_fact:
result: "{{ input | map('combine', dict(b=item.a + 'b')) | list }}"
但在这种情况下我不知道如何访问 item
。
如果 jinja 中的 "one liner" 不起作用,或者过于简洁以至于其他人无法阅读,那么这可能不是一个好主意
- set_fact:
# we are cloning the input dicts for mutation
result: >-
{%- set output = [] -%}
{%- for d in input: -%}
{%- set d2 = dict(d) -%}
{%- set _ = d2.update({"b": d2["a"] + "b"}) -%}
{%- set _ = output.append(d2) -%}
{%- endfor -%}
{{- output -}}
说我有:
[ {a: 'a'}, {a: 'b'}]
我想要:
[ {a: 'a', b: 'ab'}, {a: 'b', b: 'bb'}]
我如何在 Ansible 中执行此操作?希望它会像
- set_fact:
result: "{{ input | map('combine', dict(b=item.a + 'b')) | list }}"
但在这种情况下我不知道如何访问 item
。
如果 jinja 中的 "one liner" 不起作用,或者过于简洁以至于其他人无法阅读,那么这可能不是一个好主意
- set_fact:
# we are cloning the input dicts for mutation
result: >-
{%- set output = [] -%}
{%- for d in input: -%}
{%- set d2 = dict(d) -%}
{%- set _ = d2.update({"b": d2["a"] + "b"}) -%}
{%- set _ = output.append(d2) -%}
{%- endfor -%}
{{- output -}}