Loops 如何在Ansible中与_项一起使用时跳过未定义项

Loops 如何在Ansible中与_项一起使用时跳过未定义项,loops,ansible,Loops,Ansible,例如: - name: Check the count of numberServers matches that of jvm variables assert: that: item|count|int == numberServers msg: JVM count for {{item}} doesn't match numberServers with_items: - "{{ JVMs_ }}" - "{{ jvmName }}" - "

例如:

- name: Check the count of numberServers matches that of jvm variables
  assert:
    that: item|count|int == numberServers
    msg: JVM count for {{item}} doesn't match numberServers
  with_items:
    - "{{ JVMs_ }}"
    - "{{ jvmName }}"
    - "{{ jvmSize }}"
    - "{{ jvmHeap }}"
因此,假设未定义
JVMs
,我希望为其余项目运行任务

*注意:在时尝试过使用
;如果条件不满足,它将跳过整个任务


另外,创建单独的任务不是一个好的选择,因为我有4个以上的项目要循环。

在您的情况下,您可以使用
|默认([])
和_个项目
忽略空列表:

- name: Check the count of numberServers matches that of jvm variables
  assert:
    that: item|count|int == numberServers
    msg: JVM count for {{item}} doesn't match numberServers
  with_items:
    - "{{ JVMs_ | default([]) }}"
    - "{{ jvmName | default([]) }}"
    - "{{ jvmSize | default([]) }}"
    - "{{ jvmHeap | default([]) }}"

当:{{item}}
时,您是否尝试过
?显示您的输出。@tedder42这是我在尝试上述
任务[输入验证:检查NumberServer的计数是否与jvm变量的计数匹配]******************************************************************************************************************************************************************************************致命:[34.209.242.130]:失败!=>{“failed”:true,“msg”:“'JVMs_'未定义”}
hmm,在带有_项的情况下,如何执行
{{JVMs | default('')}
或类似操作?您可能可以执行
|omit
,尝试类似的操作。啊哈,
|default(omit)
好的,default(omit)并不是真正跳过任务,而是执行它(这是更好的一步),但是任务(即断言)失败,因为omit不匹配。