我有这个可靠的角色
---
- name: render motd template
template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: 0644
和 motd.j2
Welcome to {{inventory_hostname}}!
{{if ansible_distribution_major_version == 'NA'}}
There is {{ansible_distribution}}!
{{else}}
There is {{ansible_distribution}} {{ansible_distribution_major_version}}!
{{endif}}
当我尝试执行此角色时,出现此错误:
AnsibleError: template error while templating string:
expected token 'end of print statement', got 'string'.
这里出了什么问题?
如果我使用模板
Welcome to {{ inventory_hostname }}!
There is {{ansible_distribution}} {{ansible_distribution_major_version}}!
一切正常。
我假设 if
条件
答案 0 :(得分:0)
似乎是正确渲染模板的固定任务,jinja2 对变量输出 ({{ }}
) 和条件渲染命令 ({% %}
) 具有不同寻常的不同分隔符:
Welcome to {{ inventory_hostname }}!
{% if ansible_distribution_major_version == 'NA' %}
There is {{ ansible_distribution }}!
{% else %}
There is {{ ansible_distribution }} {{ ansible_distribution_major_version }}!
{% endif %}