Ansible 和模板文件将值插入到模板中

Ansible and Template file inserting values in to the template

我现在遇到了一些问题,我有一些 aws 子网的映射及其路由表(通过 ansible 输出示例):

"subnetwork_route_map": [
    {
        "route_table_id": "rtb-xxxxxx",
        "subnet_id": "subnet-xxxxxx"
    },
    {
        "route_table_id": "rtb-xxxxxx",
        "subnet_id": "subnet-xxxxxxx"
    },
    {
        "route_table_id": "rtb-xxxxxx",
        "subnet_id": "subnet-xxxxxx"
    }
]

我希望将这些值插入到模板文件中,我认为在看到几个示例后我可以做这样的事情:

{% for item in subnetwork_route_map %}
{{ item[1]['subnet_id'] }},{{ item[1]['route_table_id'] }}§
{% endfor %}

当我尝试这个时我怎么会收到错误我从 ansible 收到错误:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "dict object has no element 1"}

subnetwork_route_map 是一个 字典列表 item 是一个单独的字典,你不需要 [1] 部分:

{% for item in subnetwork_route_map %}
    {{ item['subnet_id'] }},{{ item['route_table_id'] }}§
{% endfor %}