【发布时间】:2017-10-03 16:19:45
【问题描述】:
我想在另一个使用模板变量的项目中使用ansible 优秀的模板引擎(基于Jinja2)。
模板变量可以使用所有ansible 查找和过滤器。
我想建立一个类似这样的渲染管道:
input.yaml.j2 => ansible (template engine) => output.yaml
示例:
input.yaml.j2
vars:
users: "{{ lookup('file', '/tmp/users.json') }}"
template:
- name: "{{ item.name }}"
type: "user"
fist_user_group: "{{ item.user_groups.0 }}"
with_items:
- "{{ users }}"
/tmp/users.json
[
{'John': 'groups': ['apache', 'webapp']},
{'Rohit': 'groups': ['rabbitmq', 'postgresql']}
]
output.yaml
- name: "John"
type: "user"
first_user_group: "apache"
- name: "Rohit"
type: "user"
first_user_group: "rabbitmq"
问题:
如何使用ansible渲染引擎解析我自己的模板?
【问题讨论】:
-
为什么不使用执行
template模块的小剧本? -
这可能工作正常。你能举个例子吗?我对
ansible不是很熟悉,但对渲染功能印象深刻。我将如何从 Python 代码本地运行这个剧本?有没有办法可以在内存中而不是文件中获取输出模板?
标签: python templates ansible jinja2 templating