使用Ansible LXML模块解析XML的错误

发布于 01-17 16:22 字数 5193 浏览 2 评论 0原文

以下是我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2025-01-24 16:22:14

发生这种情况是因为您确实在XML中定义了一个命名空间:

xmlns="http://xmlns.oracle.com/weblogic/jdbc_data_source"

并且您需要考虑并绘制它,并使用 namespaces 参数。

因此,这应该是您的任务:

- community.general.xml:
  path: jdbc.xml.bkp
    xpath: /x:jdbc_data_source/x:jdbc_driver_params/x:driver_name
    namespaces:
      x: http://xmlns.oracle.com/weblogic/jdbc_data_source
    content: text
  register: hits

如果您需要简单的这些值列表:

- debug:
    var: hits.matches | map('dict2items') | flatten | map(attribute="value")

给定三个任务:

- community.general.xml:
    path: jdbc.xml.bkp
    xpath: /x:jdbc_data_source/x:jdbc_driver_params/x:driver_name
    namespaces:
      x: http://xmlns.oracle.com/weblogic/jdbc_data_source
    content: text
  register: hits

- debug:
    var: hits.matches

- debug:
    var: hits.matches | map('dict2items') | flatten | map(attribute="value")

这产生:

TASK [debug] *************************************************************************************
ok: [localhost] => 
  hits.matches:
  - '{http://xmlns.oracle.com/weblogic/jdbc_data_source}driver_name': oracle.jdbc.OracleDriver
  - '{http://xmlns.oracle.com/weblogic/jdbc_data_source}driver_name': tomcat.jdbc.TomcatDriver

TASK [debug] *************************************************************************************
ok: [localhost] => 
  hits.matches | map('dict2items') | flatten | map(attribute="value"):
  - oracle.jdbc.OracleDriver
  - tomcat.jdbc.TomcatDriver

This happens because you do have an namespace defined in your XML:

xmlns="http://xmlns.oracle.com/weblogic/jdbc_data_source"

And you need to take that into consideration and map it, with the namespaces parameter.

So, here should be your task:

- community.general.xml:
  path: jdbc.xml.bkp
    xpath: /x:jdbc_data_source/x:jdbc_driver_params/x:driver_name
    namespaces:
      x: http://xmlns.oracle.com/weblogic/jdbc_data_source
    content: text
  register: hits

If you need a simple list of those values:

- debug:
    var: hits.matches | map('dict2items') | flatten | map(attribute="value")

Given the three tasks:

- community.general.xml:
    path: jdbc.xml.bkp
    xpath: /x:jdbc_data_source/x:jdbc_driver_params/x:driver_name
    namespaces:
      x: http://xmlns.oracle.com/weblogic/jdbc_data_source
    content: text
  register: hits

- debug:
    var: hits.matches

- debug:
    var: hits.matches | map('dict2items') | flatten | map(attribute="value")

This yields:

TASK [debug] *************************************************************************************
ok: [localhost] => 
  hits.matches:
  - '{http://xmlns.oracle.com/weblogic/jdbc_data_source}driver_name': oracle.jdbc.OracleDriver
  - '{http://xmlns.oracle.com/weblogic/jdbc_data_source}driver_name': tomcat.jdbc.TomcatDriver

TASK [debug] *************************************************************************************
ok: [localhost] => 
  hits.matches | map('dict2items') | flatten | map(attribute="value"):
  - oracle.jdbc.OracleDriver
  - tomcat.jdbc.TomcatDriver
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文