我有这段代码,其中第一个任务失败,第二个任务产生以下警告:
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ VRF }} == 17
我假设这个警告和第一个任务的失败是出于同样的原因。据我了解,变量是不同的类型。我该如何解决这个问题?
---
- name: PE_CE
hosts: localhost
tasks:
- include_vars: /opt/netsec/ansible/orchestration/capabilities/PE_CE/PE_CE_1.yml
- name: Build PE config Cisco
template:
src=/opt/netsec/ansible/orchestration/cisco_templates/PE_CE_CISCO_config_PE.j2
dest=/opt/netsec/ansible/orchestration/config_outputs/new/{{PE_HOSTNAME}}
when: "{{ PX_HOSTNAME }} == None"
- name: Build CE config Cisco
template:
src=/opt/netsec/ansible/orchestration/cisco_templates/PE_CE_CISCO_config_CE.j2
dest=/opt/netsec/ansible/orchestration/config_outputs/new/{{CE_HOSTNAME}}
when: "{{ VRF }} == 17"
我真正想做的是when: "{{ VRF }} == 17" and "{{ PX_HOSTNAME }} == None"
,或类似 {{ PX_HOSTNAME }}|length < 1"
最佳答案
您不需要将 {{
和 }}
与 when
配合使用。这样做:
当:PX_HOSTNAME ==“None”
和当:VRF ==“17”
这里有一些 documentation
when: PX_HOSTNAME == "None"
将检查 PX_HOSTNAME
是否包含字符串“None”。
如果您想在任务为空时运行该任务,请执行 when: not PX_HOSTNAME
或如果您想在任务为空时运行该任务,请执行 when :PX_HOSTNAME
。
您也可以使用长度:当:PX_HOSTNAME|length > 0
关于如果使用 'when',ansible jinja2 模板分隔符失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63683906/