I have an XML file that I am trying to get the value from 'id'. The problem is I have only been able to retrieve this: "{http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd}id": "data_wanted}".
I am just looking to return only 'data_wanted' and not the long associated key name.
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>data_wanted</id>
</metadata>
</package>
I have the following ansible play to register into 'nuspec_value'
- name: Get nuspec value
xml:
path: files/office.nuspec
content: text
xpath: /x:package/x:metadata/x:id
namespaces:
x: http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd
register: nuspec_value
And here is the debug output for 'nuspec_value.matches'
- name: Output nuspec
debug:
var: nuspec_value.matches
TASK [Output nuspec] *********************************************************************************************************************************************
ok: [localhost] => {
"nuspec_value.matches": [
{
"{http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd}id": "data_wanted"
}
]
I will only ever get 1 match in the XML document for 'id.' 'id' is being returned as a list, and so I can reference nuspec_value.matches[0], but it always brings along the
{http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd}id
I just want the value of 'id,' which is just 'data_wanted.'
How do I go about doing a set_fact: and just extracting the value of the matches and storing only the result of id which is 'data_wanted'? I was hoping I could just do nuspec_value.matches[0].value. When I tried 'nuspec_value.matches[0].value,' I get this error
- name: Output nuspec_value.matches[0].value
debug:
msg: "{{ nuspec_value.matches[0].value }}"
TASK [Output new_fact] *******************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'value'\n\nThe error appears to be in '/etc/ansible/examples/chocolatey/initialize_chocolatey.yml': line 46, column 13, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Output new_fact\n ^ here\n"}
I was hoping for a solution proposed somewhat like this: e.g.
pseudo-code:
set_fact: new_fact = (nuspec_value.matches filtered somehow)
debug:
var: new_fact
TASK [Output new_fact]
*******************************************************************
ok: [localhost] => {
"new_fact": "data_wanted"
}
Update: My most recent attempt, this gives the value, but I am not a fan of how readable it is. I could store the namespace in a variable, but Id like to know if there is a better way to refrence/filter the result
debug:
msg: "{{ nuspec_value.matches[0]['{http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd}id'] }}"
TASK [Output new_fact] *********************************************************
ok: [localhost] => {
"msg": "data_wanted"
}