Move local fact scripts to modules

This commit is contained in:
Alexander Grothe
2017-03-03 10:39:59 +01:00
parent 1dc6f1c00b
commit d6647e1613
12 changed files with 481 additions and 252 deletions

View File

@@ -5,5 +5,5 @@
apt:
name: vdr-plugin-dvbhddevice
when:
- '"13c2:300a" in ansible_local.hardware.pci'
- '"13c2:300b" in ansible_local.hardware.pci'
- '"13c2:300a" in pci'
- '"13c2:300b" in pci'

View File

@@ -5,4 +5,4 @@
apt:
name: vdr-plugin-dvbsddevice
when:
- '19c2:6a11" in ansible_local.modules'
- '19c2:6a11" in modules'

View File

@@ -5,5 +5,5 @@
apt:
name: vdr-plugin-imonlcd
when:
- '"15c2:0038" in ansible_local.hardware.usb'
- '"15c2:ffdc" in ansible_local.hardware.usb'
- '"15c2:0038" in usb'
- '"15c2:ffdc" in usb'

View File

@@ -5,4 +5,4 @@
apt:
name: libcec-daemon
when:
- '"2548:1002" in ansible_local.hardware.usb'
- '"2548:1002" in usb'

View File

@@ -5,4 +5,4 @@
apt:
name: vdr-plugin-pvr350
when:
- '19c2:6a11" in ansible_local.hardware.pci'
- '19c2:6a11" in pci'

View File

@@ -1,12 +1,7 @@
---
# file roles/autoinstall-satip/tasks/main.yml
- name: Display all variables/facts known for a host
debug:
var: ansible_local
verbosity: 1
- name: apt | install vdr-plugin-satip if a Sat>IP server has been detected
apt:
name: vdr-plugin-satip
when: ansible_local.satip.satip_detected
when: satip_detected

View File

@@ -5,4 +5,4 @@
apt:
name: vdr-plugin-targavfd
when:
- '"19c2:6a11" in ansible_local.hardware.usb'
- '"19c2:6a11" in usb'

View File

@@ -1,29 +0,0 @@
#!/usr/bin/env python3
# This script returns a list of Vendor- and Product-IDs for all connected usb
# and pci(e) devices in json format
import glob
import json
import os
import sys
import usb.core
from collections import namedtuple
Device = namedtuple("Device", ['idVendor', 'idProduct'])
def get_pci_devices():
for device in glob.glob('/sys/devices/pci*/*:*:*/'):
with open(os.path.join(device, 'device')) as d:
product_id = int(d.read().strip(), 16)
with open(os.path.join(device, 'vendor')) as d:
vendor_id = int(d.read().strip(), 16)
yield Device(idVendor=vendor_id, idProduct=product_id)
def format_device_list(iterator):
return ["{:04x}:{:04x}".format(d.idVendor, d.idProduct) for d in iterator]
if __name__ == '__main__':
usb_devices = format_device_list(usb.core.find(find_all=True))
pci_devices = format_device_list(get_pci_devices())
print(json.dumps({'usb': usb_devices, 'pci': pci_devices}))

View File

@@ -1,9 +0,0 @@
#!/usr/bin/env python2
# This script returns a list of currently loaded kernel modules
from __future__ import print_function
import json
import kmodpy
k = kmodpy.Kmod()
print(json.dumps([module[0] for module in k.loaded()]))

View File

@@ -1,43 +0,0 @@
#!/usr/bin/env python3
# This script sends a multicast message and awaits responses by Sat>IP servers.
# returns the boolean variable 'satip_detected' as json
import json
import socket
import sys
import time
SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900
# SSDP_MX = max delay for server response
# a value of 2s is recommended by the SAT>IP specification 1.2.2
SSDP_MX = 2
SSDP_ST = "urn:ses-com:device:SatIPServer:1"
ssdpRequest = "\r\n".join((
"M-SEARCH * HTTP/1.1",
"HOST: %s:%d" % (SSDP_ADDR, SSDP_PORT),
"MAN: \"ssdp:discover\"",
"MX: %d" % (SSDP_MX),
"ST: %s" % (SSDP_ST),
"\r\n"))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# according to Sat>IP Specification 1.2.2, p. 20
# a client should send three requests within 100 ms with a ttl of 2
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.settimeout(SSDP_MX + 0.5)
for _ in range(3):
sock.sendto(ssdpRequest.encode('ascii'), (SSDP_ADDR, SSDP_PORT))
time.sleep(0.03)
try:
response = sock.recv(1000).decode()
if response and "SERVER:" in response:
got_response = True
else:
raise ValueError('No satip server detected')
except (socket.timeout, ValueError):
got_response = False
finally:
print(json.dumps(
{'satip_detected': got_response}
))

View File

@@ -46,6 +46,7 @@
- linux-firmware
- psmisc
- python-kmodpy
- python-usb
- python3-usb
- software-properties-common
- ssh
@@ -55,28 +56,28 @@
- usbutils
- xfsprogs
- name: create directory for local facts
file:
dest: /etc/ansible/facts.d
state: directory
- name: get information about usb and pci hardware and loaded kernel modules
hardware_facts:
usb: True
pci: True
modules: True
gpus: True
- debug:
var: usb
verbosity: 1
- debug:
var: pci
verbosity: 1
- debug:
var: modules
verbosity: 1
- debug:
var: gpus
verbosity: 1
- name: copy facts script for USB- and PCI(e)-IDs
copy:
src: files/hardware.fact.py
dest: /etc/ansible/facts.d/hardware.fact
mode: '0775'
- name: "detect SAT>IP Server on the network"
action: satip_facts
- name: copy facts script for loaded modules
copy:
src: files/modules.fact.py
dest: /etc/ansible/facts.d/modules.fact
mode: '0775'
- name: copy facts script for Sat>IP server detection
copy:
src: files/satip.fact.py
dest: /etc/ansible/facts.d/satip.fact
mode: '0775'
- name: reload ansible local facts
setup: filter=ansible_local
- debug:
var: satip_detected
verbosity: 1