mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-07-31 03:44:41 +00:00
sudorule: 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:
@@ -31,13 +31,9 @@ DOCUMENTATION = """
|
|||||||
module: ipasudorule
|
module: ipasudorule
|
||||||
short description: Manage FreeIPA sudo rules
|
short description: Manage FreeIPA sudo rules
|
||||||
description: Manage FreeIPA sudo rules
|
description: Manage FreeIPA sudo 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 sudorule name
|
description: The sudorule name
|
||||||
required: true
|
required: true
|
||||||
@@ -187,10 +183,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
|
||||||
module_params_get, gen_add_del_lists, gen_add_list, gen_intersection_list
|
|
||||||
|
|
||||||
|
|
||||||
def find_sudorule(module, name):
|
def find_sudorule(module, name):
|
||||||
@@ -199,7 +194,7 @@ def find_sudorule(module, name):
|
|||||||
"cn": name,
|
"cn": name,
|
||||||
}
|
}
|
||||||
|
|
||||||
_result = api_command(module, "sudorule_find", name, _args)
|
_result = module.ipa_command("sudorule_find", name, _args)
|
||||||
|
|
||||||
if len(_result["result"]) > 1:
|
if len(_result["result"]) > 1:
|
||||||
module.fail_json(
|
module.fail_json(
|
||||||
@@ -235,12 +230,9 @@ def gen_args(description, usercat, hostcat, cmdcat, runasusercat,
|
|||||||
|
|
||||||
|
|
||||||
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
|
||||||
@@ -286,42 +278,37 @@ 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
|
||||||
# The 'noqa' variables are not used here, but required for vars().
|
# The 'noqa' variables are not used here, but required for vars().
|
||||||
# The use of 'noqa' ensures flake8 does not complain about them.
|
# The use of 'noqa' ensures flake8 does not complain about them.
|
||||||
description = module_params_get(ansible_module, "description") # noqa
|
description = ansible_module.params_get("description") # noqa
|
||||||
cmdcategory = module_params_get(ansible_module, 'cmdcategory') # noqa
|
cmdcategory = ansible_module.params_get('cmdcategory') # noqa
|
||||||
usercategory = module_params_get(ansible_module, "usercategory") # noqa
|
usercategory = ansible_module.params_get("usercategory") # noqa
|
||||||
hostcategory = module_params_get(ansible_module, "hostcategory") # noqa
|
hostcategory = ansible_module.params_get("hostcategory") # noqa
|
||||||
runasusercategory = module_params_get(ansible_module, # noqa
|
runasusercategory = ansible_module.params_get( # noqa
|
||||||
"runasusercategory")
|
"runasusercategory")
|
||||||
runasgroupcategory = module_params_get(ansible_module, # noqa
|
runasgroupcategory = ansible_module.params_get( # noqa
|
||||||
"runasgroupcategory")
|
"runasgroupcategory")
|
||||||
hostcategory = module_params_get(ansible_module, "hostcategory") # noqa
|
hostcategory = ansible_module.params_get("hostcategory") # noqa
|
||||||
nomembers = module_params_get(ansible_module, "nomembers") # noqa
|
nomembers = ansible_module.params_get("nomembers") # noqa
|
||||||
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")
|
||||||
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")
|
||||||
allow_sudocmd = module_params_get(ansible_module, 'allow_sudocmd')
|
allow_sudocmd = ansible_module.params_get('allow_sudocmd')
|
||||||
allow_sudocmdgroup = module_params_get(ansible_module,
|
allow_sudocmdgroup = ansible_module.params_get('allow_sudocmdgroup')
|
||||||
'allow_sudocmdgroup')
|
deny_sudocmd = ansible_module.params_get('deny_sudocmd')
|
||||||
deny_sudocmd = module_params_get(ansible_module, 'deny_sudocmd')
|
deny_sudocmdgroup = ansible_module.params_get('deny_sudocmdgroup')
|
||||||
deny_sudocmdgroup = module_params_get(ansible_module,
|
sudooption = ansible_module.params_get("sudooption")
|
||||||
'deny_sudocmdgroup')
|
order = ansible_module.params_get("order")
|
||||||
sudooption = module_params_get(ansible_module, "sudooption")
|
runasuser = ansible_module.params_get("runasuser")
|
||||||
order = module_params_get(ansible_module, "order")
|
runasgroup = ansible_module.params_get("runasgroup")
|
||||||
runasuser = module_params_get(ansible_module, "runasuser")
|
action = ansible_module.params_get("action")
|
||||||
runasgroup = module_params_get(ansible_module, "runasgroup")
|
|
||||||
action = module_params_get(ansible_module, "action")
|
|
||||||
|
|
||||||
# state
|
# state
|
||||||
state = module_params_get(ansible_module, "state")
|
state = ansible_module.params_get("state")
|
||||||
|
|
||||||
# Check parameters
|
# Check parameters
|
||||||
|
|
||||||
@@ -393,13 +380,9 @@ 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()
|
|
||||||
|
|
||||||
commands = []
|
commands = []
|
||||||
|
|
||||||
@@ -850,8 +833,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:
|
||||||
@@ -873,12 +855,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 ex:
|
|
||||||
ansible_module.fail_json(msg=str(ex))
|
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user