首页 > 解决方案 > Ansible Xml 模块 - 从寄存器中获取带有命名空间的 xml 值

问题描述

有一个 pom.xml 文件,我使用以下任务提取项目名称

- name: Loading project variables from the parent Maven pom.xml
  xml:
    path: "/some/path/pom.xml"
    namespaces:
      ns: "http://maven.apache.org/POM/4.0.0"
    xpath: "/ns:project/ns:name"
    content: text
  register: project_name_reg

- name: "Set project name"
  set_fact:
    project_name: "{{ project_name_reg.matches[0] }}"

它有效,现在project_name包含:

"project_name": {
        "{http://maven.apache.org/POM/4.0.0}name": "My Project Parent"
    }

如何My Project Parent通过这个带有命名空间的复杂{http://maven.apache.org/POM/4.0.0}name名称提取真正的价值?这是一个简单的字典,但我所有的尝试都失败了。

标签: xmlxpathansiblexml-parsing

解决方案


你根本不在乎那把钥匙。你可以这样做:

- name: "Set project name"
  set_fact:
    project_name: "{{ project_name_reg.matches[0].values()|first }}"

推荐阅读