hbacsvc: 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:59:39 +02:00
parent f87520d90a
commit 7d2bdd7138

View File

@@ -31,13 +31,9 @@ DOCUMENTATION = """
module: ipahbacsvc
short description: Manage FreeIPA HBAC Services
description: Manage FreeIPA HBAC Services
extends_documentation_fragment:
- ipamodule_base_docs
options:
ipaadmin_principal:
description: The admin principal
default: admin
ipaadmin_password:
description: The admin password
required: false
name:
description: The group name
required: false
@@ -70,19 +66,17 @@ EXAMPLES = """
RETURN = """
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa
from ansible.module_utils.ansible_freeipa_module import \
IPAAnsibleModule, compare_args_ipa
def find_hbacsvc(module, name):
_args = {
"all": True,
"cn": to_text(name),
"cn": name,
}
_result = api_command(module, "hbacsvc_find", to_text(name), _args)
_result = module.ipa_command("hbacsvc_find", name, _args)
if len(_result["result"]) > 1:
module.fail_json(
@@ -96,18 +90,15 @@ def find_hbacsvc(module, name):
def gen_args(description):
_args = {}
if description is not None:
_args["description"] = to_text(description)
_args["description"] = description
return _args
def main():
ansible_module = AnsibleModule(
ansible_module = IPAAnsibleModule(
argument_spec=dict(
# general
ipaadmin_principal=dict(type="str", default="admin"),
ipaadmin_password=dict(type="str", required=False, no_log=True),
name=dict(type="list", aliases=["cn", "service"], default=None,
required=True),
# present
@@ -126,15 +117,13 @@ def main():
# Get parameters
# general
ipaadmin_principal = ansible_module.params.get("ipaadmin_principal")
ipaadmin_password = ansible_module.params.get("ipaadmin_password")
names = ansible_module.params.get("name")
names = ansible_module.params_get("name")
# present
description = ansible_module.params.get("description")
description = ansible_module.params_get("description")
# state
state = ansible_module.params.get("state")
state = ansible_module.params_get("state")
# Check parameters
@@ -158,13 +147,9 @@ def main():
changed = False
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()
# Connect to IPA API
with ansible_module.ipa_connect():
commands = []
@@ -203,18 +188,12 @@ def main():
for name, command, args in commands:
try:
api_command(ansible_module, command, to_text(name), args)
ansible_module.ipa_command(command, name, args)
changed = True
except Exception as e:
ansible_module.fail_json(msg="%s: %s: %s" % (command, name,
str(e)))
except Exception as e:
ansible_module.fail_json(msg=str(e))
finally:
temp_kdestroy(ccache_dir, ccache_name)
# Done
ansible_module.exit_json(changed=changed, **exit_args)