簡體   English   中英

如何跳過 Ansible 劇本中的一些循環

[英]How to skip some loops in Ansible playbook

我正在使用 Ansible 並且我有一本劇本,其中有這樣的任務:

- name: Get remote system names
  xml:
    xmlstring: "{{ item.xml }}"
    xpath: "/rpc-reply/lldp/lldp-system-name"
    content: text
  loop: "{{ topology.results }}"
  register: names

在哪里:

"topology": {
    "changed": false, 
    "msg": "All items completed", 
    "results": [............] }

所以我循環所有結果,在里面我從結果的每個項目[]中得到一個 item.xml。 然后,我收到一個特定的標簽。 我的問題是某些標簽對xpath: "/rpc-reply/lldp/lldp-system-name"沒有任何價值,所以我想跳過它或者只是用其他東西替換它,因為現在我得到一個錯誤,我的任務失敗了,所以我的劇本不能正常工作。

有任何想法嗎?

不確定這是否是最佳解決方案,但這是您可以跳過失敗項目的方法。 首先收集xpath匹配到一個變量的結果並忽略錯誤。 然后遍歷收集的結果並通過使用when: not item.failed跳過失敗的項目來使用所需的數據。

- name: Get remote system names
  xml:
    xmlstring: "{{ item.xml }}"
    xpath: "/rpc-reply/lldp/lldp-system-name"
    content: text
  loop: "{{ topology.results }}"
  register: names
  ignore_errors: yes

- debug: var=item
  when: not item.failed
  with_items: "{{ names.results }}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM