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

时间:2021-07-29 21:47:42

标签: ansible jinja2

我有这个可靠的角色

---

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

有错误

1 个答案:

答案 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 %}