我正在与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 }}"