简体   繁体   中英

How to skip some loops in Ansible playbook

I am working with Ansible and I have a playbook where I have a task like this:

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

where:

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

So i am looping all the results and there inside i get an item.xml from each item of the results[]. Then, I receive a specific tag. My problem is that some tags do NOT have any value for the xpath: "/rpc-reply/lldp/lldp-system-name" , so I would like either to skip it or just replace it with something else, because for now I get an error and my task fails so my playbook does not work fine.

Any ideas?

Not sure if this is the best solution but this is something you can do to skip the failed items. First collect the result of xpath matching to a variable and ignore error. Then loop through the collected result and use required data by skipping failed items using 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 }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM