Jinja2 模板 if 语句未正确评估
Jinja2 template if statement is not evaluating correctly
我有一个 Jinja2 模板,作为电子邮件的一部分发送 post 使用 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
的值,以查看它们的评估是否相同。由于机器不需要重新启动 booted
评估正确。但是,即使 snapshot
为真,它也将其视为假。我哪里错了?
我认为您是在强制检查模板中的字符串文字 "yes"
。而 set_fact
将 '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
条件。
我有一个 Jinja2 模板,作为电子邮件的一部分发送 post 使用 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
的值,以查看它们的评估是否相同。由于机器不需要重新启动 booted
评估正确。但是,即使 snapshot
为真,它也将其视为假。我哪里错了?
我认为您是在强制检查模板中的字符串文字 "yes"
。而 set_fact
将 '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
条件。