Debian Codegraph @

ansible_collections/community/general/plugins/modules/usb_facts.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2024, Max Maxopoly <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: usb_facts
12 short_description: Allows listing information about USB devices
13 version_added: 8.5.0
14 description:
15 - Allows retrieving information about available USB devices through C(lsusb).
16 author:
17 - Max Maxopoly (@maxopoly)
18 extends_documentation_fragment:
19 - community.general._attributes
20 - community.general._attributes.facts
21 - community.general._attributes.facts_module
22 requirements:
23 - lsusb binary on PATH (usually installed through the package usbutils and preinstalled on many systems)
24 """
25
26 EXAMPLES = r"""
27 - name: Get information about USB devices
28 community.general.usb_facts:
29
30 - name: Print information about USB devices
31 ansible.builtin.debug:
32 msg: "On bus {{ item.bus }} device {{ item.device }} with id {{ item.id }} is {{ item.name }}"
33 loop: "{{ ansible_facts.usb_devices }}"
34 """
35
36 RETURN = r"""
37 ansible_facts:
38 description: Dictionary containing details of connected USB devices.
39 returned: success
40 type: dict
41 contains:
42 usb_devices:
43 description: A list of USB devices available.
44 returned: success
45 type: list
46 elements: dict
47 contains:
48 bus:
49 description: The bus the usb device is connected to.
50 returned: success
51 type: str
52 sample: "001"
53 device:
54 description: The device number occupied on the bus.
55 returned: success
56 type: str
57 sample: "002"
58 id:
59 description: ID of the USB device.
60 returned: success
61 type: str
62 sample: "1d6b:0002"
63 name:
64 description: Human readable name of the device.
65 returned: success
66 type: str
67 sample: Linux Foundation 2.0 root hub
68 """
69
70 import re
71
72 from ansible.module_utils.basic import AnsibleModule
73
74
75 def parse_lsusb(module, lsusb_path):
76 rc, stdout, stderr = module.run_command(lsusb_path, check_rc=True)
77 regex = re.compile(r"^Bus (\d{3}) Device (\d{3}): ID ([0-9a-f]{4}:[0-9a-f]{4}) (.*)$")
78 usb_devices = []
79 for line in stdout.splitlines():
80 match = re.match(regex, line)
81 if not match:
82 module.fail_json(msg=f"failed to parse unknown lsusb output {line}", stdout=stdout, stderr=stderr)
83 current_device = {"bus": match.group(1), "device": match.group(2), "id": match.group(3), "name": match.group(4)}
84 usb_devices.append(current_device)
85 return_value = {"usb_devices": usb_devices}
86 module.exit_json(
87 msg=f"parsed {len(usb_devices)} USB devices", stdout=stdout, stderr=stderr, ansible_facts=return_value
88 )
89
90
91 def main():
92 module = AnsibleModule({}, supports_check_mode=True)
93
94 # Set LANG env since we parse stdout
95 module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C")
96
97 lsusb_path = module.get_bin_path("lsusb", required=True)
98 parse_lsusb(module, lsusb_path)
99
100
101 if __name__ == "__main__":
102 main()