首页 > 解决方案 > 遍历来自 xml 的列表会导致 Ansible

问题描述

我有这样的xml回应:

"model": {
    "results": [ {"item": "A", "xml":"...."}, {"item": "B", "xml":"...."} ]
 }

我正在尝试遍历结果列表并获取元素“xml”。现在我正在做这样的事情:

- name: Retrieve xml tags 
  xml:
    xmlstring: "{{ item.string }}"
    xpath: "{{ item.path }}"
    content: text
  loop:  
    - { path: "/rpc-reply/lldp-remote-system-name", string: "{{ model.results[].xml }}" }

但这不起作用。我也试过这个:

model.results[*].xml但这是错误的。

我试图在里面添加第二个循环,但我什么也没得到。有什么建议么 ?

标签: xmlloopsansible

解决方案


您可以循环浏览列表作为响应,如下所示:

  - name: xml tags
    xml:
      xmlstring: "{{ item.xml }}"
      xpath: "/rpc-reply/lldp-remote-system-name"
      content: text
    loop: "{{ model.results }}"

推荐阅读