简体   繁体   English

如何跳过 Ansible 剧本中的一些循环

[英]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:我正在使用 Ansible 并且我有一本剧本,其中有这样的任务:

- 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[].所以我循环所有结果,在里面我从结果的每个项目[]中得到一个 item.xml。 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.我的问题是某些标签对xpath: "/rpc-reply/lldp/lldp-system-name"没有任何价值,所以我想跳过它或者只是用其他东西替换它,因为现在我得到一个错误,我的任务失败了,所以我的剧本不能正常工作。

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.首先收集xpath匹配到一个变量的结果并忽略错误。 Then loop through the collected result and use required data by skipping failed items using when: not item.failed .然后遍历收集的结果并通过使用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