|
13 | 13 | from module_utils.ceph_common import exit_module, build_base_cmd_shell, fatal # type: ignore
|
14 | 14 |
|
15 | 15 | import datetime
|
| 16 | +import json |
16 | 17 |
|
17 | 18 | ANSIBLE_METADATA = {
|
18 | 19 | 'metadata_version': '1.1',
|
|
84 | 85 | RETURN = '''# '''
|
85 | 86 |
|
86 | 87 |
|
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]: |
92 | 92 | 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]) |
97 | 94 |
|
98 | 95 | rc, out, err = module.run_command(cmd)
|
99 | 96 |
|
100 | 97 | return rc, cmd, out.strip(), err
|
101 | 98 |
|
102 | 99 |
|
| 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 | + |
103 | 117 | def main() -> None:
|
104 | 118 | module = AnsibleModule(
|
105 | 119 | argument_spec=dict(
|
@@ -135,16 +149,22 @@ def main() -> None:
|
135 | 149 | startd = datetime.datetime.now()
|
136 | 150 | changed = False
|
137 | 151 |
|
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) |
141 | 155 |
|
142 | 156 | if action == 'set':
|
143 |
| - if value.lower() == out: |
| 157 | + if value.lower() == current_value: |
144 | 158 | out = 'who={} option={} value={} already set. Skipping.'.format(who, option, value)
|
145 | 159 | 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) |
147 | 161 | 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 |
148 | 168 |
|
149 | 169 | exit_module(module=module, out=out, rc=rc,
|
150 | 170 | cmd=cmd, err=err, startd=startd,
|
|
0 commit comments