如何在Ansible

时间:2017-10-27 10:22:54

标签: 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 }}"
    - "{{ jvmSize }}"
    - "{{ jvmHeap }}"

所以说JVMs_没有定义,我希望任务能够为剩下的项目运行。

* 注意:已尝试使用when;如果不满足条件,它会跳过整个任务。

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

1 个答案:

答案 0 :(得分:3)

在您的情况下,您可以使用|default([])with_items忽略空列表:

- 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([]) }}"