ansible - 根据现有键为列表中的每个字典设置新键
问题描述
说我有:
[ {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 中的“单行”不起作用,或者太简洁以至于其他人无法阅读,那么这可能不是一个好主意
- 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 -}}
推荐阅读
- python - PyCharm 中的动态缩写?
- node.js - 通过代码输入用户响应
- java - 使用 REST API 将文件上传到 s3 存储桶
- firebase - Cloud Functions FCM 预期的 OAuth 2 访问令牌
- docker - WSL 上的 Docker 不会绑定挂载 $HOME
- sql - 在 SQL 中,只有在表还没有行的情况下,如何才能添加行?
- mongodb - Service 'MongoDB Server' (MongoDB) failed to start. Verify that you have sufficient privileges to start system services
- javascript - Javascript中的String构造函数如何返回没有键的Object?
- ruby - 如何从 mp4/mkv 视频文件中顺序提取视频片段?
- c++ - XCode 警告“变量 'Singleton 的实例化
::_instance' 此处需要,但没有可用的定义