Coder Social home page Coder Social logo

Comments (8)

cmprescott avatar cmprescott commented on September 3, 2024

That should definitely be included.

from ansible-xml.

wheelq avatar wheelq commented on September 3, 2024

is it possible to read the value? what is the syntax?

from ansible-xml.

kenthua avatar kenthua commented on September 3, 2024

Looks like it's "xmlstring" from the source

from ansible-xml.

hdimitriou avatar hdimitriou commented on September 3, 2024

The xmlstring is inaccurate:

    - name: xml test
      xml:
        file: "{{ some_file }}"
        xpath: //some_xpath
        content: text
      register: xml_hits

from ansible-xml.

glzavert avatar glzavert commented on September 3, 2024

So it has been months since this issue was opened and identified as a bug. What is the status of an example on how to load an attribute or element value into an ansible variable. Examples with and with out namespaces are relevant as many users simply need to access the values and do not need to manipulate the XML.

I have spent many many days trying to do this and the closest I have gotten returns the following

TASK [test : Print patch] ******************************************************
ok: [capiis.ibm.com] => {
"get_element_attribute.matches[0]['PatchManifest']['patchName']": "patch_JR55554_ISD_all_11500-1"
}

or

TASK [test : Print hist] *******************************************************
ok: [capiis.ibm.com] => {
"get_applied_patches.matches[21]['{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent']['installerId']": "patch_JR55554_ISD_all_11500-1"
}

I have been unable to get the values "patch_JR55554_ISD_all_11500-1" into variables so that I can compare a patch manifest to a list of applied patches in order to determine if a patch has been applied. This is to prevent what is apparent in the attached Version.xml file where a patch has been repeatedly applied. I like many, don't want to manipulate the xml files, I simply need the values in the attributes.

Here are my files:
PatchManifest.xml.txt
Version.xml.txt

Here is my main.yml

  • name: Get patch name attributes
    xml:
    file: "/opt/IBM/install/tmp/PatchManifest.xml"
    xpath: /PatchManifest
    content: 'attribute'
    register: get_element_attribute

  • name: print get_element_attribute
    debug: var=get_element_attribute

  • name: pring patchName attributes
    debug: var=get_element_attribute.matches[0]['PatchManifest']['patchName']

  • name: Count applied patches
    xml:
    file: "/opt/IBM/InformationServer/Version.xml"
    xpath: /iis:History/iis:HistoricalEvent
    namespaces:
    iis: http://www.ibm.com/LocalInstallRegistry
    count: true
    register: applied_patches

  • name: Print count of applied patches
    debug: var=applied_patches.count

  • name: Get applied patches
    xml:
    file: "/opt/IBM/InformationServer/Version.xml"
    xpath: //iis:HistoricalEvent
    namespaces:
    iis: http://www.ibm.com/LocalInstallRegistry
    content: 'attribute'
    register: get_applied_patches

  • name: Print applied patch for first patch
    debug: var=get_elements.matches[0]['{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent']['installerId']

  • name: iterate over applied patches applied patchs
    include: inner.yml
    vars:
    patch: get_element_attribute.matches[0]['PatchManifest']['patchName']
    hist: get_applied_patches.matches[{{item}}]['{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent']['installerId']
    with_sequence: start=0 end={{ get_applied_patches.count|int - 1}}

Here is my inner.yml

  • name: Print patch
    debug: var="{{ patch }}"

  • name: Print hist
    debug: var="{{ hist }}"

I have no idea why this has to be so difficult but any prompt resolution to this bug or direction on how to get the values into variables for testing would be appreciated.

from ansible-xml.

glzavert avatar glzavert commented on September 3, 2024

Ok, I finally figured it out. I had to use json_query in order to search the nested data that is returned as json data. This is not well documented and I only figured it out be looking at the code. However, the method I took is a kludge at best and a real set of methods and examples that a user can implement to read an attribute or element into a variable is needed.

I should be able to use an xpath like "/PatchManifest/@patchName" and get back only "patch_JR55554_ISD_all_11500-1" from my PatchManifest.xml and an xpath like "//iis:HistoricalEvent/@installerId" with a defined namespace and get back "patch_JR55554_ISD_all_11500-1" from my Version.xml file as well as a loop construct that would allow me to iterate of the items in all "//iis:HistoricalEvent" elements under "//iis:History"

The files I used are above but I modified my role/tasks as follows


  • name: Get attributes from non-namespaced file
    xml:
    file: "/opt/IBM/install/tmp/PatchManifest.xml"
    xpath: /PatchManifest
    content: 'attribute'
    register: get_element_text

  • name: print get_element_text
    debug: var=get_element_text

  • name: Get patch name attributes
    xml:
    file: "/opt/IBM/install/tmp/PatchManifest.xml"
    xpath: /PatchManifest
    content: 'attribute'
    register: get_element_attribute

  • name: print get_element_attribute
    debug: var=get_element_attribute

  • name: pring patchName attributes
    debug: var=get_element_attribute.matches[0]['PatchManifest']['patchName']

  • name: Count applied patches
    xml:
    file: "/opt/IBM/InformationServer/Version.xml"
    xpath: /iis:History/iis:HistoricalEvent
    namespaces:
    iis: http://www.ibm.com/LocalInstallRegistry
    count: true
    register: applied_patches

  • name: Print count of applied patches
    debug: var=applied_patches.count

  • name: Get applied patches
    xml:
    file: "/opt/IBM/InformationServer/Version.xml"
    xpath: //iis:HistoricalEvent
    namespaces:
    iis: http://www.ibm.com/LocalInstallRegistry
    content: 'attribute'
    register: get_applied_patches

  • name: Print applied patch for first patch
    debug: var=get_applied_patches.matches[0]['{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent']['installerId']

  • name: Print attribute from nested JSON data
    debug: msg="{{get_element_attribute.matches[0]|json_query('PatchManifest.patchName')}}"

  • name: Print nested JSON data for the first item in a list of namespaced data
    debug: msg="{{get_applied_patches.matches[0]}}"

# important to cast numerics to integer otherwise it is a string and no matches will be found or looping will occur

  • name: Print the desired attribute value from each item in a list of namespaced data
    debug: msg="{{get_applied_patches.matches[item|int]|json_query('*.installerId')}}"
    with_sequence: start=0 end={{ get_applied_patches.count|int - 1}}

output:
TASK [test : Get attributes from non-namespaced file] **************************
ok: [capiis.ibm.com]

TASK [test : print get_element_text] *******************************************
ok: [capiis.ibm.com] => {
"get_element_text": {
"actions": {
"ensure": "present",
"namespaces": {},
"xpath": "/PatchManifest"
},
"changed": false,
"count": 1,
"matches": [
{
"PatchManifest": {
"installVersion": "11.5.0.71",
"patchName": "patch_JR55554_ISD_all_11500-1",
"specversion": "1.0"
}
}
],
"msg": 1
}
}

TASK [test : Get patch name attributes] ****************************************
ok: [capiis.ibm.com]

TASK [test : print get_element_attribute] **************************************
ok: [capiis.ibm.com] => {
"get_element_attribute": {
"actions": {
"ensure": "present",
"namespaces": {},
"xpath": "/PatchManifest"
},
"changed": false,
"count": 1,
"matches": [
{
"PatchManifest": {
"installVersion": "11.5.0.71",
"patchName": "patch_JR55554_ISD_all_11500-1",
"specversion": "1.0"
}
}
],
"msg": 1
}
}

TASK [test : pring patchName attributes] ***************************************
ok: [capiis.ibm.com] => {
"get_element_attribute.matches[0]['PatchManifest']['patchName']": "patch_JR55554_ISD_all_11500-1"
}

TASK [test : Count applied patches] ********************************************
ok: [capiis.ibm.com]

TASK [test : Print count of applied patches] ***********************************
ok: [capiis.ibm.com] => {
"applied_patches.count": "22"
}

TASK [test : Get applied patches] **********************************************
ok: [capiis.ibm.com]

TASK [test : Print applied patch for first patch] ******************************
ok: [capiis.ibm.com] => {
"get_applied_patches.matches[0]['{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent']['installerId']": "information.server"
}

TASK [test : Print attribute from nested JSON data] ****************************
ok: [capiis.ibm.com] => {
"msg": "patch_JR55554_ISD_all_11500-1"
}

TASK [test : Print nested JSON data for the first item in a list of namespaced data] ***
ok: [capiis.ibm.com] => {
"msg": {
"{http://www.ibm.com/LocalInstallRegistry}HistoricalEvent": {
"description": "Information Server Installation",
"eventDate": "2016-12-14T12:15:52.577",
"installType": "INSTALL",
"installerId": "information.server",
"rollback": "/opt/IBM/InformationServer/_uninstall",
"status": "SUCCESS",
"userId": "root",
"version": "11.5.0.1"
}
}
}

TASK [test : Print the desired attribute value from each item in a list of namespaced data] ***
ok: [capiis.ibm.com] => (item=0) => {
"item": "0",
"msg": [
"information.server"
]
}
ok: [capiis.ibm.com] => (item=1) => {
"item": "1",
"msg": [
"information.server"
]
}
ok: [capiis.ibm.com] => (item=2) => {
"item": "2",
"msg": [
"information.server"
]
}
ok: [capiis.ibm.com] => (item=3) => {
"item": "3",
"msg": [
"information.server"
]
}
ok: [capiis.ibm.com] => (item=4) => {
"item": "4",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=5) => {
"item": "5",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=7) => {
"item": "7",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=6) => {
"item": "6",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=8) => {
"item": "8",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=9) => {
"item": "9",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=10) => {
"item": "10",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=12) => {
"item": "12",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=11) => {
"item": "11",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=13) => {
"item": "13",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=14) => {
"item": "14",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=15) => {
"item": "15",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=16) => {
"item": "16",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=17) => {
"item": "17",
"msg": [
"patch_JR56156_JDBC_HIVE_all_11500-01"
]
}
ok: [capiis.ibm.com] => (item=18) => {
"item": "18",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=19) => {
"item": "19",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=20) => {
"item": "20",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}
ok: [capiis.ibm.com] => (item=21) => {
"item": "21",
"msg": [
"patch_JR55554_ISD_all_11500-1"
]
}

from ansible-xml.

dagwieers avatar dagwieers commented on September 3, 2024

This issue was moved to ansible/ansible#27951

from ansible-xml.

dagwieers avatar dagwieers commented on September 3, 2024

Please close this issue.

from ansible-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.