使用Ansible LXML模块解析XML的错误
以下是我使用Ansible lxml
python插件从XML打印< driver_name>
的Ansible Playbook。
---
- name: ReadJsonfile
hosts: localhost
tasks:
- name: Print drivers
community.general.xml:
path: jdbc.xml.bkp
xpath: /jdbc_data_source/jdbc_driver_params/driver_name
register: hits
- ansible.builtin.debug:
var: hits
我遇到了以下错误运行剧本的错误:
解析文档时错误:jdbc.xml.bkp(不支持编码UTF_8
提议的解决方案之一是从下面的xml删除下一行的第一行
是我从下面XML删除的第一行
<?xml version='1.0' encoding='UTF_8'?>
是我当前的XML文件:
<jdbc_data_source xmlns="http://xmlns.oracle.com/weblogic/jdbc_data_source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema_instance" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc_data_source http://xmlns.oracle.com/weblogic/jdbc_data_source/1.0/jdbc_data_source.xsd">
<jdbc_driver_params>
<driver_name>oracle.jdbc.OracleDriver</driver_name>
<properties>
<property>
<name>user</name>
</property>
</properties>
<driver_name>tomcat.jdbc.TomcatDriver</driver_name>
</jdbc_driver_params>
</jdbc_data_source>
不幸,不幸的是,I现在获取以下错误:
TASK [Print drivers] *****************************************************************************
Tuesday 29 March 2022 05:04:49 -0500 (0:00:01.427) 0:00:01.443 *********
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: lxml.etree.XPathEvalError: Invalid expression
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 107, in <module>\n _ansiballz_main()\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.xml', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.xml', _modlib_path=modlib_path),\n File \"/usr/local/lib/python3.8/runpy.py\", line 207, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/local/lib/python3.8/runpy.py\", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/local/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 987, in <module>\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 977, in main\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 656, in ensure_xpath_exists\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 594, in check_or_make_target\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 593, in check_or_make_target\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 417, in is_node\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 441, in xpath_matches\n File \"src/lxml/etree.pyx\", line 2311, in lxml.etree._ElementTree.xpath\n File \"src/lxml/xpath.pxi\", line 357, in lxml.etree.XPathDocumentEvaluator.__call__\n File \"src/lxml/xpath.pxi\", line 225, in lxml.etree._XPathEvaluatorBase._handle_result\nlxml.etree.XPathEvalError: Invalid expression\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
PLAY RECAP *************
我使用的是Ansible核心版本,
ansible [core 2.12.3]
python version = 3.8.12 (default, Mar 29 2022, 01:40:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Name: lxml
Version: 4.8.0
您可以建议您解决问题以及如何克服它吗?
Below is my Ansible playbook to print <driver_name>
from XML using Ansible lxml
Python plugin.
---
- name: ReadJsonfile
hosts: localhost
tasks:
- name: Print drivers
community.general.xml:
path: jdbc.xml.bkp
xpath: /jdbc_data_source/jdbc_driver_params/driver_name
register: hits
- ansible.builtin.debug:
var: hits
I got the below error running the playbook:
Error while parsing document: jdbc.xml.bkp (Unsupported encoding UTF_8
One of the solutions proposed was to delete the below first line from the xml
Below is the first line that I removed from the XML
<?xml version='1.0' encoding='UTF_8'?>
Below is my current XML file:
<jdbc_data_source xmlns="http://xmlns.oracle.com/weblogic/jdbc_data_source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema_instance" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc_data_source http://xmlns.oracle.com/weblogic/jdbc_data_source/1.0/jdbc_data_source.xsd">
<jdbc_driver_params>
<driver_name>oracle.jdbc.OracleDriver</driver_name>
<properties>
<property>
<name>user</name>
</property>
</properties>
<driver_name>tomcat.jdbc.TomcatDriver</driver_name>
</jdbc_driver_params>
</jdbc_data_source>
Unfortunately, I now get the below error:
TASK [Print drivers] *****************************************************************************
Tuesday 29 March 2022 05:04:49 -0500 (0:00:01.427) 0:00:01.443 *********
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: lxml.etree.XPathEvalError: Invalid expression
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 107, in <module>\n _ansiballz_main()\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/wladmin/.ansible/tmp/ansible-tmp-1648548289.1452537-29064-207706807914112/AnsiballZ_xml.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.xml', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.xml', _modlib_path=modlib_path),\n File \"/usr/local/lib/python3.8/runpy.py\", line 207, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/local/lib/python3.8/runpy.py\", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/local/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 987, in <module>\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 977, in main\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 656, in ensure_xpath_exists\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 594, in check_or_make_target\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 593, in check_or_make_target\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 417, in is_node\n File \"/tmp/ansible_community.general.xml_payload_yt4drx6g/ansible_community.general.xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py\", line 441, in xpath_matches\n File \"src/lxml/etree.pyx\", line 2311, in lxml.etree._ElementTree.xpath\n File \"src/lxml/xpath.pxi\", line 357, in lxml.etree.XPathDocumentEvaluator.__call__\n File \"src/lxml/xpath.pxi\", line 225, in lxml.etree._XPathEvaluatorBase._handle_result\nlxml.etree.XPathEvalError: Invalid expression\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
PLAY RECAP *************
I'm on Ansible core version
ansible [core 2.12.3]
python version = 3.8.12 (default, Mar 29 2022, 01:40:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Name: lxml
Version: 4.8.0
Can you please suggest what is the issue and how to overcome it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您确实在XML中定义了一个命名空间:
并且您需要考虑并绘制它,并使用
namespaces
参数。因此,这应该是您的任务:
如果您需要简单的这些值列表:
给定三个任务:
这产生:
This happens because you do have an namespace defined in your XML:
And you need to take that into consideration and map it, with the
namespaces
parameter.So, here should be your task:
If you need a simple list of those values:
Given the three tasks:
This yields: