Skip to content

Commit a1d6266

Browse files
authored
Merge pull request #222 from ceph/mergify/bp/pacific/pr-218
library: fix a bug in ceph_config module (backport #218)
2 parents d3b87c4 + 87b4a71 commit a1d6266

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

library/ceph_config.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from module_utils.ceph_common import exit_module, build_base_cmd_shell, fatal # type: ignore
1414

1515
import datetime
16+
import json
1617

1718
ANSIBLE_METADATA = {
1819
'metadata_version': '1.1',
@@ -84,22 +85,35 @@
8485
RETURN = '''# '''
8586

8687

87-
def get_or_set_option(module: "AnsibleModule",
88-
action: str,
89-
who: str,
90-
option: str,
91-
value: str) -> Tuple[int, List[str], str, str]:
88+
def set_option(module: "AnsibleModule",
89+
who: str,
90+
option: str,
91+
value: str) -> Tuple[int, List[str], str, str]:
9292
cmd = build_base_cmd_shell(module)
93-
cmd.extend(['ceph', 'config', action, who, option])
94-
95-
if action == 'set':
96-
cmd.append(value)
93+
cmd.extend(['ceph', 'config', 'set', who, option, value])
9794

9895
rc, out, err = module.run_command(cmd)
9996

10097
return rc, cmd, out.strip(), err
10198

10299

100+
def get_config_dump(module: "AnsibleModule"):
101+
cmd = build_base_cmd_shell(module)
102+
cmd.extend(['ceph', 'config', 'dump', '--format', 'json'])
103+
rc, out, err = module.run_command(cmd)
104+
if rc:
105+
fatal(message=f"Can't get current configuration via `ceph config dump`.Error:\n{err}", module=module)
106+
out = out.strip()
107+
return rc, cmd, out, err
108+
109+
110+
def get_current_value(who, option, config_dump):
111+
for config in config_dump:
112+
if config['section'] == who and config['name'] == option:
113+
return config['value']
114+
return None
115+
116+
103117
def main() -> None:
104118
module = AnsibleModule(
105119
argument_spec=dict(
@@ -135,16 +149,22 @@ def main() -> None:
135149
startd = datetime.datetime.now()
136150
changed = False
137151

138-
rc, cmd, out, err = get_or_set_option(module, 'get', who, option, value)
139-
if rc:
140-
fatal(message=f"Can't get current value. who={who} option={option}", module=module)
152+
rc, cmd, out, err = get_config_dump(module)
153+
config_dump = json.loads(out)
154+
current_value = get_current_value(who, option, config_dump)
141155

142156
if action == 'set':
143-
if value.lower() == out:
157+
if value.lower() == current_value:
144158
out = 'who={} option={} value={} already set. Skipping.'.format(who, option, value)
145159
else:
146-
rc, cmd, out, err = get_or_set_option(module, action, who, option, value)
160+
rc, cmd, out, err = set_option(module, who, option, value)
147161
changed = True
162+
else:
163+
if current_value is None:
164+
out = ''
165+
err = 'No value found for who={} option={}'.format(who, option)
166+
else:
167+
out = current_value
148168

149169
exit_module(module=module, out=out, rc=rc,
150170
cmd=cmd, err=err, startd=startd,

0 commit comments

Comments
 (0)