从XML解析IP地址 - Ansible

时间:2018-05-14 18:20:56

标签: regex ansible

我正在尝试使用Playbook中的regex_findall功能来解析从以XML格式返回值的API调用返回的IP地址。

示例数据:

me><certificate-expiry>2027/07/31 18:29:03</certificate-expiry><connected-at>2018/05/03 13:10:20</connected-at><custom-certificate-usage>no</custom-certificate-usage><multi-vsys>no</multi-vsys><vsys><entry name=\"vsys1\"><display-name>vsys1</display-name><shared-policy-status></shared-policy-status><shared-policy-md5sum>9bef65fea5df7d86617dbecfb1ef6053</shared-policy-md5sum></entry></vsys></entry><entry name=\"001606026760\"><serial>001606026760</serial><connected>yes</connected><unsupported-version>no</unsupported-version><hostname>us-583-int-fw-01</hostname><ip-address>10.200.226.8</ip-address><mac-addr></mac-addr><uptime>72 days, 6:19:31</uptime><family>200</family><model>PA-200</model><sw-version>8.0.0</sw-version><app-version>8013-4681</app-version><av-version>2599-3095</av-version><wildfire-version>240995-243478</wildfire-version><threat-version>8013-4681</threat-version><url-db>paloaltonetworks</url-db><url-filtering-version>20180508.40211</url-filtering-version><logdb-version>8.0.15</logdb-version><vpnclient-package-version></vpnclient-package-version><global-protect-client-package-version>0.0.0</global-protect-client-package-version><domain></domain><vpn-disable-mode>no</vpn-disable-mode><operational-mode>normal</operational-mode><certificate-status></certificate-status><certificate-subject-name>001606026760</certificate-subject-name><certificate-expiry>2027/02/08 17:39:01</certificate-expiry><connected-at>2018/05/03 13:10:20</connected-at><custom-certificate-usage>no</custom-certificate-usage><multi-vsys>no</multi-vsys><vsys><entry name=\"vsys1\"><display-name>vsys1</display-name><shared-policy-status></shared-policy-status><shared-policy-md5sum>47c67d0aeec16c2dbd738ffab7948886</shared-policy-md5sum></entry></vsys></entry><entry name=\"001606055848\"><serial>001606055848</serial><connected>yes</connected><unsupported-version>no</unsupported-version><hostname>us-575-int-fw-01</hostname><ip-address>10.200.230.8</ip-address><mac-addr></mac-addr><uptime>111 days, 12:30:25</uptime><family>200</family><model>PA-200</model><sw-version>8.0.7</sw-version><app-version>8017-4708</app-version><av-version>2609-3105</av-version><wildfire-version>243887-246373</wildfire-version><threat-version>8017-4708</threat-version><url-db>paloaltonetworks</url-db><url-filtering-version>20180514.20206</url-filtering-version><logdb-version>8.0.16</logdb-version><vpnclient-package-version></vpnclient-package-version><global-protect-client-package-version>0.0.0</global-protect-client-package-version><domain></domain><vpn-disable-mode>no</vpn-disable-mode><operational-mode>normal</operational-mode><certificate-status></certificate-status><certificate-subject-name>

我将批量XML结果存储在register: request_devices中,然后尝试解析:

- name: Parse XML result for IP Address
  set_fact:
    returned_ipaddress: "{{ request_devices | regex_findall('\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b') }}"

然而,当我运行游戏时,我得到以下内容:

ERROR! Syntax Error while loading YAML.
  found unknown escape character

The error appears to have been in '/Users/tfranklin/gitrepos/retail/test-clean-palo-alto/testpw.yaml': line 21, column 81, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      set_fact:
        returned_ipaddress: "{{ request_devices | regex_findall('\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b') }}"
                                                                                ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我尝试使用\转义\\,但仍然失败。

1 个答案:

答案 0 :(得分:1)

Obligatory "can't parse n with regular expressions link"

当然有时您可以,但在这种情况下,如果您使用最近的(&gt; 2.4)版本的Ansible,则可以使用the xml module代替。

我已经截断了你的xml,但是这里显示的方法也适用于更长/更深层次嵌套的文件:

data.xml中

<entry name="001606026760">
  <serial>001606026760</serial>
  <connected>yes</connected>
  <unsupported-version>no</unsupported-version>
  <hostname>us-583-int-fw-01</hostname>
  <ip-address>10.200.226.8</ip-address>
  <mac-addr></mac-addr>
  <uptime>72 days, 6:19:31</uptime>
  <family>200</family>
  <model>PA-200</model>
  <sw-version>8.0.0</sw-version>
  <app-version>8013-4681</app-version>
  <av-version>2599-3095</av-version>
  <wildfire-version>240995-243478</wildfire-version>
</entry>

playbook.yml

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Get IP address from xmldata.
      xml:
        path: "data.xml"
        content: "text"
        xpath: "/entry/ip-address"

输出

PLAY [localhost] *********************************************************************************

TASK [Get IP address from xmldata.] **************************************************************
ok: [localhost] => changed=false
  actions:
    namespaces: {}
    state: present
    xpath: /entry/ip-address
  count: 1
  matches:
  - ip-address: 10.200.226.8
  msg: 1

PLAY RECAP ***************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0