【发布时间】:2019-07-15 02:11:03
【问题描述】:
我得到了以下内容,并坚持得到正确的答案。我有一个字典,我想用文件名中的 item.key 和模板中的所有值作为模板。
my_dict:
name1:
{ path=/x/y/z, action=all, filter=no },
{ path=/a/b/c, action=some, filter=yes }
name2:
{ path=/z/y/x, action=nothing, filter=no },
{ path=/c/b/a, action=all, filter=yes }
tasks:
- name: generate check config
template:
src: check.j2
dest: "{{ config_dir }}/{{ item.key }}-directories.json"
owner: Own
group: Wheel
mode: 0644
with_dict:
- "{{ my_dict }}"
when:
- my_dict is defined
become: true
我的模板看起来像
{
"configs": [
{% for value in my_dict %}
{
"path": "{{ value.path }}",
"action": "{{ value.action }}",
{% if value.filter is defined %}
"filter": "{{ value.filter }}"
{% endif %}
}{% if !loop.last %},{% endif %}
{% endfor %}
]
}
所以我测试了这么多,现在我没有看到太多树木的任何森林原因。
上面应该会产生 2 个文件。 文件名 = name1-directories.json 内容:
{
"configs": [
{
"path": /x/y/z,
"action": all,
"filter": no
},
{
"path": /a/b/c,
"action": some,
"filter": yes
}
]
}
提前致谢
【问题讨论】:
-
您能否简化您的问题并添加您想要达到的结果。
-
@gentux 你的问题回答了吗?您还有其他问题吗?
标签: json dictionary templates hash ansible