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

pwpolicy: 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 7ff49265
No related branches found
No related tags found
No related merge requests found
...@@ -111,19 +111,17 @@ EXAMPLES = """ ...@@ -111,19 +111,17 @@ EXAMPLES = """
RETURN = """ RETURN = """
""" """
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ansible_freeipa_module import \
from ansible.module_utils._text import to_text IPAAnsibleModule, compare_args_ipa
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa
def find_pwpolicy(module, name): def find_pwpolicy(module, name):
_args = { _args = {
"all": True, "all": True,
"cn": to_text(name), "cn": name,
} }
_result = api_command(module, "pwpolicy_find", to_text(name), _args) _result = module.ipa_command("pwpolicy_find", name, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
...@@ -160,12 +158,9 @@ def gen_args(maxlife, minlife, history, minclasses, minlength, priority, ...@@ -160,12 +158,9 @@ def gen_args(maxlife, minlife, history, minclasses, minlength, priority,
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=False), required=False),
# present # present
...@@ -198,28 +193,26 @@ def main(): ...@@ -198,28 +193,26 @@ def main():
# Get parameters # Get parameters
# general # general
ipaadmin_principal = ansible_module.params.get("ipaadmin_principal") names = ansible_module.params_get("name")
ipaadmin_password = ansible_module.params.get("ipaadmin_password")
names = ansible_module.params.get("name")
# present # present
maxlife = ansible_module.params.get("maxlife") maxlife = ansible_module.params_get("maxlife")
minlife = ansible_module.params.get("minlife") minlife = ansible_module.params_get("minlife")
history = ansible_module.params.get("history") history = ansible_module.params_get("history")
minclasses = ansible_module.params.get("minclasses") minclasses = ansible_module.params_get("minclasses")
minlength = ansible_module.params.get("minlength") minlength = ansible_module.params_get("minlength")
priority = ansible_module.params.get("priority") priority = ansible_module.params_get("priority")
maxfail = ansible_module.params.get("maxfail") maxfail = ansible_module.params_get("maxfail")
failinterval = ansible_module.params.get("failinterval") failinterval = ansible_module.params_get("failinterval")
lockouttime = ansible_module.params.get("lockouttime") lockouttime = ansible_module.params_get("lockouttime")
# state # state
state = ansible_module.params.get("state") state = ansible_module.params_get("state")
# Check parameters # Check parameters
if names is None: if names is None:
names = ["global_policy"] names = [u"global_policy"]
if state == "present": if state == "present":
if len(names) != 1: if len(names) != 1:
...@@ -245,13 +238,8 @@ def main(): ...@@ -245,13 +238,8 @@ def main():
changed = False changed = False
exit_args = {} exit_args = {}
ccache_dir = None
ccache_name = None with ansible_module.ipa_connect():
try:
if not valid_creds(ansible_module, ipaadmin_principal):
ccache_dir, ccache_name = temp_kinit(ipaadmin_principal,
ipaadmin_password)
api_connect()
commands = [] commands = []
...@@ -292,18 +280,12 @@ def main(): ...@@ -292,18 +280,12 @@ def main():
for name, command, args in commands: for name, command, args in commands:
try: try:
api_command(ansible_module, command, to_text(name), args) ansible_module.ipa_command(command, name, args)
changed = True changed = True
except Exception as e: except Exception as e:
ansible_module.fail_json(msg="%s: %s: %s" % (command, name, ansible_module.fail_json(msg="%s: %s: %s" % (command, name,
str(e))) str(e)))
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