mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-07-31 03:44:41 +00:00
user: Use execute_ipa_commands
execute_ipa_commands replces the check mode exit, the loop over the generated commands and also in the member failure handling for modules with member support.
This commit is contained in:
@@ -716,6 +716,46 @@ def gen_certmapdata_args(certmapdata):
|
|||||||
return {"ipacertmapdata": to_text(certmapdata)}
|
return {"ipacertmapdata": to_text(certmapdata)}
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def result_handler(module, result, command, name, args, errors, exit_args,
|
||||||
|
one_name):
|
||||||
|
|
||||||
|
if "random" in args and command in ["user_add", "user_mod"] \
|
||||||
|
and "randompassword" in result["result"]:
|
||||||
|
if one_name:
|
||||||
|
exit_args["randompassword"] = \
|
||||||
|
result["result"]["randompassword"]
|
||||||
|
else:
|
||||||
|
exit_args.setdefault(name, {})["randompassword"] = \
|
||||||
|
result["result"]["randompassword"]
|
||||||
|
|
||||||
|
# Get all errors
|
||||||
|
# All "already a member" and "not a member" failures in the
|
||||||
|
# result are ignored. All others are reported.
|
||||||
|
if "failed" in result and len(result["failed"]) > 0:
|
||||||
|
for item in result["failed"]:
|
||||||
|
failed_item = result["failed"][item]
|
||||||
|
for member_type in failed_item:
|
||||||
|
for member, failure in failed_item[member_type]:
|
||||||
|
if "already a member" in failure \
|
||||||
|
or "not a member" in failure:
|
||||||
|
continue
|
||||||
|
errors.append("%s: %s %s: %s" % (
|
||||||
|
command, member_type, member, failure))
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def exception_handler(module, ex, errors, exit_args, one_name):
|
||||||
|
msg = str(ex)
|
||||||
|
if "already contains" in msg \
|
||||||
|
or "does not contain" in msg:
|
||||||
|
return True
|
||||||
|
# The canonical principal name may not be removed
|
||||||
|
if "equal to the canonical principal name must" in msg:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
user_spec = dict(
|
user_spec = dict(
|
||||||
# present
|
# present
|
||||||
@@ -1359,58 +1399,11 @@ def main():
|
|||||||
|
|
||||||
del user_set
|
del user_set
|
||||||
|
|
||||||
# Check mode exit
|
|
||||||
if ansible_module.check_mode:
|
|
||||||
ansible_module.exit_json(changed=len(commands) > 0, **exit_args)
|
|
||||||
|
|
||||||
# Execute commands
|
# Execute commands
|
||||||
|
|
||||||
errors = []
|
changed = ansible_module.execute_ipa_commands(
|
||||||
for name, command, args in commands:
|
commands, result_handler, exception_handler,
|
||||||
try:
|
exit_args=exit_args, one_name=len(names) == 1)
|
||||||
result = ansible_module.ipa_command(command, name, args)
|
|
||||||
if "completed" in result:
|
|
||||||
if result["completed"] > 0:
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
if "random" in args and command in ["user_add", "user_mod"] \
|
|
||||||
and "randompassword" in result["result"]:
|
|
||||||
if len(names) == 1:
|
|
||||||
exit_args["randompassword"] = \
|
|
||||||
result["result"]["randompassword"]
|
|
||||||
else:
|
|
||||||
exit_args.setdefault(name, {})["randompassword"] = \
|
|
||||||
result["result"]["randompassword"]
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
msg = str(e)
|
|
||||||
if "already contains" in msg \
|
|
||||||
or "does not contain" in msg:
|
|
||||||
continue
|
|
||||||
# The canonical principal name may not be removed
|
|
||||||
if "equal to the canonical principal name must" in msg:
|
|
||||||
continue
|
|
||||||
ansible_module.fail_json(msg="%s: %s: %s" % (command, name,
|
|
||||||
msg))
|
|
||||||
|
|
||||||
# Get all errors
|
|
||||||
# All "already a member" and "not a member" failures in the
|
|
||||||
# result are ignored. All others are reported.
|
|
||||||
if "failed" in result and len(result["failed"]) > 0:
|
|
||||||
for item in result["failed"]:
|
|
||||||
failed_item = result["failed"][item]
|
|
||||||
for member_type in failed_item:
|
|
||||||
for member, failure in failed_item[member_type]:
|
|
||||||
if "already a member" in failure \
|
|
||||||
or "not a member" in failure:
|
|
||||||
continue
|
|
||||||
errors.append("%s: %s %s: %s" % (
|
|
||||||
command, member_type, member, failure))
|
|
||||||
|
|
||||||
if len(errors) > 0:
|
|
||||||
ansible_module.fail_json(msg=", ".join(errors))
|
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
ansible_module.exit_json(changed=changed, user=exit_args)
|
ansible_module.exit_json(changed=changed, user=exit_args)
|
||||||
|
|||||||
Reference in New Issue
Block a user