例如这里我们有一个摘要列表。我想知道是否有简单的方法可以从中生成 get_url 的校验和值。
有没有简单的方法将rdf解析成字典?或者也许我们可以像xml一样查询rdf路径?
更新
我刚刚意识到 rdf 是带有一些命名空间的 xml。我可以用像Python的lxml这样的程序语言来查询它,如下所示:
from lxml import etree
root = etree.parse("/tmp/digest.rdf")
ns = root.getroot().nsmap
root.xpath('/rdf:RDF/digest:Content[@rdf:about="magick"]/digest:sha256/text()', namespaces=ns)[0]
但我无法让它在 ansible 中工作:
- 我必须显式设置名称空间参数。
- 我不能
/text()
让它只返回文本。如果没有它,返回的匹配值是一个字典列表。
ansible localhost -m xml -a 'path=/tmp/digest.rdf xpath=/rdf:RDF/digest:Content[@rdf:about="magick"]/digest:sha256/text() print_match=yes' -i inventory
PLAY [Ansible Ad-Hoc] *************************************************************************************************************************************************************************************
TASK [xml] ************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: lxml.etree.XPathEvalError: Undefined namespace prefix
fatal: [localhost]: FAILED! => changed=false
module_stderr: |-
Traceback (most recent call last):
File "<stdin>", line 107, in <module>
File "<stdin>", line 99, in _ansiballz_main
File "<stdin>", line 47, in invoke_module
File "/usr/lib/python3.10/runpy.py", line 224, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/lib/python3.10/runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/tmp/ansible_xml_payload_zgisyqdh/ansible_xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py", line 989, in <module>
File "/tmp/ansible_xml_payload_zgisyqdh/ansible_xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py", line 943, in main
File "/tmp/ansible_xml_payload_zgisyqdh/ansible_xml_payload.zip/ansible_collections/community/general/plugins/modules/xml.py", line 399, in do_print_match
File "src/lxml/etree.pyx", line 2311, in lxml.etree._ElementTree.xpath
File "src/lxml/xpath.pxi", line 357, in lxml.etree.XPathDocumentEvaluator.__call__
File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Undefined namespace prefix
module_stdout: ''
msg: |-
MODULE FAILURE
See stdout/stderr for the exact error
rc: 1
有没有办法让它像lxml一样自动获取nsmap?