如何在Ansible中测试jinja2模板? [英] How can I test jinja2 templates in ansible?

查看:196
本文介绍了如何在Ansible中测试jinja2模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我需要测试一些我在角色中使用的jinja2模板。



例如,我有一个模板(test.j2):

  {%,如果已定义用户并且用户%} 
{%用户中的用户%} {{user}}
{%endfor%}
{%endif%}

和vars(在group_vars / all中):

  --- 
用户:
-迈克
-史密斯
-克拉拉
- Alex


解决方案

目前存在4种不同的变体:



1_Online (使用



2_Interactive (使用py thon和库jinja2,PyYaml)

  import yaml 
从jinja2导入模板
>>> ; template = Template(
... {%,如果已定义用户且用户%}
... {%,用于用户%} {{user}}
.. {%endfor%}
... {%endif%}
...)
>>值= yaml.load(
...--
...用户:
...-Mike
...-Smith
...-克拉拉
...-亚历克斯
...)
>>打印 {}。format(template.render(values))


Mike
Smith
Klara
Alex

3_Ansible (使用--check)

创建测试用书jinja2test.yml:

  --- 
-主机:127.0.0.1
任务:
-名称:测试jinja2template
模板:src = test.j2 dest = test.conf

并运行它:

  ansible-playbook jinja2test.yml --check --diff --connection = local 

示例输出:

  PLAY [ 127.0.0.1] ************************************************** **************** 

汇总事实************************ *******************************************

好​​的:[ 127.0.0.1]

任务:[测试jinja2template] ************************************** *******************
---之前:test.conf
+++之后:/Users/user/ansible/test.j2
@@ -0,0 +1,4 @@
+迈克
+史密斯
+克拉拉
+ Alex

已更改:[127.0.0.1]

PLAY RECAP ******************* ******************************************************
127.0.0.1:ok = 2更改= 1不可达= 0失败= 0

4_Ansible (使用-m模板)感谢@artburkart



制作一个名为test.txt.j2的文件

  {%,如果已定义用户和用户%} 
{%用户中的用户%}
{{user}}
{ %endfor%}
{%endif%}

像这样呼叫ansible:

 可以全部使用-i localhost -c local -m template -a src = test.txt.j2 dest =。/ test .txt --extra-vars ='{ users:[ Mike, Smith, Klara, Alex]}'

它将在当前目录中输出名为 test.txt 的文件,其中将包含评估后的<$的输出c $ c> test.txt.j2 模板。



我知道这并不直接使用vars f ile,但我认为这是不使用任何外部依赖项来测试模板的最简单方法。另外,我相信jinja2库提供的内容与ansible提供的内容之间存在一些差异,因此直接使用ansible可以避免任何差异。当提供给-extra-vars 的JSON满足您的需求时,您可以将其转换为YAML并继续使用。


Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this?

For example, I have a template (test.j2):

{% if users is defined and users %}
{% for user in users %}{{ user }}
{% endfor %}
{% endif %}

and vars (in group_vars/all):

---
users:
  - Mike
  - Smith
  - Klara
  - Alex

解决方案

At this time exists 4 different variants:

1_Online (using https://cryptic-cliffs-32040.herokuapp.com/)
Based on jinja2-live-parser code.

2_Interactive (using python and library jinja2, PyYaml)

import yaml
from jinja2 import Template
>>> template = Template("""
... {% if users is defined and users %}
... {% for user in users %}{{ user }}
... {% endfor %}
... {% endif %}
... """)
>>> values = yaml.load("""
... ---
... users:
...   - Mike
...   - Smith
...   - Klara
...   - Alex
... """)
>>> print "{}".format(template.render(values))


Mike
Smith
Klara
Alex

3_Ansible (using --check)
Create test playbook jinja2test.yml:

---
- hosts: 127.0.0.1
  tasks:
  - name: Test jinja2template
    template: src=test.j2 dest=test.conf

and run it:

ansible-playbook jinja2test.yml --check --diff --connection=local

sample output:

PLAY [127.0.0.1] **************************************************************

GATHERING FACTS ***************************************************************

ok: [127.0.0.1]

TASK: [Test jinja2template] ***************************************************
--- before: test.conf
+++ after: /Users/user/ansible/test.j2
@@ -0,0 +1,4 @@
+Mike
+Smith
+Klara
+Alex

changed: [127.0.0.1]

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0

4_Ansible (using -m template) thanks for @artburkart

Make a file called test.txt.j2

{% if users is defined and users %}
{% for user in users %}
{{ user }}
{% endfor %}
{% endif %}

Call ansible like so:

ansible all -i "localhost," -c local -m template -a "src=test.txt.j2 dest=./test.txt" --extra-vars='{"users": ["Mike", "Smith", "Klara", "Alex"]}'

It will output a file called test.txt in the current directory, which will contain the output of the evaluated test.txt.j2 template.

I understand this doesn't directly use a vars file, but I think it's the simplest way to test a template without using any external dependencies. Also, I believe there are some differences between what the jinja2 library provides and what ansible provides, so using ansible directly circumvents any discrepancies. When the JSON that is fed to --extra-vars satisfies your needs, you can convert it to YAML and be on your way.

这篇关于如何在Ansible中测试jinja2模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆