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

问题描述 投票:0回答:1

我正在与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"的任何值,所以我想跳过它或只用其他东西代替它,因为现在我遇到了一个错误,我的任务失败了,所以我的剧本不起作用很好。

任何想法?

xml loops ansible skip
1个回答
1
投票

不确定这是否是最好的解决方案,但是您可以执行此操作以跳过失败的项目。首先收集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 }}"
© www.soinside.com 2019 - 2024. All rights reserved.