Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading an attribute value into an ansible variable is not documented #59

Closed
ssbarnea opened this issue Apr 15, 2016 · 8 comments · Fixed by ansible/ansible#28667
Closed
Labels

Comments

@ssbarnea
Copy link

The documentation should include a code sample that loads an attribute value into an ansible variable. I may make sense to provide a simple example for reading the charset attribute from insite HTML page. html>headers>meta>charset. Maybe even specifying what happens if it is missing.

@cmprescott cmprescott added the bug label May 20, 2016
@cmprescott
Copy link
Owner

That should definitely be included.

@wheelq
Copy link

wheelq commented Jun 15, 2016

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

@kenthua
Copy link

kenthua commented Jun 15, 2016

Looks like it's "xmlstring" from the source

@hdimitriou
Copy link

The xmlstring is inaccurate:

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

@glzavert
Copy link

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.

@glzavert
Copy link

glzavert commented Mar 16, 2017

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"
]
}

@dagwieers
Copy link
Collaborator

This issue was moved to ansible/ansible#27951

@dagwieers
Copy link
Collaborator

dagwieers commented Aug 9, 2017

Please close this issue.

dagwieers added a commit to dagwieers/ansible that referenced this issue Aug 25, 2017
Missing bits and pieces, clarifications and examples.

This fixes ansible#27951
This fixes ansible#28119
This fixes cmprescott/ansible-xml#59
dagwieers added a commit to ansible/ansible that referenced this issue Aug 25, 2017
Missing bits and pieces, clarifications and examples.

This fixes #27951
This fixes #28119
This fixes cmprescott/ansible-xml#59
prasadkatti pushed a commit to prasadkatti/ansible that referenced this issue Oct 1, 2017
Missing bits and pieces, clarifications and examples.

This fixes ansible#27951
This fixes ansible#28119
This fixes cmprescott/ansible-xml#59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants