ansible过滤器映射数组并放入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:((上发生意外的模板类型错误\"graph \":\"{{docker_home}}\",\"不安全注册表":{{registrys|map(attribute='host'(|to_json}}(:类型的对象"generator"不是JSON可序列化的"}

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

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

最新更新