/
kconfig.py
174 lines (143 loc) · 5.04 KB
/
kconfig.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from typing import List, Optional
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = r"""
module: kconfig
author:
- Jake Howard
short_description: Manage KDE's configuration through kconfig
description:
- This module allows modification and reading of KDE's C(kconfig) configuration.
- This module works by wrapping the C(kreadconfig5) and C(kwriteconfig5) CLI tools,
which need to be on your C($PATH). These should come with any KDE install.
notes:
- KDE sadly doesn't easily expose which keys and groups respond to which settings,
so it will require some work to diff the C(~/config) directory yourself.
options:
file:
type: str
required: false
description:
- The filename to read / write configuration from.
group:
type: list
required: true
description:
- The group in the given I(file) to write into.
- For nested groups, provide multiple values
key:
type: str
required: true
description:
- The key in the I(group) which is modified or read.
value:
type: str
required: false
description:
- The value to set for the specified key.
- Required for I(state=present).
state:
type: str
required: false
default: present
choices: [ 'read', 'present' ]
description:
- The action to take upon the key/value.
"""
RETURN = r"""
value:
description: value associated with the requested key
returned: success, state was "read"
type: str
"""
class KConfigWrapper:
def __init__(self, module: AnsibleModule):
self.module = module
self.check_mode = module.check_mode
self.write_config_bin = self.module.get_bin_path(
"kwriteconfig6", required=False
)
self.read_config_bin = self.module.get_bin_path("kreadconfig6", required=False)
if self.write_config_bin is None or self.read_config_bin is None:
self.write_config_bin = self.module.get_bin_path(
"kwriteconfig5", required=True
)
self.read_config_bin = self.module.get_bin_path(
"kreadconfig5", required=True
)
def read(self, file: Optional[str], groups: List[str], key: str) -> Optional[str]:
command = [self.read_config_bin, "--key", key]
for group in groups:
command.extend(["--group", group])
if file is not None:
command.extend(["--file", file])
rc, out, err = self.module.run_command(command)
if rc != 0:
self.module.fail_json(
msg="kreadconfig5 failed while reading the value with error: %s" % err,
out=out,
err=err,
)
if out == "":
value = None
else:
value = out.rstrip("\n")
return value
def write(
self, file: Optional[str], groups: List[str], key: str, value: str
) -> bool:
# If no change is needed (or won't be done due to check_mode), notify
# caller straight away.
if value == self.read(file, groups, key):
return False
elif self.check_mode:
return True
command = [self.write_config_bin, "--key", key]
for group in groups:
command.extend(["--group", group])
if file is not None:
command.extend(["--file", file])
# The value must always be the last argument
command.append(value)
# Run the command and fetch standard return code, stdout, and stderr.
rc, out, err = self.module.run_command(command)
if rc != 0:
self.module.fail_json(
msg="kwriteconfig5 failed while write the value with error: %s" % err,
out=out,
err=err,
)
return True
def main() -> None:
module = AnsibleModule(
argument_spec={
"state": {"default": "present", "choices": ["present", "read"]},
"key": {
"required": True,
"type": "str",
},
"group": {
"required": True,
"type": "list",
},
"value": {"required": False, "type": "str", "default": None},
"file": {"required": False, "type": "str", "default": None},
},
supports_check_mode=True,
)
params = module.params
if not params["group"]:
module.fail_json(msg="Must provide a group.")
# If present state was specified, value must be provided.
if params["state"] == "present" and params["value"] is None:
module.fail_json(msg='State "present" requires "value" to be set.')
kconfig = KConfigWrapper(module)
if params["state"] == "read":
value = kconfig.read(params["file"], params["group"], params["key"])
module.exit_json(changed=False, value=value)
elif params["state"] == "present":
changed = kconfig.write(
params["file"], params["group"], params["key"], params["value"]
)
module.exit_json(changed=changed)
if __name__ == "__main__":
main()