Ansible - Is there a way to get access to USB devices?
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. 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.
You can lookup the available classes via sudo lshw -short
. USB devices are behind the class bus
, so maybe lshw -class bus -json
could be enough. 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.
To do so, you may have a look into the tool JSON Convert (jc
) and jc.parsers.lsub
.
Your command could change to jc lsusb -v
only. The converter seems to be available as Ansible module too jc
filter – Convert output of many shell commands and file-types to JSON.
In both ways you can get smaller, simpler, less and easier to maintain code, and already JSON formatted data structures. Furthermore an other kind of data processing since there is no need for grep
, awk
and regex_search
.