Skip to content

Commit 6ac60e4

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

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

library/ceph_config_key.py

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

0 commit comments

Comments
 (0)