Ansible Jinja2模板if语句计算不正确

Ansible Jinja2模板if语句计算不正确,ansible,jinja2,Ansible,Jinja2,我有一个Jinja2模板,它是作为使用Ansible对服务器进行电子邮件补丁的一部分发送的。其中有一个if语句,用来验证是否拍摄了快照。以下是剧本中设置要评估的变量的部分: - name: Was a snapshot taken set_fact: snapshot: "{{ 'yes' if (snap_result is changed and snap_result is not failed) else 'no'}}" 以下是模板: *********

我有一个Jinja2模板,它是作为使用Ansible对服务器进行电子邮件补丁的一部分发送的。其中有一个if语句,用来验证是否拍摄了快照。以下是剧本中设置要评估的变量的部分:

- name: Was a snapshot taken
  set_fact:
    snapshot: "{{ 'yes' if (snap_result is changed and snap_result is not failed) else 'no'}}"
以下是模板:

**********************************************************
PATCH REPORT FOR {{ inventory_hostname }}
DATE: {{ ansible_date_time.date }}
TIME: {{ ansible_date_time.time }}
----------------------------------------------------------
{# Determine boot message #}
{% if booted == "yes" %}
  {% set boot_message = "The system rebooted!" %}
  {% set boot_message2 = "" %}
{% else %}
  {% set boot_message = "The system did NOT reboot!" %}
  {% set boot_message2 = "Either no reboot was required OR their was an issue." %}
{% endif %}

{# Determine Snapshot Message #}
{% if snapshot == "yes" %}
  {% set snapshot_message = "A snapshot was taken of your machine." %}
  {% set snapshot_message2 = "Contact DC Operations to delete the snapshot when you are satisfied all is well." %}
{% else %}
  {% set snapshot_message = "A snapshot was NOT taken of your machine." %}
  {% set snapshot_message2 = "None was requested or it failed." %}
{% endif %}

Vendor:                 {{ ansible_system_vendor }}
Server Type:            {{ ansible_product_name }}
BIOS Version:           {{ ansible_bios_version }}
O/S Distribution:       {{ ansible_distribution }}
Previous Dist rev:      {{ pre_patch_dist | to_nice_json }}
Upgraded Dist rev:      {{ ansible_distribution_version }}
Previous Kernel rev:    {{ pre_patch_kern|to_nice_json }}
Upgraded Kernel rev:    {{ ansible_kernel }}
Python Version:         {{ ansible_python_version }}
PrePatch Uptime:        {{ pre_uptime.stdout|to_nice_json }}
PostPatch Uptime:       {{ post_uptime.stdout|to_nice_json }}

{{ boot_message }}
{{ boot_message2 }}
{{ snapshot_message }}
{{ snapshot_message2 }}
{{ snapshot }}
System took {{ elapsed_boot_time }} seconds to reboot.
----------------------------------------------------------
******** END REPORT FOR {{ inventory_hostname }} *********
结果如下:

**********************************************************
PATCH REPORT FOR na2-dvanstst06
DATE: 2021-04-07
TIME: 10:48:26
----------------------------------------------------------
    
    
Vendor:                 VMware, Inc.
Server Type:            VMware Virtual Platform
BIOS Version:           6.00
O/S Distribution:       Ubuntu
Previous Dist rev:      "18.04"
Upgraded Dist rev:      18.04
Previous Kernel rev:    "4.15.0-132-generic"
Upgraded Kernel rev:    4.15.0-132-generic
Python Version:         3.6.9
PrePatch Uptime:        " 10:48:20 up 5 days, 23:21,  1 user,  load average: 1.01, 1.00, 1.00"
PostPatch Uptime:       " 10:48:27 up 5 days, 23:22,  1 user,  load average: 1.01, 1.00, 1.00"

The system did NOT reboot!
Either no reboot was required OR their was an issue.
A snapshot was NOT taken of your machine.
None was requested or it failed.
Snapshot value is True
Booted value is False
System took 0 seconds to reboot.
----------------------------------------------------------
******** END REPORT FOR na2-dvanstst06 *********

正如您在模板末尾看到的,我输入了
snapshot
booted
的值,以查看它们的计算结果是否相同。因为机器不需要重新启动
引导
正确评估。然而,即使快照为真,它也将其视为假。我哪里出错了?

我想您是在强制检查模板中的字符串文字
“yes”
。而
set\u事实
的“yes”设置为布尔值。看这个

因此,
snapshot
变量是一个布尔值
True
,而不是您期望的字符串
'yes'

这表现在:

Snapshot value is True
因此,对
if
条件的模板进行简单更改应该可以实现以下目的:

{% if snapshot %}
  {% set snapshot_message = "A snapshot was taken of your machine." %}
  {% set snapshot_message2 = "Contact DC Operations to delete the snapshot when you are satisfied all is well." %}
{% else %}
  {% set snapshot_message = "A snapshot was NOT taken of your machine." %}
  {% set snapshot_message2 = "None was requested or it failed." %}
{% endif %}

这同样适用于
boot
变量。您可以将其设置为布尔值
True/False
,并相应地使用
if
条件。

我认为您正在强制检查模板中的字符串文字
“yes”
。而
set\u事实
的“yes”设置为布尔值。看这个

因此,
snapshot
变量是一个布尔值
True
,而不是您期望的字符串
'yes'

这表现在:

Snapshot value is True
因此,对
if
条件的模板进行简单更改应该可以实现以下目的:

{% if snapshot %}
  {% set snapshot_message = "A snapshot was taken of your machine." %}
  {% set snapshot_message2 = "Contact DC Operations to delete the snapshot when you are satisfied all is well." %}
{% else %}
  {% set snapshot_message = "A snapshot was NOT taken of your machine." %}
  {% set snapshot_message2 = "None was requested or it failed." %}
{% endif %}

这同样适用于
boot
变量。您可以将其设置为布尔值
True/False
,并相应地使用
if
条件。

我尝试了
{{%if snapshot%}}
,但它仍然认为快照拍摄条件不满足该条件,即使它显然应该满足该条件。好的,我在这方面做得太久了。那确实有效。我刚刚编辑了一个不同角色的重复j2文件,因此,当然,我一次又一次地尝试相同的错误代码。现在我要为可重用模板找出一个通用模板目录。我能理解,这是一种“大脑麻木”综合症很高兴它成功了!我尝试了
{{%if snapshot%}}
,但它仍然认为快照拍摄条件不满足条件,即使它显然应该满足。好的,我在这方面做得太久了。那确实有效。我刚刚编辑了一个不同角色的重复j2文件,因此,当然,我一次又一次地尝试相同的错误代码。现在我要为可重用模板找出一个通用模板目录。我能理解,这是一种“大脑麻木”综合症很高兴它成功了!