hbacrule: 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.
This commit is contained in:
Thomas Woerner
2021-08-27 14:11:12 +02:00
parent 31ee4f9b69
commit f87520d90a

View File

@@ -31,13 +31,9 @@ DOCUMENTATION = """
module: ipahbacrule module: ipahbacrule
short description: Manage FreeIPA HBAC rules short description: Manage FreeIPA HBAC rules
description: Manage FreeIPA HBAC rules description: Manage FreeIPA HBAC rules
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 hbacrule name description: The hbacrule name
required: true required: true
@@ -156,11 +152,9 @@ 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, gen_add_list, \
temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa, \ gen_intersection_list, ensure_fqdn
module_params_get, gen_add_del_lists, gen_add_list, \
gen_intersection_list, api_get_domain, ensure_fqdn
def find_hbacrule(module, name): def find_hbacrule(module, name):
@@ -169,7 +163,7 @@ def find_hbacrule(module, name):
"cn": name, "cn": name,
} }
_result = api_command(module, "hbacrule_find", name, _args) _result = module.ipa_command("hbacrule_find", name, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
@@ -198,12 +192,9 @@ def gen_args(description, usercategory, hostcategory, servicecategory,
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
@@ -236,26 +227,23 @@ def main():
# Get parameters # Get parameters
# general # general
ipaadmin_principal = module_params_get(ansible_module, names = ansible_module.params_get("name")
"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")
usercategory = module_params_get(ansible_module, "usercategory") usercategory = ansible_module.params_get("usercategory")
hostcategory = module_params_get(ansible_module, "hostcategory") hostcategory = ansible_module.params_get("hostcategory")
servicecategory = module_params_get(ansible_module, "servicecategory") servicecategory = ansible_module.params_get("servicecategory")
nomembers = module_params_get(ansible_module, "nomembers") nomembers = ansible_module.params_get("nomembers")
host = module_params_get(ansible_module, "host") host = ansible_module.params_get("host")
hostgroup = module_params_get(ansible_module, "hostgroup") hostgroup = ansible_module.params_get("hostgroup")
hbacsvc = module_params_get(ansible_module, "hbacsvc") hbacsvc = ansible_module.params_get("hbacsvc")
hbacsvcgroup = module_params_get(ansible_module, "hbacsvcgroup") hbacsvcgroup = ansible_module.params_get("hbacsvcgroup")
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")
action = module_params_get(ansible_module, "action") action = ansible_module.params_get("action")
# state # state
state = module_params_get(ansible_module, "state") state = ansible_module.params_get("state")
# Check parameters # Check parameters
@@ -318,16 +306,12 @@ def main():
changed = False changed = False
exit_args = {} exit_args = {}
ccache_dir = None
ccache_name = None # Connect to IPA API
try: with ansible_module.ipa_connect():
if not valid_creds(ansible_module, ipaadmin_principal):
ccache_dir, ccache_name = temp_kinit(ipaadmin_principal,
ipaadmin_password)
api_connect()
# Get default domain # Get default domain
default_domain = api_get_domain() default_domain = ansible_module.ipa_get_domain()
# Ensure fqdn host names, use default domain for simple names # Ensure fqdn host names, use default domain for simple names
if host is not None: if host is not None:
@@ -620,8 +604,7 @@ def main():
errors = [] errors = []
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
@@ -642,12 +625,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)