Parsing Ansible Telnet输出解析

Parsing Ansible Telnet输出解析,parsing,ansible,telnet,Parsing,Ansible,Telnet,我正在使用ansible telnet模块登录到位于的公共路由服务器(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

我正在使用ansible telnet模块登录到位于的公共路由服务器(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}}"
我看到的结果是:

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
--- 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
我查看了ansible telnet模块的“返回值”部分,它没有标准输出或标准输出行

B部分:我为平做了一个循环

---
- 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
变量

获取错误:

 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
--- 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
试试这个

-调试:
msg:“{ping.output.0.split('\r\n')[-4]}”

要显示第一项结果,请尝试以下操作

-调试:
msg:“{r1.split('\r\n')[-4]}”
变量:
r1:{{ping.results | map(attribute='output')| first}

感谢您的上述建议,它对A部分有效,我修改了问题并在问题中添加了B部分(循环ping命令)。无法为b部分工作。错误在哪里??。