Comments (8)
That should definitely be included.
from ansible-xml.
is it possible to read the value? what is the syntax?
from ansible-xml.
Looks like it's "xmlstring" from the source
from ansible-xml.
The xmlstring is inaccurate:
- name: xml test
xml:
file: "{{ some_file }}"
xpath: //some_xpath
content: text
register: xml_hits
from ansible-xml.
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.
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.
This issue was moved to ansible/ansible#27951
from ansible-xml.
Please close this issue.
from ansible-xml.
Related Issues (20)
- '_ElementStringResult' object has no attribute 'attrname' HOT 2
- Older lxml versions have no predictable element attribute order HOT 1
- Fails on python3 due to unicode-related errors HOT 1
- How to create multiple nodes? HOT 8
- Feature idea: Add 'namespace catch' in issue_template.md HOT 8
- xpath is not a legal parameter in an Ansible task or handler HOT 1
- winrm send_input failed HOT 2
- Out-of sync with upstream
- No idempotency when combining namespace and set_children HOT 3
- Do we need an "issue template" HOT 5
- comprescott.xm fails with centos user HOT 1
- deliverable package HOT 3
- Update "xml:base" attribute HOT 4
- Convert xml file to yaml file using java
- Get entire xml node HOT 1
- add_children before other element HOT 6
- Make this repo readonly? HOT 2
- Add_children in an XML file is not working HOT 2
- How to assign mulity attribute value in xml node
- Ansible: Custom Tag field of a Dictionary. Error: Invalid tag name '}'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ansible-xml.