英文:
How can I use Ansible to print the CRC count using 'ip -s -s link show' command in Red Hat machine?
问题
我一直在尝试使用Ansible来使用ip -s -s link show
命令在Red Hat机器上打印CRC计数。我的stdout_lines
是一个包含以下输出的列表:
"stdout_lines": [
"1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000",
" link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00",
" RX: bytes packets errors dropped overrun mcast ",
" 0 0 0 0 0 0 ",
" RX errors: length crc frame fifo missed",
" 0 0 0 0 0 ",
" TX: bytes packets errors dropped carrier collsns ",
" 0 0 0 0 0 0 ",
" TX errors: aborted fifo window heartbeat transns",
" 0 0 0 0 0 ",
"2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000",
" link/ether 00:15:5d:e3:bb:dd brd ff:ff:ff:ff:ff:ff",
" RX: bytes packets errors dropped overrun mcast ",
" 102522514 69859 0 0 0 1205 ",
" RX errors: length crc frame fifo missed",
" 0 0 0 0 0 ",
" TX: bytes packets errors dropped carrier collsns ",
" 1179376 15194 0 0 0 0 ",
" TX errors: aborted fifo window heartbeat transns",
" 0 0 0 0 5 "
]
到目前为止,我已经尝试了以下步骤来创建一个Ansible playbook:
- 读取编号为
1:
和2:
的行,并映射接口为lo
和eth0
。 - 现在读取行,并为每个接口(
lo
,eth0
)搜索包含"RX errors: crc
"的行。 - 还要读取"
RX errors: crc
"行下面的行,并打印CRC列的值。
但我没有成功。欢迎任何建议。
英文:
I've been trying to use Ansible to print the CRC count using the ip -s -s link show
command in a Red Hat machine. My stdout_lines
is a list with below output:
"stdout_lines": [
"1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000",
" link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00",
" RX: bytes packets errors dropped overrun mcast ",
" 0 0 0 0 0 0 ",
" RX errors: length crc frame fifo missed",
" 0 0 0 0 0 ",
" TX: bytes packets errors dropped carrier collsns ",
" 0 0 0 0 0 0 ",
" TX errors: aborted fifo window heartbeat transns",
" 0 0 0 0 0 ",
"2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000",
" link/ether 00:15:5d:e3:bb:dd brd ff:ff:ff:ff:ff:ff",
" RX: bytes packets errors dropped overrun mcast ",
" 102522514 69859 0 0 0 1205 ",
" RX errors: length crc frame fifo missed",
" 0 0 0 0 0 ",
" TX: bytes packets errors dropped carrier collsns ",
" 1179376 15194 0 0 0 0 ",
" TX errors: aborted fifo window heartbeat transns",
" 0 0 0 0 5 "
]
So far I have tried these steps to create an Ansible playbook
- Read the line numbered
1:
,2:
and map the interfaceslo
,eth0
- Now read the lines and search the line with "
RX errors: crc
" for each interface (lo
,eth0
) - Also read the lines below "
RX errors: crc
" line and print the value for CRC column
But I did not succeed. Any suggestion is welcome.
答案1
得分: 1
不要玩弄不令人满意的人类格式,当可用时,请使用专为从头开始解析而制作的格式(即 JSON,ip
命令的 -j
开关)。
以下示例获取了我家机器上可用的卡的信息。根据您的需求更改变量。
---
- name: 显示给定网络卡的 CRC 错误
hosts: localhost
gather_facts: false
vars:
report_cards:
- lo
- enp5s0
tasks:
- name: 从链接获取详细信息
ansible.builtin.command: ip -s -s -j link show
register: ip_link_cmd
changed_when: false
- name: 显示给定设备的信息
vars:
link: "{{ item.ifname }}"
crc_errors: "{{ item.stats64.rx.crc_errors }}"
ansible.builtin.debug:
msg: "链接 {{ link }} 的 CRC 错误:{{ crc_errors }}"
loop: "{{ ip_link_cmd.stdout | from_json
| selectattr('ifname', 'in', report_cards) }}"
loop_control:
label: "{{ link }}"
在我的机器上运行上面的剧本会得到以下结果:
PLAY [显示给定网络卡的 CRC 错误] *******************************************************
TASK [从链接获取详细信息] *************************************************************
ok: [localhost]
TASK [显示给定设备的信息] ************************************************************
ok: [localhost] => (item=lo) => {
"msg": "链接 lo 的 CRC 错误:0"
}
ok: [localhost] => (item=enp5s0) => {
"msg": "链接 enp5s0 的 CRC 错误:0"
}
PLAY RECAP *************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
英文:
Rather than playing around unsatisfyingly with a format made for humans, use a format made for parsing from scratch when it is available (aka json which is the -j
switch for the ip
command).
The below example gets information from cards available on my home machine. Change the var to whatever suits your needs.
---
- name: Display CRC errors for given network cards
hosts: localhost
gather_facts: false
vars:
report_cards:
- lo
- enp5s0
tasks:
- name: Get detailed information from links
ansible.builtin.command: ip -s -s -j link show
register: ip_link_cmd
changed_when: false
- name: Display information for given devices
vars:
link: "{{ item.ifname }}"
crc_errors: "{{ item.stats64.rx.crc_errors }}"
ansible.builtin.debug:
msg: "CRC Errors for link {{ link }}: {{ crc_errors }}"
loop: "{{ ip_link_cmd.stdout | from_json
| selectattr('ifname', 'in', report_cards) }}"
loop_control:
label: "{{ link }}"
Running the above playbook on my machine gives:
PLAY [Display CRC errors for given network cards] *******************************************************
TASK [Get detailed information from links] **************************************************************
ok: [localhost]
TASK [Display information for given devices] ************************************************************
ok: [localhost] => (item=lo) => {
"msg": "CRC Errors for link lo: 0"
}
ok: [localhost] => (item=enp5s0) => {
"msg": "CRC Errors for link enp5s0: 0"
}
PLAY RECAP **********************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
答案2
得分: 0
总的来说,ip
命令通常具有-j, --json
选项,在RHEL 7.x的示例中,它没有此选项。在这种情况下,可能需要使用其他方法。
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: 获取链接的详细信息
ansible.builtin.command: ip -s -s link show
register: result
changed_when: false
- name: 将命令结果转换为JSON
ansible.builtin.debug:
msg: "{{ result.stdout | community.general.jc('ip') }}"
请注意,jc
过滤器 - 将许多Shell命令和文件类型的输出转换为JSON有其自己的要求,需要在解决之前解决。此外,还需要指定解析器。由于缺乏测试环境,我尚未测试ip
命令。
进一步的类似问答文档
英文:
Whereby in general the ip
command has an option -j, --json
in example in RHEL 7.x it hasn't. There it might be necessary to use an other approach.
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Get detailed information from links
ansible.builtin.command: ip -s -s link show
register: result
changed_when: false
- name: Convert command's result to JSON
ansible.builtin.debug:
msg: "{{ result.stdout | community.general.jc('ip') }}"
Please take note that jc
filter – Convert output of many shell commands and file-types to JSON has it's own requirements which needs to resolved before. Furthermore, it will be necessary to specifiy the Parser. Because of the lack of testing environment, I haven't tested for the ip
command yet.
Further Documentation Similar Q&A
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论