user: 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 18:47:43 +02:00
parent c4fbd0d5fd
commit c2df7be22a

View File

@@ -31,13 +31,9 @@ DOCUMENTATION = """
module: ipauser module: ipauser
short description: Manage FreeIPA users short description: Manage FreeIPA users
description: Manage FreeIPA users description: Manage FreeIPA users
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 list of users (internally uid). description: The list of users (internally uid).
required: false required: false
@@ -472,16 +468,11 @@ user:
returned: always returned: always
""" """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text 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, date_format, \
temp_kdestroy, valid_creds, api_connect, api_command, date_format, \ encode_certificate, load_cert_from_str, DN_x500_text, to_text
compare_args_ipa, module_params_get, api_check_param, api_get_realm, \
api_command_no_name, gen_add_del_lists, encode_certificate, \
load_cert_from_str, DN_x500_text, api_check_command
import six import six
if six.PY3: if six.PY3:
unicode = str unicode = str
@@ -494,7 +485,7 @@ def find_user(module, name, preserved=False):
if preserved: if preserved:
_args["preserved"] = preserved _args["preserved"] = preserved
_result = api_command(module, "user_find", name, _args) _result = module.ipa_command("user_find", name, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
@@ -792,12 +783,9 @@ def main():
nomembers=dict(type='bool', default=None), nomembers=dict(type='bool', default=None),
) )
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=["login"], default=None, name=dict(type="list", aliases=["login"], default=None,
required=False), required=False),
users=dict(type="list", aliases=["login"], default=None, users=dict(type="list", aliases=["login"], default=None,
@@ -836,69 +824,65 @@ def main():
# Get parameters # Get parameters
# general # general
ipaadmin_principal = module_params_get(ansible_module, names = ansible_module.params_get("name")
"ipaadmin_principal") users = ansible_module.params_get("users")
ipaadmin_password = module_params_get(ansible_module, "ipaadmin_password")
names = module_params_get(ansible_module, "name")
users = module_params_get(ansible_module, "users")
# present # present
first = module_params_get(ansible_module, "first") first = ansible_module.params_get("first")
last = module_params_get(ansible_module, "last") last = ansible_module.params_get("last")
fullname = module_params_get(ansible_module, "fullname") fullname = ansible_module.params_get("fullname")
displayname = module_params_get(ansible_module, "displayname") displayname = ansible_module.params_get("displayname")
initials = module_params_get(ansible_module, "initials") initials = ansible_module.params_get("initials")
homedir = module_params_get(ansible_module, "homedir") homedir = ansible_module.params_get("homedir")
shell = module_params_get(ansible_module, "shell") shell = ansible_module.params_get("shell")
email = module_params_get(ansible_module, "email") email = ansible_module.params_get("email")
principal = module_params_get(ansible_module, "principal") principal = ansible_module.params_get("principal")
principalexpiration = module_params_get(ansible_module, principalexpiration = ansible_module.params_get(
"principalexpiration") "principalexpiration")
if principalexpiration is not None: if principalexpiration is not None:
if principalexpiration[:-1] != "Z": if principalexpiration[:-1] != "Z":
principalexpiration = principalexpiration + "Z" principalexpiration = principalexpiration + "Z"
principalexpiration = date_format(principalexpiration) principalexpiration = date_format(principalexpiration)
passwordexpiration = module_params_get(ansible_module, passwordexpiration = ansible_module.params_get("passwordexpiration")
"passwordexpiration")
if passwordexpiration is not None: if passwordexpiration is not None:
if passwordexpiration[:-1] != "Z": if passwordexpiration[:-1] != "Z":
passwordexpiration = passwordexpiration + "Z" passwordexpiration = passwordexpiration + "Z"
passwordexpiration = date_format(passwordexpiration) passwordexpiration = date_format(passwordexpiration)
password = module_params_get(ansible_module, "password") password = ansible_module.params_get("password")
random = module_params_get(ansible_module, "random") random = ansible_module.params_get("random")
uid = module_params_get(ansible_module, "uid") uid = ansible_module.params_get("uid")
gid = module_params_get(ansible_module, "gid") gid = ansible_module.params_get("gid")
city = module_params_get(ansible_module, "city") city = ansible_module.params_get("city")
userstate = module_params_get(ansible_module, "userstate") userstate = ansible_module.params_get("userstate")
postalcode = module_params_get(ansible_module, "postalcode") postalcode = ansible_module.params_get("postalcode")
phone = module_params_get(ansible_module, "phone") phone = ansible_module.params_get("phone")
mobile = module_params_get(ansible_module, "mobile") mobile = ansible_module.params_get("mobile")
pager = module_params_get(ansible_module, "pager") pager = ansible_module.params_get("pager")
fax = module_params_get(ansible_module, "fax") fax = ansible_module.params_get("fax")
orgunit = module_params_get(ansible_module, "orgunit") orgunit = ansible_module.params_get("orgunit")
title = module_params_get(ansible_module, "title") title = ansible_module.params_get("title")
manager = module_params_get(ansible_module, "manager") manager = ansible_module.params_get("manager")
carlicense = module_params_get(ansible_module, "carlicense") carlicense = ansible_module.params_get("carlicense")
sshpubkey = module_params_get(ansible_module, "sshpubkey") sshpubkey = ansible_module.params_get("sshpubkey")
userauthtype = module_params_get(ansible_module, "userauthtype") userauthtype = ansible_module.params_get("userauthtype")
userclass = module_params_get(ansible_module, "userclass") userclass = ansible_module.params_get("userclass")
radius = module_params_get(ansible_module, "radius") radius = ansible_module.params_get("radius")
radiususer = module_params_get(ansible_module, "radiususer") radiususer = ansible_module.params_get("radiususer")
departmentnumber = module_params_get(ansible_module, "departmentnumber") departmentnumber = ansible_module.params_get("departmentnumber")
employeenumber = module_params_get(ansible_module, "employeenumber") employeenumber = ansible_module.params_get("employeenumber")
employeetype = module_params_get(ansible_module, "employeetype") employeetype = ansible_module.params_get("employeetype")
preferredlanguage = module_params_get(ansible_module, "preferredlanguage") preferredlanguage = ansible_module.params_get("preferredlanguage")
certificate = module_params_get(ansible_module, "certificate") certificate = ansible_module.params_get("certificate")
certmapdata = module_params_get(ansible_module, "certmapdata") certmapdata = ansible_module.params_get("certmapdata")
noprivate = module_params_get(ansible_module, "noprivate") noprivate = ansible_module.params_get("noprivate")
nomembers = module_params_get(ansible_module, "nomembers") nomembers = ansible_module.params_get("nomembers")
# deleted # deleted
preserve = module_params_get(ansible_module, "preserve") preserve = ansible_module.params_get("preserve")
# mod # mod
update_password = module_params_get(ansible_module, "update_password") update_password = ansible_module.params_get("update_password")
# general # general
action = module_params_get(ansible_module, "action") action = ansible_module.params_get("action")
state = module_params_get(ansible_module, "state") state = ansible_module.params_get("state")
# Check parameters # Check parameters
@@ -930,21 +914,17 @@ 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()
# Check version specific settings # Check version specific settings
server_realm = api_get_realm() server_realm = ansible_module.ipa_get_realm()
# Default email domain # Default email domain
result = api_command_no_name(ansible_module, "config_show", {}) result = ansible_module.ipa_command_no_name("config_show", {})
default_email_domain = result["result"]["ipadefaultemaildomain"][0] default_email_domain = result["result"]["ipadefaultemaildomain"][0]
# Extend email addresses # Extend email addresses
@@ -1048,7 +1028,8 @@ def main():
# be part of check_parameters as this is used also before the # be part of check_parameters as this is used also before the
# connection to the API has been established. # connection to the API has been established.
if passwordexpiration is not None and \ if passwordexpiration is not None and \
not api_check_param("user_add", "krbpasswordexpiration"): not ansible_module.ipa_command_param_exists(
"user_add", "krbpasswordexpiration"):
ansible_module.fail_json( ansible_module.fail_json(
msg="The use of passwordexpiration is not supported by " msg="The use of passwordexpiration is not supported by "
"your IPA version") "your IPA version")
@@ -1058,7 +1039,7 @@ def main():
# be part of check_parameters as this is used also before the # be part of check_parameters as this is used also before the
# connection to the API has been established. # connection to the API has been established.
if certmapdata is not None and \ if certmapdata is not None and \
not api_check_command("user_add_certmapdata"): not ansible_module.ipa_command_exists("user_add_certmapdata"):
ansible_module.fail_json( ansible_module.fail_json(
msg="The use of certmapdata is not supported by " msg="The use of certmapdata is not supported by "
"your IPA version") "your IPA version")
@@ -1387,8 +1368,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
@@ -1432,12 +1412,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, user=exit_args) ansible_module.exit_json(changed=changed, user=exit_args)