How can I use Ansible to print the CRC count using 'ip -s -s link show' command in Red Hat machine?

huangapple go评论68阅读模式
英文:

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. 读取编号为1:2:的行,并映射接口为loeth0
  2. 现在读取行,并为每个接口(loeth0)搜索包含"RX errors: crc"的行。
  3. 还要读取"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:

 &quot;stdout_lines&quot;: [
        &quot;1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000&quot;,
        &quot;    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00&quot;,
        &quot;    RX: bytes  packets  errors  dropped overrun mcast   &quot;,
        &quot;    0          0        0       0       0       0       &quot;,
        &quot;    RX errors: length   crc     frame   fifo    missed&quot;,
        &quot;               0        0       0       0       0       &quot;,
        &quot;    TX: bytes  packets  errors  dropped carrier collsns &quot;,
        &quot;    0          0        0       0       0       0       &quot;,
        &quot;    TX errors: aborted  fifo   window heartbeat transns&quot;,
        &quot;               0        0       0       0       0       &quot;,
        &quot;2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000&quot;,
        &quot;    link/ether 00:15:5d:e3:bb:dd brd ff:ff:ff:ff:ff:ff&quot;,
        &quot;    RX: bytes  packets  errors  dropped overrun mcast   &quot;,
        &quot;    102522514  69859    0       0       0       1205    &quot;,
        &quot;    RX errors: length   crc     frame   fifo    missed&quot;,
        &quot;               0        0       0       0       0       &quot;,
        &quot;    TX: bytes  packets  errors  dropped carrier collsns &quot;,
        &quot;    1179376    15194    0       0       0       0       &quot;,
        &quot;    TX errors: aborted  fifo   window heartbeat transns&quot;,
        &quot;               0        0       0       0       5       &quot;
    ]

So far I have tried these steps to create an Ansible playbook

  1. Read the line numbered 1:, 2: and map the interfaces lo, eth0
  2. Now read the lines and search the line with "RX errors: crc" for each interface (lo, eth0)
  3. 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: &quot;{{ item.ifname }}&quot;
        crc_errors: &quot;{{ item.stats64.rx.crc_errors }}&quot;
      ansible.builtin.debug:
        msg: &quot;CRC Errors for link {{ link }}: {{ crc_errors }}&quot;
      loop: &quot;{{ ip_link_cmd.stdout | from_json
        | selectattr(&#39;ifname&#39;, &#39;in&#39;, report_cards) }}&quot;
      loop_control:
        label: &quot;{{ link }}&quot;

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] =&gt; (item=lo) =&gt; {
    &quot;msg&quot;: &quot;CRC Errors for link lo: 0&quot;
}
ok: [localhost] =&gt; (item=enp5s0) =&gt; {
    &quot;msg&quot;: &quot;CRC Errors for link enp5s0: 0&quot;
}

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&#39;s result to JSON
    ansible.builtin.debug:
      msg: &quot;{{ result.stdout | community.general.jc(&#39;ip&#39;) }}&quot;

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

huangapple
  • 本文由 发表于 2023年5月29日 15:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定