Skip to content

Commit 6baf7b1

Browse files
committed
modules: Add _info modules
Validate modules test fails with: Argument state includes get, list or info as a choice. Functionality should be in an _info or (if further conditions apply) _facts module. on modules ceph_crush_rule and ceph_key With Ansible 2.8 and onwards best practice requires that modules gathering info to be put in a separate module named _info[1] [1]https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#creating-an-info-or-a-facts-module Signed-off-by: Teoman ONAY <[email protected]>
1 parent 3814c2e commit 6baf7b1

File tree

21 files changed

+421
-64
lines changed

21 files changed

+421
-64
lines changed

infrastructure-playbooks/ceph-keys.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
with_items: "{{ keys_to_delete }}"
5151

5252
- name: Info ceph key(s)
53-
ceph_key:
53+
ceph_key_info:
5454
name: "{{ item }}"
5555
state: info
5656
cluster: "{{ cluster }}"
@@ -60,7 +60,7 @@
6060
with_items: "{{ keys_to_info }}"
6161

6262
- name: List ceph key(s)
63-
ceph_key:
63+
ceph_key_info:
6464
state: list
6565
cluster: "{{ cluster }}"
6666
containerized: "{{ container_exec_cmd | default(False) }}"

infrastructure-playbooks/cephadm-adopt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@
382382
inventory_hostname in groups.get(rbdmirror_group_name, [])
383383

384384
- name: Get the client.admin keyring
385-
ceph_key:
385+
ceph_key_info:
386386
name: client.admin
387387
cluster: "{{ cluster }}"
388388
output_format: plain

library/ceph_crush_rule.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@
5959
If 'present' is used, the module creates a rule if it doesn't
6060
exist or update it if it already exists.
6161
If 'absent' is used, the module will simply delete the rule.
62-
If 'info' is used, the module will return all details about the
63-
existing rule (json formatted).
6462
required: false
65-
choices: ['present', 'absent', 'info']
63+
choices: ['present', 'absent']
6664
default: present
6765
rule_type:
6866
description:
@@ -192,7 +190,7 @@ def main():
192190
argument_spec=dict(
193191
name=dict(type='str', required=False),
194192
cluster=dict(type='str', required=False, default='ceph'),
195-
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501
193+
state=dict(type='str', required=False, choices=['present', 'absent'], default='present'), # noqa: E501
196194
rule_type=dict(type='str', required=False, choices=['replicated', 'erasure']), # noqa: E501
197195
bucket_root=dict(type='str', required=False),
198196
bucket_type=dict(type='str', required=False, choices=['osd', 'host', 'chassis', 'rack', 'row', 'pdu', 'pod', # noqa: E501

library/ceph_crush_rule_info.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Copyright 2020, Red Hat, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import absolute_import, division, print_function
16+
__metaclass__ = type
17+
18+
from ansible.module_utils.basic import AnsibleModule
19+
try:
20+
from ansible.module_utils.ca_common import exit_module, \
21+
generate_cmd, \
22+
is_containerized, \
23+
exec_command
24+
except ImportError:
25+
from module_utils.ca_common import exit_module, \
26+
generate_cmd, \
27+
is_containerized, \
28+
exec_command
29+
import datetime
30+
31+
32+
ANSIBLE_METADATA = {
33+
'metadata_version': '1.1',
34+
'status': ['preview'],
35+
'supported_by': 'community'
36+
}
37+
38+
DOCUMENTATION = '''
39+
---
40+
module: ceph_crush_rule_info
41+
short_description: Lists Ceph Crush Replicated/Erasure Rules
42+
version_added: "2.8"
43+
description:
44+
- Retrieces Ceph Crush rule(s).
45+
options:
46+
name:
47+
description:
48+
- name of the Ceph Crush rule. If state is 'info' - empty string
49+
can be provided as a value to get all crush rules
50+
required: true
51+
cluster:
52+
description:
53+
- The ceph cluster name.
54+
required: false
55+
default: ceph
56+
author:
57+
- Teoman ONAY <@asm0deuz>
58+
'''
59+
60+
EXAMPLES = '''
61+
- name: get a Ceph Crush rule information
62+
ceph_crush_rule:
63+
name: foo
64+
state: info
65+
'''
66+
67+
RETURN = '''# '''
68+
69+
70+
def get_rule(module, container_image=None):
71+
'''
72+
Get existing crush rule
73+
'''
74+
75+
cluster = module.params.get('cluster')
76+
name = module.params.get('name')
77+
78+
args = ['dump', name, '--format=json']
79+
80+
cmd = generate_cmd(sub_cmd=['osd', 'crush', 'rule'],
81+
args=args,
82+
cluster=cluster,
83+
container_image=container_image)
84+
85+
return cmd
86+
87+
88+
def main():
89+
module = AnsibleModule(
90+
argument_spec=dict(
91+
name=dict(type='str', required=False),
92+
cluster=dict(type='str', required=False, default='ceph'),
93+
),
94+
supports_check_mode=True,
95+
)
96+
97+
if module.check_mode:
98+
module.exit_json(
99+
changed=False,
100+
stdout='',
101+
stderr='',
102+
rc=0,
103+
start='',
104+
end='',
105+
delta='',
106+
)
107+
108+
startd = datetime.datetime.now()
109+
changed = False
110+
111+
# will return either the image name or None
112+
container_image = is_containerized()
113+
114+
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
115+
116+
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501
117+
118+
119+
if __name__ == '__main__':
120+
main()

library/ceph_key.py

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
from ansible.module_utils.basic import AnsibleModule
1919
try:
2020
from ansible.module_utils.ca_common import generate_cmd, \
21-
is_containerized, \
22-
container_exec, \
23-
fatal
21+
is_containerized, \
22+
container_exec, \
23+
fatal
2424
except ImportError:
2525
from module_utils.ca_common import generate_cmd, \
26-
is_containerized, \
27-
container_exec, \
28-
fatal
26+
is_containerized, \
27+
container_exec, \
28+
fatal
2929
import datetime
3030
import json
3131
import os
@@ -85,11 +85,9 @@
8585
If 'absent' is used, the module will simply delete the keyring.
8686
If 'list' is used, the module will list all the keys and will
8787
return a json output.
88-
If 'info' is used, the module will return in a json format the
89-
description of a given keyring.
9088
If 'generate_secret' is used, the module will simply output a cephx keyring.
9189
required: false
92-
choices: ['present', 'update', 'absent', 'list', 'info', 'fetch_initial_keys', 'generate_secret']
90+
choices: ['present', 'update', 'absent', 'fetch_initial_keys', 'generate_secret']
9391
default: present
9492
caps:
9593
description:
@@ -182,22 +180,6 @@
182180
name: "my_key"
183181
state: absent
184182
185-
- name: info cephx key
186-
ceph_key:
187-
name: "my_key""
188-
state: info
189-
190-
- name: info cephx admin key (plain)
191-
ceph_key:
192-
name: client.admin
193-
output_format: plain
194-
state: info
195-
register: client_admin_key
196-
197-
- name: list cephx keys
198-
ceph_key:
199-
state: list
200-
201183
- name: fetch cephx keys
202184
ceph_key:
203185
state: fetch_initial_keys
@@ -487,7 +469,7 @@ def run_module():
487469
cluster=dict(type='str', required=False, default='ceph'),
488470
name=dict(type='str', required=False),
489471
state=dict(type='str', required=False, default='present', choices=['present', 'update', 'absent', # noqa: E501
490-
'list', 'info', 'fetch_initial_keys', 'generate_secret']), # noqa: E501
472+
'fetch_initial_keys', 'generate_secret']), # noqa: E501
491473
caps=dict(type='dict', required=False, default=None),
492474
secret=dict(type='str', required=False, default=None, no_log=True),
493475
import_key=dict(type='bool', required=False, default=True),
@@ -518,7 +500,7 @@ def run_module():
518500
output_format = module.params.get('output_format')
519501

520502
# Can't use required_if with 'name' for some reason...
521-
if state in ['present', 'absent', 'update', 'info'] and not name:
503+
if state in ['present', 'absent', 'update'] and not name:
522504
fatal(f'"state" is "{state}" but "name" is not defined.', module)
523505

524506
changed = False
@@ -626,14 +608,6 @@ def run_module():
626608
else:
627609
rc = 0
628610

629-
elif state == "info":
630-
rc, cmd, out, err = exec_commands(
631-
module, info_key(cluster, name, user, user_key_path, output_format, container_image)) # noqa: E501
632-
633-
elif state == "list":
634-
rc, cmd, out, err = exec_commands(
635-
module, list_keys(cluster, user, user_key_path, container_image))
636-
637611
elif state == "fetch_initial_keys":
638612
hostname = socket.gethostname().split('.', 1)[0]
639613
user = "mon."

0 commit comments

Comments
 (0)