Python ansible筛选器映射数组并放入json模板
我有一个数组变量,如下所示Python ansible筛选器映射数组并放入json模板,python,ansible,jinja2,ansible-filter,Python,Ansible,Jinja2,Ansible Filter,我有一个数组变量,如下所示 registries: - type: primary host: r1.example.com - type: secondary host: r2.example.com 我只想从json.j2模板中的每个数组项呈现host属性。 我在模板中尝试了以下操作: { "insecure-registries": {{ registries | map(attribute='host') | to_json }} } 不幸的是,它不起作用,但在运行pl
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:在({\n)上发生意外的模板类型错误
\“graph\”:“{{docker\u home}}\”,\n“不安全的注册表”:{{
注册表|映射(attribute='host')|到_json}}\n}):类型的对象
“生成器”不可JSON序列化“}
map
返回一个不是列表的特定对象类型。在使用list
过滤器将其馈送到to_json
之前,需要将其转换为列表
{
"insecure-registries": {{ registries | map(attribute='host') | list | to_json }}
}