Ansible购买域名并使用python解析xml输出 [英] Ansible purchasing a domain name and using python to parse the xml output

查看:121
本文介绍了Ansible购买域名并使用python解析xml输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Ansible剧本中的一个动作:

This is an action in my Ansible playbook:

- name: Purchase Domain Name
  local_action: >
        uri
        url=https://api.sandbox.namecheap.com/xml.response
        method=GET
        body="{{ namecheap_purchase_domain_name }}"
        status_code=200
        HEADER_Content-Type="application/x-www-form-urlencoded"
        return_content=yes
  register: domain_name_purchase

- debug: var=domain_name_purchase.content

它返回如下内容:

ok: [162.243.67.77] => {
    "domain_name_purchase.content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ApiResponse Status=\"ERROR\" xmlns=\"http://api.namecheap.com/xml.response\">\r\n  <Errors>\r\n    <Error Number=\"4014104\">Possible duplicate create command for unavailable domain. Try again after 11/20/2013 7:51:07 AM UTC</Error>\r\n  </Errors>\r\n  <Warnings />\r\n  <RequestedCommand>namecheap.domains.create</RequestedCommand>\r\n  <CommandResponse Type=\"namecheap.domains.create\">\r\n    <DomainCreateResult Domain=\"elitereceipt202321414.com\" ChargedAmount=\"0\" DomainID=\"0\" OrderID=\"0\" TransactionID=\"0\" WhoisguardEnable=\"false\" FreePositiveSSL=\"false\" NonRealTimeDomain=\"false\" />\r\n  </CommandResponse>\r\n  <Server>WEB1-SANDBOX1</Server>\r\n  <GMTTimeDifference>--5:00</GMTTimeDifference>\r\n  <ExecutionTime>0.07</ExecutionTime>\r\n</ApiResponse>", 
    "item": ""
}

是否可以使用类似xmltodict.parse的方法来解析xml并将其转换为dict?特别是,我希望返回ApiResponse Status(返回ERROR或SUCCESS)和Error Number.谢谢.

Is it possible to use something like xmltodict.parse to parse the xml and turn it into a dict? In particular I'm looking to return ApiResponse Status (which returns ERROR or SUCCESS) and Error Number. Thanks.

推荐答案

最近有一个外部的Ansible模块,允许解析和处理XML,对其进行检查

There's a recent, external Ansible module that allows to parse and manipulate XML, check it here

不幸的是,即使应该接受XML字符串(并因此接受Ansible变量)作为输入,我也无法使其正常工作.您必须先将XML保存到文件,然后解析它:

Unfortunately, even if it is supposed to accept XML strings (and thus Ansible variables) as input, I couldn't get it to work properly. You would have to save your XML to a file first, and then parse it:

- name: Purchase Domain Name
  local_action: >
        uri
        url=https://api.sandbox.namecheap.com/xml.response
        method=GET
        body="{{ namecheap_purchase_domain_name }}"
        status_code=200
        HEADER_Content-Type="application/x-www-form-urlencoded"
        return_content=yes
  register: domain_name_purchase

- name: save Namecheap output
  copy: content="{{domain_name_purchase.content}}" dest=resp.xml
  delegate_to: localhost

- name: Parse Namecheap XML
  xml: file=resp.xml xpath="/ApiResponse/etc..." content=text
  register: resp_status_node
  delegate_to: localhost

- debug: msg="{{resp_status_node.matches[0]}}"

这篇关于Ansible购买域名并使用python解析xml输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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