ansible - ansible jinja2:嵌套字典
问题描述
我正在尝试生成以下列表:
list1:127.0.0.1,127.0.0.2
list2:192.168.1.1,192.168.1.254
从这本词典:
ip_allowed:
list1:
- 127.0.0.1
- 127.0.0.2
list2:
- 192.168.1.1
- 192.168.1.254
使用以下 jinja2 Ansible 模板:
#ZONE HOST(S) OPTIONS
{% for hosts_key, hosts_value in ip_allowed.iteritems() %}
{% set hosts_dict = hosts_value %}
{% for item in hosts_dict %}
{{ hosts_key }} {{ item }}
{%- if loop.first %},{% endif %}
{% endfor %}
{% endfor %}
但我得到以下结果:
#ZONE HOST(S) OPTIONS
list1 127.0.0.1, list1 127.0.0.2 list2 192.168.1.1, list2 192.168.1.254
解决方案
I'm not entirely sure I got the exact format you want out of the template but you'll adapt if needed once you get the idea. Just join each ip in the value.
#ZONE HOST(S) OPTIONS
{% for allowed_item in (ip_allowed | dict2items) %}
{{ allowed_item.key }} {{ allowed_item.value | join(',') }}
{% endfor %}
推荐阅读
- php - PHP - 如何修复“strpos”错误地评估我的 URL
- javascript - Dispatch Redux Action by Promise.all 回调几个 API Endpoint
- yaml - Cloudformation 未解决的资源依赖关系
- r - R包括逗号并显示数字的最大dp
- php - 如果我离开没有操作属性的表单,我在哪里可以找到提交的数据?
- kotlin - 根据参数值动态加载 Kotlin 类
- python - 在 python 中使用 sentinel 进行卫星图像分析
- apache-spark - 如何通过应用程序 id 监控 spark-on-yarn 的进度?
- django - 如何再次过滤 django 查询集以获取唯一参数?
- java - 在哪里存储.pem文件(私钥)android java