如何在剧本中等待元素

时间:2018-05-17 06:09:49

标签: ansible ansible-2.x

我有创建vm的剧本。我需要等待何时出现网络。如果元素不存在怎么办?

我的剧本部分:

    - ovirt_nics_facts:
        vm: "{{ vm }}"
        auth: "{{ ovirt_auth }}"
      register: nics_facts
      until: (ovirt_nics[0].reported_devices[0].ips[0].address is search("10.10.20.")) or
             (ovirt_nics[0].reported_devices[0].ips[0].address is search("192.168.250."))
      retries: 20
      delay: 60

错误:

fatal: [localhost]: FAILED! => {"msg": "The conditional check 'ovirt_nics[0].reported_devices[0].ips[0].address is defined' failed. The error was: error while evaluating conditional (ovirt_nics[0].reported_devices[0].ips[0].address is defined): list object has no element 0"}

1 个答案:

答案 0 :(得分:1)

我决定麻烦:

- name: add vm to temporary inventory
  block:
    - ovirt_nics_facts:
        vm: "{{ vm }}"
        auth: "{{ ovirt_auth }}"
      register: nics_facts
      until: ovirt_nics[0].reported_devices|length > 0
      retries: 20
      delay: 60