linux Ansible -是否有办法访问USB设备?

bnlyeluc  于 2022-12-26  发布在  Linux
关注(0)|答案(1)|浏览(136)

我正在为我的Raspberry Pi 4构建一个剧本,我有一个podman容器,需要访问打印机(在/dev/bus/usb/XXX/YYY中)设备。
我有一个任务可以完成这项工作,但它使用了shell模块,不推荐使用该模块:

- name: "ScannerJS - Detect printer's USB bus"
  block:
    - name: "ScannerJS - Detect printer's USB bus"
      ansible.builtin.shell: |
        set -e -o pipefail
        lsusb | grep Epson | awk '{print "/dev/bus/usb/"$2"/"substr($4, 1, length($4)-1)}'
      args:
        executable: /bin/bash
      register: scUSBBus
      failed_when: scUSBBus.stdout | regex_search("^\/dev\/bus\/usb\/(\\d{3})\/(\\d{3})$") is none

    - name: "ScannerJS - assert the device path exists"
      ansible.builtin.stat:
        path: "{{ scUSBBus.stdout }}"
      register: scUSBBusStat
      failed_when: scUSBBusStat.stat.exists is not true

是否有更干净的方法来获取打印机的总线/USB?

我试着在事实中搜索,但没有关于USB设备的任何内容,我找不到任何与USB设备相关的模块。

cwdobuhd

cwdobuhd1#

  • Ansible -是否有办法访问USB设备?*

简短的回答是肯定的,有多种可能的方法。
尽管在注解中已经给出了关于使用或Developing Custom Modules的建议,但关于

  • 有没有更干净的方式来获得 *

您可以考虑以下方法

---
- hosts: localhost
  become: true # is necessary to gather data
  gather_facts: false

  tasks:

  - name: List USB hardware
    command: lshw -json
    register: lshw

  - name: Show USB hardware
    debug:
      msg: "{{ lshw.stdout }}"

导致输出(在示例中

...
    "children" : [                                                                                                                                                                                                                                                                                                                                                                                                                                                                 {
    "id" : "usb",
    "class" : "bus",
    "claimed" : true,
    "handle" : "PCI:0000:00:14.0",
    "description" : "USB controller",
    "product" : "C620 Series Chipset Family USB 3.0 xHCI Controller",
    "vendor" : "Intel Corporation",
    "physid" : "14",
    "businfo" : "pci@0000:00:14.0",
    "version" : "09",
    "width" : 64,
    "clock" : 33000000,
    "configuration" : {
      "driver" : "xhci_hcd",
      "latency" : "0"
    },
    "capabilities" : {
      "pm" : "Power Management",
      "msi" : "Message Signalled Interrupts",
      "xhci" : true,
      "bus_master" : "bus mastering",
      "cap_list" : "PCI capabilities listing"
    },
    "children" : [
      {
        "id" : "usbhost:0",
        "class" : "bus",
        "claimed" : true,
        "handle" : "USB:2:1",
        "product" : "xHCI Host Controller",
        "vendor" : "Linux 4.18.0-000.0.0.el8.x86_64 xhci-hcd",
        "physid" : "0",
        "businfo" : "usb@2",
        "logicalname" : "usb2",
        "version" : "4.18",
        "configuration" : {
          "driver" : "hub",
          "slots" : "16",
          "speed" : "480Mbit/s"
        },
        "capabilities" : {
          "usb-2.00" : "USB 2.0"
        },
        "children" : [
          {
            "id" : "usb",
            "class" : "bus",
            "claimed" : true,
            "handle" : "USB:2:3",
            "description" : "USB hub",
            "product" : "Hub",
            "vendor" : "Microchip Technology, Inc. (formerly SMSC)",
            "physid" : "3",
            "businfo" : "usb@2:3",
            "version" : "8.01",
            "configuration" : {
              "driver" : "hub",
              "maxpower" : "2mA",
              "slots" : "2",
              "speed" : "480Mbit/s"
            },
            "capabilities" : {
              "usb-2.00" : "USB 2.0"
            }
          }
        ]
      },
      {
        "id" : "usbhost:1",
        "class" : "bus",
        "claimed" : true,
        "handle" : "USB:3:1",
        "product" : "xHCI Host Controller",
        "vendor" : "Linux 4.18.0-000.0.0.el8.x86_64 xhci-hcd",
        "physid" : "1",
        "businfo" : "usb@3",
        "logicalname" : "usb3",
        "version" : "4.18",
        "configuration" : {
          "driver" : "hub",
          "slots" : "10",
          "speed" : "5000Mbit/s"
        },
        "capabilities" : {
          "usb-3.00" : true
        },
        "children" : [

        ]
      }
    ]
  }
...

这里假设lshw已预装在远程节点上,这是某些Linux发行版的情况。它能够列出某些对象类的信息,并提供JSON格式的输出,可以通过Ansible轻松注册和处理。
你可以通过sudo lshw -short查找可用的类。USB设备在bus类后面,所以lshw -class bus -json可能就足够了。我将把进一步的测试和实现留给你。

进一步阅读

关于您对lsusb的评论,您可能会使用一种预处理输出的方法。
为此,您可以使用JSON Convert(jc)和jc.parsers.lsub工具。您的命令只能更改为jc lsusb -v。该转换器似乎也可以作为Ansible模块jc filter – Convert output of many shell commands and file-types to JSON使用。
这两种方法都可以让代码更小、更简单、更少、更容易维护,并且已经是JSON格式的数据结构。此外,由于不需要grepawkregex_search,因此可以进行另一种数据处理。

相关问题