Ansible Telnet 输出解析

Ansible Telnet output parse

我正在使用 ansible telnet 模块登录到位于 http://www.routeservers.org/ 的 public 路由服务器 (ATT-route-server.ip.att.net) 并进行 ping 测试。这是我的脚本:

A 部分:

---
- name: ping test
  hosts: telnet
  gather_facts: false
  connection: local
  tasks:
    - name: telnet prs
      ansible.netcommon.telnet:
        user: rviews
        password: rviews
        login_prompt: "login: "
        password_prompt: "Password:"
        prompts:
          - '[>|#]'
        command:
          - ping 8.8.8.8 count 10
      register: ping

    - name: output
      debug: msg="{{ping.output}}"
[telnet]
route-server.ip.att.net

我看到的输出是:

ansible-playbook -i telnet_inv.inv ping4.yml

PLAY [ping test] ************************************************************************************************************************************************************

TASK [telnet prs] ***********************************************************************************************************************************************************
changed: [route-server.ip.att.net]

TASK [output] ***************************************************************************************************************************************************************
ok: [route-server.ip.att.net] => {
    "msg": [
        " ping 8.8.8.8 count 10 \r\nPING 8.8.8.8 (8.8.8.8): 56 data bytes\r\n64 bytes from 8.8.8.8: icmp_seq=0 ttl=118 time=4.113 ms\r\n64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=4.078 ms\r\n64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=4.139 ms\r\n64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=5.017 ms\r\n64 bytes from 8.8.8.8: icmp_seq=4 ttl=118 time=4.170 ms\r\n64 bytes from 8.8.8.8: icmp_seq=5 ttl=118 time=4.175 ms\r\n64 bytes from 8.8.8.8: icmp_seq=6 ttl=118 time=5.078 ms\r\n64 bytes from 8.8.8.8: icmp_seq=7 ttl=118 time=4.093 ms\r\n64 bytes from 8.8.8.8: icmp_seq=8 ttl=118 time=4.080 ms\r\n64 bytes from 8.8.8.8: icmp_seq=9 ttl=118 time=4.108 ms\r\n\r\n--- 8.8.8.8 ping statistics ---\r\n10 packets transmitted, 10 packets received, 0% packet loss\r\nround-trip min/avg/max/stddev = 4.078/4.305/5.078/0.373 ms\r\n\r\nrviews@route-server.ip.att.net>"
    ]
}

PLAY RECAP ******************************************************************************************************************************************************************
route-server.ip.att.net    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我想解析输出的最后一部分,如下所示。如何实现?

--- 8.8.8.8 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss

我看了 ansible telnet 模块 'return value' 部分,它没有标准输出或 stdout_lines https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/telnet_module.html

B 部分:我为 ping 创建了循环

---
- name: ping test
  hosts: telnet
  gather_facts: false
  connection: local
  tasks:
    - name: grabe variables
      include_vars: 'pingvar.yml'

    - name: telnet prs
      ansible.netcommon.telnet:
        user: rviews
        password: rviews
        login_prompt: "login: "
        password_prompt: "Password:"
        prompts:
          - '[>|#]'
        command:
          - ping {{item.ip}} count 10
      with_items: "{{ ips }}"
      register: ping

    - name: output
      debug:
        msg: "{{ ping.results|map(attribute='output').0.split('\r\n')[-4]}}"
      register: splitoutput1

变量

ips:
   - ip: 8.8.8.8
   - ip: 1.1.1.1

出现错误:

 ansible-playbook -i telnet_inv.inv ping4.yml

PLAY [ping test] ************************************************************************************************************************************************************

TASK [grabe variables] ******************************************************************************************************************************************************
ok: [route-server.ip.att.net]

TASK [telnet prs] ***********************************************************************************************************************************************************
changed: [route-server.ip.att.net] => (item={'ip': '8.8.8.8'})
changed: [route-server.ip.att.net] => (item={'ip': '1.1.1.1'})

TASK [output] ***************************************************************************************************************************************************************
fatal: [route-server.ip.att.net]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got 'split'. String: {{ ping.results|map(attribute='output') split('\r\n')}}"}

PLAY RECAP ******************************************************************************************************************************************************************
route-server.ip.att.net    : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Vish@SRMB-10130 cisco $ ansible-playbook -i telnet_inv.inv ping4.yml

PLAY [ping test] ************************************************************************************************************************************************************

TASK [grabe variables] ******************************************************************************************************************************************************
ok: [route-server.ip.att.net]

TASK [telnet prs] ***********************************************************************************************************************************************************
changed: [route-server.ip.att.net] => (item={'ip': '8.8.8.8'})
changed: [route-server.ip.att.net] => (item={'ip': '1.1.1.1'})

TASK [output] ***************************************************************************************************************************************************************
fatal: [route-server.ip.att.net]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got '.'. String: {{ ping.results|map(attribute='output').0.split('\r\n')[-4]}}"}

PLAY RECAP ******************************************************************************************************************************************************************
route-server.ip.att.net    : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

我想解析输出的最后一部分,如下所示。如何实现?

--- 8.8.8.8 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss

--- 1.1.1.1 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss




试试这个

    - debug:
        msg: "{{ ping.output.0.split('\r\n')[-4] }}"

要显示第一项结果试试这个

    - debug:
        msg: "{{ r1.split('\r\n')[-4] }}"
      vars:
        r1: "{{ ping.results|map(attribute='output')|first }}"