Skip to content

Commit

Permalink
library: fix a bug in ceph_config module
Browse files Browse the repository at this point in the history
`ceph config get` doesn't allow getting value when entity is 'global' so
it makes the module fail. Indeed, the module first tries to get the
current value of the option for idempotency concern.
The idea is to check in `ceph config dump` instead.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2188319

Signed-off-by: Guillaume Abrioux <gabrioux@ibm.com>
(cherry picked from commit b28fe12)
  • Loading branch information
guits committed Apr 27, 2023
1 parent d3b87c4 commit 87b4a71
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions library/ceph_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from module_utils.ceph_common import exit_module, build_base_cmd_shell, fatal # type: ignore

import datetime
import json

ANSIBLE_METADATA = {
'metadata_version': '1.1',
Expand Down Expand Up @@ -84,22 +85,35 @@
RETURN = '''# '''


def get_or_set_option(module: "AnsibleModule",
action: str,
who: str,
option: str,
value: str) -> Tuple[int, List[str], str, str]:
def set_option(module: "AnsibleModule",
who: str,
option: str,
value: str) -> Tuple[int, List[str], str, str]:
cmd = build_base_cmd_shell(module)
cmd.extend(['ceph', 'config', action, who, option])

if action == 'set':
cmd.append(value)
cmd.extend(['ceph', 'config', 'set', who, option, value])

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

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


def get_config_dump(module: "AnsibleModule"):
cmd = build_base_cmd_shell(module)
cmd.extend(['ceph', 'config', 'dump', '--format', 'json'])
rc, out, err = module.run_command(cmd)
if rc:
fatal(message=f"Can't get current configuration via `ceph config dump`.Error:\n{err}", module=module)
out = out.strip()
return rc, cmd, out, err


def get_current_value(who, option, config_dump):
for config in config_dump:
if config['section'] == who and config['name'] == option:
return config['value']
return None


def main() -> None:
module = AnsibleModule(
argument_spec=dict(
Expand Down Expand Up @@ -135,16 +149,22 @@ def main() -> None:
startd = datetime.datetime.now()
changed = False

rc, cmd, out, err = get_or_set_option(module, 'get', who, option, value)
if rc:
fatal(message=f"Can't get current value. who={who} option={option}", module=module)
rc, cmd, out, err = get_config_dump(module)
config_dump = json.loads(out)
current_value = get_current_value(who, option, config_dump)

if action == 'set':
if value.lower() == out:
if value.lower() == current_value:
out = 'who={} option={} value={} already set. Skipping.'.format(who, option, value)
else:
rc, cmd, out, err = get_or_set_option(module, action, who, option, value)
rc, cmd, out, err = set_option(module, who, option, value)
changed = True
else:
if current_value is None:
out = ''
err = 'No value found for who={} option={}'.format(who, option)
else:
out = current_value

exit_module(module=module, out=out, rc=rc,
cmd=cmd, err=err, startd=startd,
Expand Down

0 comments on commit 87b4a71

Please sign in to comment.