mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-07-30 19:34:45 +00:00
sudocmd: 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:
@@ -32,13 +32,9 @@ DOCUMENTATION = """
|
|||||||
module: ipasudocmd
|
module: ipasudocmd
|
||||||
short description: Manage FreeIPA sudo command
|
short description: Manage FreeIPA sudo command
|
||||||
description: Manage FreeIPA sudo command
|
description: Manage FreeIPA sudo command
|
||||||
|
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 sudo command
|
description: The sudo command
|
||||||
required: true
|
required: true
|
||||||
@@ -71,19 +67,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_sudocmd(module, name):
|
def find_sudocmd(module, name):
|
||||||
_args = {
|
_args = {
|
||||||
"all": True,
|
"all": True,
|
||||||
"sudocmd": to_text(name),
|
"sudocmd": name,
|
||||||
}
|
}
|
||||||
|
|
||||||
_result = api_command(module, "sudocmd_find", to_text(name), _args)
|
_result = module.ipa_command("sudocmd_find", name, _args)
|
||||||
|
|
||||||
if len(_result["result"]) > 1:
|
if len(_result["result"]) > 1:
|
||||||
module.fail_json(
|
module.fail_json(
|
||||||
@@ -97,18 +91,15 @@ def find_sudocmd(module, name):
|
|||||||
def gen_args(description):
|
def gen_args(description):
|
||||||
_args = {}
|
_args = {}
|
||||||
if description is not None:
|
if description is not None:
|
||||||
_args["description"] = to_text(description)
|
_args["description"] = description
|
||||||
|
|
||||||
return _args
|
return _args
|
||||||
|
|
||||||
|
|
||||||
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=["sudocmd"], default=None,
|
name=dict(type="list", aliases=["sudocmd"], default=None,
|
||||||
required=True),
|
required=True),
|
||||||
# present
|
# present
|
||||||
@@ -125,14 +116,12 @@ 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
|
||||||
description = ansible_module.params.get("description")
|
description = ansible_module.params_get("description")
|
||||||
# state
|
# state
|
||||||
state = ansible_module.params.get("state")
|
state = ansible_module.params_get("state")
|
||||||
|
|
||||||
# Check parameters
|
# Check parameters
|
||||||
if state == "absent":
|
if state == "absent":
|
||||||
@@ -147,13 +136,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 = []
|
||||||
|
|
||||||
@@ -189,8 +174,7 @@ def main():
|
|||||||
# Execute commands
|
# Execute commands
|
||||||
for name, command, args in commands:
|
for name, command, args in commands:
|
||||||
try:
|
try:
|
||||||
result = api_command(ansible_module, command, to_text(name),
|
result = ansible_module.ipa_command(command, name, args)
|
||||||
args)
|
|
||||||
# Check if any changes were made by any command
|
# Check if any changes were made by any command
|
||||||
if command == 'sudocmd_del':
|
if command == 'sudocmd_del':
|
||||||
changed |= "Deleted" in result['summary']
|
changed |= "Deleted" in result['summary']
|
||||||
@@ -200,12 +184,6 @@ def main():
|
|||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user