ansible以字符串的形式读取xml的一部分-而不是dict/list [英] ansible read part of xml as string - not as dict / list

查看:68
本文介绍了ansible以字符串的形式读取xml的一部分-而不是dict/list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下提到的xml:

I have a below mentioned xml:

<?xml version='1.0' encoding='UTF-8'?>`  
<Envelope>  
  <Body>  
    <response>  
      <timestamp>2018-11-01T15:18:44-04:00</timestamp>  
      <content>`  
        <element1>value1</element1>  
        <element2>value2(/element2>  
        <element3>  
          <child1>c1</child1>  
          <child2>c2</child2>  
        </element3>  
      </content>  
    </response>  
  </Body>  
</Envelope>  

我必须捕获xml格式的 content 标记子代,才能对其进行编码.

I have to capture the content tag children in xml format only to encode it.

当我使用xml模块获取内容及其后代时,将其捕获为词典列表.

when I use xml module to get the content and its descendants, captured as list of dictionaries.

我想要的就是将 content 捕获为类似

All I want is I should capture the content as a string like

"<element1>value1</element1>  
 <element2>value2(/element2>  
 <element3>  
      <child1>c1</child1>  
      <child2>c2</child2>  
 </element3>"

作为字符串.
稍后,我将使用此字符串进行编码和解码.

as a string.
Later I will use this string to encode and decode.

我不想对 content 的每个后代进行编码,而是对所有 content 进行编码.

I do not want to encode each and every descendant of content, but all the content together.

如何使用ansible实现此目的.我正在使用ansible 2.4版

How can I achieve this using ansible. I am using ansible version 2.4

推荐答案

基于阅读 Ansible xml模块,似乎不可能.

Based off reading the Ansible xml module, it doesn't seem possible.

您可以使用诸如 xmllint 之类的实用程序使用 command 模块来做到这一点:

You can use the command module using a utility like xmllint to do it like this:

---
- name: run the playbook tasks on the localhost
  hosts: 127.0.0.1
  connection: local
  tasks:
  - name: Get Content Section
    command: "xmllint --xpath '/Envelope/Body/response/content' --format test.xml"
    register: out

  - name: output
    debug:
      msg: "{{ out.stdout }}"

看起来像这样:

PLAY [run the playbook tasks on the localhost] ***************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [127.0.0.1]

TASK [Get Content Section] ***********************************************************************************************************************
changed: [127.0.0.1]

TASK [output] ************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "<content><element1>value1</element1><element2>value2</element2><element3><child1>c1</child1><child2>c2</child2></element3></content>"
}

PLAY RECAP     ***************************************************************************************************************************************
127.0.0.1                  : ok=3    changed=1    unreachable=0    failed=0

这篇关于ansible以字符串的形式读取xml的一部分-而不是dict/list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆