0

i am working with Ansible and i am trying to retrieve the values of multiple tags from one xml. I have found how to retrieve the value from 1 tag, but i need to retrieve the values from many different tags. In particular, i know that if you want to retrieve 1 tag's value you do in your playbook sth like this: How to parse a XML response in ansible?

So I can use this xml module, but what do I do if I want to have more than one 'xpath' ?

Xenia Ioannidou
  • 67
  • 1
  • 1
  • 8

2 Answers2

2

If I understood correctly and based on the example provided in your link. I would use an Ansible loop with subkeys:

playbook.yml

---
- hosts: localhost
  gather_facts: no
  tasks:
    - name: Retrieve multiple xml tags value
      xml:
        xmlstring: "{{ item.string }}" 
        xpath: "{{ item.path }}"
        content: text 
      loop: 
        - { path: "/value", string: "<value>foo</value>" }
        - { path: "/tag/other-value", string: "<tag><other-value>bar</other-value></tag>" }
      register: tags_value 

    - debug:
        msg: "{{ item.matches }}"
      loop: "{{ tags_value.results }}"
      loop_control:
        label: "{{ item.matches }}"

result

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Retrieve multiple xml tags value] ********************************************************************************************************************************************************************
ok: [localhost] => (item={u'path': u'/value', u'string': u'<value>foo</value>'})
ok: [localhost] => (item={u'path': u'/tag/other-value', u'string': u'<tag><other-value>bar</other-value></tag>'})

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => (item=[{u'value': u'foo'}]) => {
    "msg": [
        {
            "value": "foo"
        }
    ]
}
ok: [localhost] => (item=[{u'other-value': u'bar'}]) => {
    "msg": [
        {
            "other-value": "bar"
        }
    ]
}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
SolalVall
  • 292
  • 2
  • 5
  • Yes it is working properly , exactly as I wanted to !!! Thank you a lot !!! – Xenia Ioannidou Apr 20 '20 at 09:58
  • May I ask you sth more ? If in any case you want to group 2 paths in 1 how can you do it ? Let's say we have 3 paths like the ones you provide, but we want to put 2 of them together as a pair ? Is that possible ??? @SolalVall – Xenia Ioannidou Apr 20 '20 at 16:03
  • Could you provide more details/examples of what you want to achieve please – SolalVall Apr 22 '20 at 03:13
  • I will write another answer below , because my question is too long for a comment @SolalVall – Xenia Ioannidou Apr 22 '20 at 11:24
  • Because you are trying to achieve something different than your original question, could you please create another question instead of posting an answer in the same post and provide the link here. (+ Could you provide in your new post a basic example of your xml content) – SolalVall Apr 22 '20 at 15:37
  • Sure !!! Thank you for the feedback !!! Here it is: https://stackoverflow.com/questions/61370223/combining-the-output-of-multiple-loops-in-ansible – Xenia Ioannidou Apr 22 '20 at 16:31
0

Ok let's say that from 1 loop i get this:

=> (item=[{vlan-member-interface': 'at-0/0/0.0*'}, {vlan-member-interface': 'at-0/0/1.0*'}]) => {
"msg": [
    {
        "vlan-member-interface": "at-0/0/0.0*"
    }, 
    {
        "vlan-member-interface": "at-0/0/1.0*"
    }
]

}

And from the 2nd loop something like this:

(item=[{vlan-tag': '5071'}, {'vlan-tag': '5072'}]) => {
"msg": [
    {
        "vlan-tag": "5071"
    }, 
    {
        "vlan-tag": "5072"
    }
] 

How can i group the loops together so as to get at-0/0/0.0 and 5071 in 1 result ? And last question is can i extract somehow in a list the 5071, 5072 ? I am trying to handle this response as a dictionary and parse the values but i am doing it wrong @SolalVall

Xenia Ioannidou
  • 67
  • 1
  • 1
  • 8