Skip to content

Commit 080a392

Browse files
committed
feat: add support for ceph config-key
1 parent 7452074 commit 080a392

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

library/ceph_config_key.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from __future__ import absolute_import, division, print_function
2+
from typing import Any, Dict, List, Tuple, Union
3+
__metaclass__ = type
4+
5+
from ansible.module_utils.basic import AnsibleModule # type: ignore
6+
try:
7+
from ansible.module_utils.ceph_common import exit_module, build_base_cmd_shell, fatal # type: ignore
8+
except ImportError:
9+
from module_utils.ceph_common import exit_module, build_base_cmd_shell, fatal # type: ignore
10+
11+
import datetime
12+
import json
13+
14+
ANSIBLE_METADATA = {
15+
'metadata_version': '1.1',
16+
'status': ['preview'],
17+
'supported_by': 'community'
18+
}
19+
20+
DOCUMENTATION = '''
21+
---
22+
module: ceph_config_key
23+
short_description: get/set ceph config-key
24+
version_added: ""
25+
description:
26+
- Set Ceph config key options.
27+
options:
28+
fsid:
29+
type: str
30+
description:
31+
- the fsid of the Ceph cluster to interact with.
32+
required: false
33+
image:
34+
type: str
35+
description:
36+
- The Ceph container image to use.
37+
required: false
38+
action:
39+
type: str
40+
description:
41+
- whether to get or set the parameter specified in 'option'
42+
required: false
43+
default: 'set'
44+
option:
45+
type: str
46+
description:
47+
- name of the parameter to be set
48+
required: true
49+
value:
50+
type: str
51+
description:
52+
- value of the parameter
53+
required: true if action is 'set'
54+
55+
'''
56+
57+
EXAMPLES = '''
58+
- name: get config/mgr/mgr/prometheus/scrape_interval
59+
ceph_config_key:
60+
action: get
61+
option: config/mgr/mgr/prometheus/scrape_interval
62+
63+
- name: set config/mgr/mgr/prometheus/scrape_interval to 15s
64+
ceph_config_key:
65+
action: set
66+
option: config/mgr/mgr/prometheus/scrape_interval
67+
value: 15
68+
69+
- name: set mgr/cephadm/services/prometheus/prometheus.yml from file
70+
ceph_config_key:
71+
action: set
72+
option: mgr/cephadm/services/prometheus/prometheus.yml
73+
value: '{{lookup("file","/path/to/prometheus.yml")}}'
74+
'''
75+
76+
RETURN = '''# '''
77+
78+
79+
def set_configkey_value(module: "AnsibleModule",
80+
option: str,
81+
value: str) -> Tuple[int, List[str], str, str]:
82+
cmd = build_base_cmd_shell(module)
83+
cmd.extend(['ceph', 'config-key', 'set', option, '-i', '-'])
84+
85+
# pass the value through stdin to avoid issues with multi-line values, encoding, etc.
86+
rc, out, err = module.run_command(args=cmd, data=value)
87+
88+
return rc, cmd, out.strip(), err
89+
90+
91+
def get_configkey_dump(module: "AnsibleModule") -> Tuple[int, List[str], str, str]:
92+
cmd = build_base_cmd_shell(module)
93+
cmd.extend(['ceph', 'config-key', 'dump', '--format', 'json'])
94+
rc, out, err = module.run_command(cmd)
95+
if rc:
96+
fatal(message=f"Can't get current configuration via `ceph config-key dump`.Error:\n{err}", module=module)
97+
out = out.strip()
98+
return rc, cmd, out, err
99+
100+
101+
def get_configkey_current_value(option: str, config_dump: Dict[str, Any]) -> Union[str, None]:
102+
for key in config_dump:
103+
if key == option:
104+
return config_dump[key]
105+
return None
106+
107+
108+
def main() -> None:
109+
module = AnsibleModule(
110+
argument_spec=dict(
111+
action=dict(type='str', required=False, choices=['get', 'set'], default='set'),
112+
option=dict(type='str', required=False),
113+
value=dict(type='str', required=False),
114+
fsid=dict(type='str', required=False),
115+
image=dict(type='str', required=False)
116+
),
117+
supports_check_mode=True,
118+
required_if=[['action', 'set', ['value']]]
119+
)
120+
121+
# Gather module parameters in variables
122+
option = module.params.get('option')
123+
value = module.params.get('value')
124+
action = module.params.get('action')
125+
126+
startd = datetime.datetime.now()
127+
changed = False
128+
129+
# getting the value via a dump simplifies checking whether the value is currently set
130+
rc, cmd, dumpout, err = get_configkey_dump(module)
131+
config_dump = json.loads(dumpout)
132+
current_value = get_configkey_current_value(option, config_dump)
133+
diff = None
134+
out = ''
135+
136+
if action == 'set':
137+
if value is None:
138+
module.fail_json(msg="Value is required when action is set")
139+
elif value == current_value:
140+
out = 'option={} already set. Skipping.'.format(option)
141+
else:
142+
changed = True
143+
diff = dict(before=current_value, after=value)
144+
if not module.check_mode:
145+
rc, cmd, out, err = set_configkey_value(module, option, value)
146+
else:
147+
if current_value is None:
148+
out = ''
149+
err = 'No value found for option={}'.format(option)
150+
else:
151+
out = current_value
152+
153+
exit_module(module=module, out=out, rc=rc,
154+
cmd=cmd, err=err, startd=startd,
155+
changed=changed, diff=diff)
156+
157+
158+
if __name__ == '__main__':
159+
main()

0 commit comments

Comments
 (0)