parsing - Ansible Telnet 输出解析
问题描述
我正在使用 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
解决方案
尝试这个
- debug:
msg: "{{ ping.output.0.split('\r\n')[-4] }}"
要显示结果的第一项,试试这个
- debug:
msg: "{{ r1.split('\r\n')[-4] }}"
vars:
r1: "{{ ping.results|map(attribute='output')|first }}"
推荐阅读
- c++ - C++ 二次方程未按预期出现
- regex - 我想要一个只接受由逗号或空格分隔的字符列表的正则表达式代码
- java - 最大承载能力为 500 磅的运输车辆,重量为 [130, 170, 140. 100] 的乘客名单,为了安全起见,返回 [130, 170] 以准确保留 300 磅
- windows - 目录中的文件数
- java - 如何修复 Java 中的“类 Rectangle 中的构造函数不能应用于给定类型”错误?
- python - wtforms 上的单元测试失败
- crystal-reports - Crystal Report 中的并行详细信息部分?
- javascript - 使用视差滚动在Javascript中固定/修复div
- android - BoofCV 多边形绘图等效于 Android
- oauth - OAuth/OpenID Connect,如果令牌的标头中没有孩子,我如何从 JWK 中选择密钥?