/
kernel_module.py
105 lines (86 loc) · 2.37 KB
/
kernel_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2017, Rudd-O. GPLv2+.
import errno
import re
import subprocess
DOCUMENTATION = """
---
module: module
author: Rudd-O
short_description: Load kernel modules now and on boot
description:
- This module will allow you configure kernel module load.
options:
name:
required: true
state:
required: false
choices: [ "loaded" ]
default: "loaded"
"""
EXAMPLES = r"""
- kernel_module:
name: uinput
state: loaded
"""
NOTHING_TO_DO = "Nothing to do"
def loaded(module, name):
changed = False
msg = NOTHING_TO_DO
present = False
with open('/proc/modules') as modules:
module_name = name.replace('-', '_') + ' '
for line in modules:
if line.startswith(module_name):
present = True
break
if not present:
if not module.check_mode:
p = subprocess.Popen(["modprobe", "--", name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = p.communicate()[0].strip()
ret = p.wait()
if ret != 0:
return module.fail_json(rc=ret, msg=out)
changed = True
msg = "Module %s loaded" % name
sanitized = re.sub(r'\W+', '', name)
sanitized = "/etc/modules-load.d/%s.conf" % sanitized
try:
f = file(sanitized)
text = f.read()
f.close()
except IOError as e:
if e.errno != errno.ENOENT:
raise
text = ""
if text != name:
if not module.check_mode:
f = file(sanitized, "wb")
f.write(name)
f.close()
os.chmod(sanitized, 0644)
changed = True
if msg != NOTHING_TO_DO:
msg = msg + ", %s created" % sanitized
else:
msg = "%s created" % sanitized
module.exit_json(changed=changed, msg=msg)
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
state=dict(default='loaded', choices=['loaded']),
),
supports_check_mode=True
)
params = module.params
name = params["name"]
if params["state"] == "loaded":
loaded(module, name)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.splitter import *
main()