Ansible模板在使用条件渲染时出现奇怪的错误

Ansible template strange error appears while using conditional rendering

我有这个ansible角色

---

- 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 条件

有错误

似乎是正确渲染模板的固定任务,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 %}