【问题标题】:Ansible Telnet output parseAnsible Telnet 输出解析
【发布时间】:2021-04-20 01:41:45
【问题描述】:

我正在使用 ansible telnet 模块在http://www.routeservers.org/ 登录公共路由服务器 (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 模块的“返回值”部分,它没有 stdout 或 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




【问题讨论】:

    标签: parsing ansible telnet


    【解决方案1】:

    试试这个

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

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

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

    【讨论】:

    • 谢谢你,它适用于 Part-A,我修改了问题并在问题中添加了 PART-B(循环 ping 命令)。无法为 B 部分工作。哪里错了??
    猜你喜欢
    • 2014-10-19
    • 2019-03-23
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多