简体   繁体   English

Ansible - 有没有办法访问 USB 设备?

[英]Ansible - Is there a way to get access to USB devices?

I'm building a playbook for my Raspberry Pi 4 and I have a podman container that needs to access the printer (in /dev/bus/usb/XXX/YYY ) device.我正在为我的 Raspberry Pi 4 构建一个剧本,我有一个需要访问打印机(在/dev/bus/usb/XXX/YYY中)设备的podman容器。

I have a task that does the job, but it uses the shell module, which is not recommended:我有一个任务可以完成这项工作,但它使用了不推荐的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

Is there a cleaner way to obtain the bus/usb for my printer?有没有更简洁的方法来为我的打印机获取总线/USB?

I tried searching in the facts, but there is nothing about USB devices and I can't find any module related to USB devices.我尝试在事实中搜索,但没有关于 USB 设备的任何信息,我找不到任何与 USB 设备相关的模块。

Ansible - Is there a way to get access to USB devices? Ansible - 有没有办法访问 USB 设备?

The short answer is yes, there are multiple ways possible.简短的回答是肯定的,有多种可能的方法。

Despite of the already given recommendation within the comments about using or Developing Custom Modules , regarding尽管在关于使用或开发自定义模块的评论中已经给出了建议,关于

Is there a cleaner way to obtain...有没有更清洁的方法来获得...

you may have a look into the following approach您可以查看以下方法

---
- 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 }}"

resulting into an output (in example) of导致输出(例如)

...
    "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" : [

        ]
      }
    ]
  }
...

Here it is assumed that lshw is pre-installed on the Remote Node(s), which is the case for certain Linux distributions.此处假定lshw已预安装在远程节点上,某些 Linux 发行版就是这种情况。 It has the capability to list information for certain object classes and can provide the output in JSON format which can easily be registered and processed via Ansible.它能够列出某些对象类的信息,并可以提供 JSON 格式的输出,可以通过 Ansible 轻松注册和处理。

You can lookup the available classes via sudo lshw -short .您可以通过sudo lshw -short查找可用的类。 USB devices are behind the class bus , so maybe lshw -class bus -json could be enough. USB 设备在类bus后面,所以也许lshw -class bus -json就足够了。 I'll leave further testing and implementation up to you.我会将进一步的测试和实施留给您。

Further Reading进一步阅读


Regarding your comment about lsusb you could probably use an approach of pre-processing the output.关于您对lsusb的评论,您可能会使用一种预处理输出的方法。

To do so, you may have a look into the tool JSON Convert ( jc ) and jc.parsers.lsub .为此,您可以查看工具 JSON Convert ( jc ) 和jc.parsers.lsub Your command could change to jc lsusb -v only.您的命令只能更改为jc lsusb -v The converter seems to be available as Ansible module too jc filter – Convert output of many shell commands and file-types to JSON .该转换器似乎也可以作为 Ansible 模块使用jc filter – 将许多 shell 命令和文件类型的输出转换为 JSON

In both ways you can get smaller, simpler, less and easier to maintain code, and already JSON formatted data structures.通过这两种方式,您可以获得更小、更简单、更少和更容易维护的代码,以及已经 JSON 格式的数据结构。 Furthermore an other kind of data processing since there is no need for grep , awk and regex_search .此外还有另一种数据处理,因为不需要grepawkregex_search

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM