ansible筛选器映射数组并放入json temp

2024-05-12 18:12:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数组变量如下

registries:
- type: primary
  host: r1.example.com
- type: secondary
  host: r2.example.com

我只想从json.j2模板中的每个数组项呈现host属性。 我在模板中尝试了以下操作:

{ 
  "insecure-registries": {{ registries | map(attribute='host') | to_json }}
}

不幸的是,它不起作用,但在运行playbook时抛出此错误:

AnsibleError: Unexpected templating type error occurred on ({ \n \"graph\": \"{{ docker_home }}\",\n \"insecure-registries\" : {{ registries | map(attribute='host') | to_json }}\n}): Object of type 'generator' is not JSON serializable"}


Tags: tocom模板jsonhostmapexampletype
1条回答
网友
1楼 · 发布于 2024-05-12 18:12:39

map返回不是列表的特定对象类型。在使用list过滤器将其馈送到to_json之前,需要将其转换为一个列表

{ 
  "insecure-registries": {{ registries | map(attribute='host') | list | to_json }}
}

相关问题 更多 >