ansible 过滤器映射数组并放入 json 模板
ansible filter to map array and put in json template
我有一个数组变量如下
registries:
- type: primary
host: r1.example.com
- type: secondary
host: r2.example.com
我只想从 json.j2
模板中的每个数组项呈现主机属性。
我在模板中尝试了以下内容:
{
"insecure-registries": {{ registries | map(attribute='host') | to_json }}
}
不幸的是,它不起作用,但它在 运行 剧本时抛出此错误:
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"}
map
returns 不是列表的特定对象类型。在使用 list
过滤器
将其提供给 to_json
之前,您需要将其转换为列表
{
"insecure-registries": {{ registries | map(attribute='host') | list | to_json }}
}
我有一个数组变量如下
registries:
- type: primary
host: r1.example.com
- type: secondary
host: r2.example.com
我只想从 json.j2
模板中的每个数组项呈现主机属性。
我在模板中尝试了以下内容:
{
"insecure-registries": {{ registries | map(attribute='host') | to_json }}
}
不幸的是,它不起作用,但它在 运行 剧本时抛出此错误:
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"}
map
returns 不是列表的特定对象类型。在使用 list
过滤器
to_json
之前,您需要将其转换为列表
{
"insecure-registries": {{ registries | map(attribute='host') | list | to_json }}
}