From 5035437aa26ecdda8d0bff4b4356c9827f7bc50e Mon Sep 17 00:00:00 2001 From: Alexander Grothe Date: Fri, 10 Aug 2018 08:59:46 +0200 Subject: [PATCH] Fix shebang --- Manual.org | 2 +- library/hardware_facts.py | 2 +- library/pci_facts.py | 69 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100755 library/pci_facts.py diff --git a/Manual.org b/Manual.org index dd139f6..d8f4ac1 100644 --- a/Manual.org +++ b/Manual.org @@ -5515,7 +5515,7 @@ fi * Modules This section contains custom modules for the yaVDR Playbooks. They are used to collect facts about the system and configure applications and daemons. ** hardware_facts.py -#+BEGIN_SRC python :tangle library/hardware_facts.py :shebang #!/usr/bin/env/python +#+BEGIN_SRC python :tangle library/hardware_facts.py :shebang "#!/usr/bin/env python" # This Module collects the vendor- and device ids for USB- and PCI(e)-devices and currently loaded kernel modules. DOCUMENTATION = ''' --- diff --git a/library/hardware_facts.py b/library/hardware_facts.py index c9f0693..115aeb8 100755 --- a/library/hardware_facts.py +++ b/library/hardware_facts.py @@ -1,4 +1,4 @@ -#!/usr/bin/env/python +#!/usr/bin/env python # This Module collects the vendor- and device ids for USB- and PCI(e)-devices and currently loaded kernel modules. DOCUMENTATION = ''' --- diff --git a/library/pci_facts.py b/library/pci_facts.py new file mode 100755 index 0000000..5cc0e0c --- /dev/null +++ b/library/pci_facts.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# This module parses the output of lspci for detailed information about available (sub) devices. +DOCUMENTATION = ''' + --- + module: pci_facts + short_description: parses lspci output for detailed (sub) devices data + description: + - This module parses the output of lspci for detailed information about available (sub) devices. + - returns a list with a dict for each device + +notes: + - requires lspci (package pciutils) + +''' + +EXAMPLES = ''' +- name: get detailled pci device infos + pci_facts: + +- debug: + var: pci_devices +''' + + +import argparse +import shlex +import subprocess +from collections import namedtuple + +from ansible.module_utils.basic import * + +def convert2hex(arg): + arg = arg.strip('"') + if arg: + return int(arg, 16) + else: + return None + +def parse_lspci_data(): + parser = argparse.ArgumentParser() + parser.add_argument('-r', '--revision', help='revision', type=convert2hex) + parser.add_argument('-p', '--progif', help='proginf', type=convert2hex) + parser.add_argument('slot') + parser.add_argument('device_class', type=convert2hex) + parser.add_argument('vendor_id', type=convert2hex) + parser.add_argument('device_id', type=convert2hex) + parser.add_argument('sub_vendor_id', type=convert2hex) + parser.add_argument('sub_device_id', type=convert2hex) + parser.add_argument('other', nargs='*', default=[]) + + devices = [] + for line in subprocess.check_output(['lspci', '-nm'], universal_newlines=True).splitlines(): + args = parser.parse_args(args=shlex.split(line)) + devices.append(vars(args)) + return devices + + +def main(): + arg_specs = {} + module = AnsibleModule(argument_spec=arg_specs, supports_check_mode=True,) + try: + pci_devices = parse_lspci_data() + except: + module.fail_json(msg="Something fatal happened") + data = {'pci_devices': pci_devices} + module.exit_json(changed=False, ansible_facts=data, msg=data) + +if __name__ == '__main__': + main()