外部使用 Ansible 模板引擎
External use of Ansible templating engine
我想在另一个使用模板变量的项目中使用 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
渲染引擎来解析我自己的模板?
简单的剧本:
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- template:
src: input.j2
dest: output.file
执行:ansible-playbook myplaybook.yml
.
关于您的信息,Ansible 使用 Jinja2 模板引擎的稍微扩展版本。
看看吧——这可能是你真正想要的。
我想在另一个使用模板变量的项目中使用 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
渲染引擎来解析我自己的模板?
简单的剧本:
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- template:
src: input.j2
dest: output.file
执行:ansible-playbook myplaybook.yml
.
关于您的信息,Ansible 使用 Jinja2 模板引擎的稍微扩展版本。
看看吧——这可能是你真正想要的。