Skip to content
Snippets Groups Projects
Commit 31ee4f9b authored by Thomas Woerner's avatar Thomas Woerner
Browse files

group: Use IPAAnsibleModule class

ipaadmin_variables are handled by IPAAnsibleModule,
ansible_module.params_get is used to get the parameters and
ansible_module.ipa_connect is used to simplify the module.
parent 7318302f
No related branches found
No related tags found
No related merge requests found
...@@ -31,13 +31,9 @@ DOCUMENTATION = """ ...@@ -31,13 +31,9 @@ DOCUMENTATION = """
module: ipagroup module: ipagroup
short description: Manage FreeIPA groups short description: Manage FreeIPA groups
description: Manage FreeIPA groups description: Manage FreeIPA groups
extends_documentation_fragment:
- ipamodule_base_docs
options: options:
ipaadmin_principal:
description: The admin principal
default: admin
ipaadmin_password:
description: The admin password
required: false
name: name:
description: The group name description: The group name
required: false required: false
...@@ -182,10 +178,8 @@ EXAMPLES = """ ...@@ -182,10 +178,8 @@ EXAMPLES = """
RETURN = """ RETURN = """
""" """
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ansible_freeipa_module import \
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \ IPAAnsibleModule, compare_args_ipa, gen_add_del_lists, \
temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa, \
api_check_param, module_params_get, gen_add_del_lists, api_check_command, \
gen_add_list, gen_intersection_list gen_add_list, gen_intersection_list
...@@ -195,7 +189,7 @@ def find_group(module, name): ...@@ -195,7 +189,7 @@ def find_group(module, name):
"cn": name, "cn": name,
} }
_result = api_command(module, "group_find", name, _args) _result = module.ipa_command("group_find", name, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
...@@ -278,12 +272,9 @@ def should_modify_group(module, res_find, args, nonposix, posix, external): ...@@ -278,12 +272,9 @@ def should_modify_group(module, res_find, args, nonposix, posix, external):
def main(): def main():
ansible_module = AnsibleModule( ansible_module = IPAAnsibleModule(
argument_spec=dict( argument_spec=dict(
# general # general
ipaadmin_principal=dict(type="str", default="admin"),
ipaadmin_password=dict(type="str", required=False, no_log=True),
name=dict(type="list", aliases=["cn"], default=None, name=dict(type="list", aliases=["cn"], default=None,
required=True), required=True),
# present # present
...@@ -319,31 +310,24 @@ def main(): ...@@ -319,31 +310,24 @@ def main():
# Get parameters # Get parameters
# general # general
ipaadmin_principal = module_params_get( names = ansible_module.params_get("name")
ansible_module,
"ipaadmin_principal",
)
ipaadmin_password = module_params_get(ansible_module, "ipaadmin_password")
names = module_params_get(ansible_module, "name")
# present # present
description = module_params_get(ansible_module, "description") description = ansible_module.params_get("description")
gid = module_params_get(ansible_module, "gid") gid = ansible_module.params_get("gid")
nonposix = module_params_get(ansible_module, "nonposix") nonposix = ansible_module.params_get("nonposix")
external = module_params_get(ansible_module, "external") external = ansible_module.params_get("external")
posix = module_params_get(ansible_module, "posix") posix = ansible_module.params_get("posix")
nomembers = module_params_get(ansible_module, "nomembers") nomembers = ansible_module.params_get("nomembers")
user = module_params_get(ansible_module, "user") user = ansible_module.params_get("user")
group = module_params_get(ansible_module, "group") group = ansible_module.params_get("group")
service = module_params_get(ansible_module, "service") service = ansible_module.params_get("service")
membermanager_user = module_params_get(ansible_module, membermanager_user = ansible_module.params_get("membermanager_user")
"membermanager_user") membermanager_group = ansible_module.params_get("membermanager_group")
membermanager_group = module_params_get(ansible_module, externalmember = ansible_module.params_get("externalmember")
"membermanager_group") action = ansible_module.params_get("action")
externalmember = module_params_get(ansible_module, "externalmember")
action = module_params_get(ansible_module, "action")
# state # state
state = module_params_get(ansible_module, "state") state = ansible_module.params_get("state")
# Check parameters # Check parameters
...@@ -378,21 +362,19 @@ def main(): ...@@ -378,21 +362,19 @@ def main():
changed = False changed = False
exit_args = {} exit_args = {}
ccache_dir = None
ccache_name = None
try:
if not valid_creds(ansible_module, ipaadmin_principal):
ccache_dir, ccache_name = temp_kinit(ipaadmin_principal,
ipaadmin_password)
api_connect()
has_add_member_service = api_check_param("group_add_member", "service") # Connect to IPA API
with ansible_module.ipa_connect():
has_add_member_service = ansible_module.ipa_command_param_exists(
"group_add_member", "service")
if service is not None and not has_add_member_service: if service is not None and not has_add_member_service:
ansible_module.fail_json( ansible_module.fail_json(
msg="Managing a service as part of a group is not supported " msg="Managing a service as part of a group is not supported "
"by your IPA version") "by your IPA version")
has_add_membermanager = api_check_command("group_add_member_manager") has_add_membermanager = ansible_module.ipa_command_exists(
"group_add_member_manager")
if ((membermanager_user is not None or if ((membermanager_user is not None or
membermanager_group is not None) and not has_add_membermanager): membermanager_group is not None) and not has_add_membermanager):
ansible_module.fail_json( ansible_module.fail_json(
...@@ -684,8 +666,7 @@ def main(): ...@@ -684,8 +666,7 @@ def main():
for name, command, args in commands: for name, command, args in commands:
try: try:
result = api_command(ansible_module, command, name, result = ansible_module.ipa_command(command, name, args)
args)
if "completed" in result: if "completed" in result:
if result["completed"] > 0: if result["completed"] > 0:
changed = True changed = True
...@@ -706,12 +687,6 @@ def main(): ...@@ -706,12 +687,6 @@ def main():
if len(errors) > 0: if len(errors) > 0:
ansible_module.fail_json(msg=", ".join(errors)) ansible_module.fail_json(msg=", ".join(errors))
except Exception as e:
ansible_module.fail_json(msg=str(e))
finally:
temp_kdestroy(ccache_dir, ccache_name)
# Done # Done
ansible_module.exit_json(changed=changed, **exit_args) ansible_module.exit_json(changed=changed, **exit_args)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment