Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ansible筛选器映射数组并放入json模板_Python_Ansible_Jinja2_Ansible Filter - Fatal编程技术网

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 }}
}