mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-08-01 04:14:42 +00:00
location: 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:
@@ -66,21 +66,14 @@ 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, temp_kdestroy, valid_creds, api_connect, api_command, \
|
IPAAnsibleModule, compare_args_ipa
|
||||||
compare_args_ipa, module_params_get, ipamodule_base_spec, \
|
|
||||||
get_ipamodule_base_vars
|
|
||||||
import six
|
|
||||||
|
|
||||||
if six.PY3:
|
|
||||||
unicode = str
|
|
||||||
|
|
||||||
|
|
||||||
def find_location(module, name):
|
def find_location(module, name):
|
||||||
"""Find if a location with the given name already exist."""
|
"""Find if a location with the given name already exist."""
|
||||||
try:
|
try:
|
||||||
_result = api_command(module, "location_show", name, {"all": True})
|
_result = module.ipa_command("location_show", name, {"all": True})
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
# An exception is raised if location name is not found.
|
# An exception is raised if location name is not found.
|
||||||
return None
|
return None
|
||||||
@@ -96,20 +89,16 @@ def gen_args(description):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Arguments
|
ansible_module = IPAAnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec=dict(
|
||||||
name=dict(type="list", aliases=["idnsname"],
|
name=dict(type="list", aliases=["idnsname"],
|
||||||
default=None, required=True),
|
default=None, required=True),
|
||||||
# present
|
# present
|
||||||
description=dict(required=False, type='str', default=None),
|
description=dict(required=False, type='str', default=None),
|
||||||
# state
|
# state
|
||||||
state=dict(type="str", default="present",
|
state=dict(type="str", default="present",
|
||||||
choices=["present", "absent"]),
|
choices=["present", "absent"]),
|
||||||
)
|
),
|
||||||
argument_spec.update(ipamodule_base_spec)
|
|
||||||
|
|
||||||
ansible_module = AnsibleModule(
|
|
||||||
argument_spec=argument_spec,
|
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -118,14 +107,13 @@ def main():
|
|||||||
# Get parameters
|
# Get parameters
|
||||||
|
|
||||||
# general
|
# general
|
||||||
base_vars = get_ipamodule_base_vars(ansible_module)
|
names = ansible_module.params_get("name")
|
||||||
names = module_params_get(ansible_module, "name")
|
|
||||||
|
|
||||||
# present
|
# present
|
||||||
description = module_params_get(ansible_module, "description")
|
description = ansible_module.params_get("description")
|
||||||
|
|
||||||
# state
|
# state
|
||||||
state = module_params_get(ansible_module, "state")
|
state = ansible_module.params_get("state")
|
||||||
|
|
||||||
# Check parameters
|
# Check parameters
|
||||||
|
|
||||||
@@ -148,15 +136,9 @@ def main():
|
|||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
exit_args = {}
|
exit_args = {}
|
||||||
ccache_dir = None
|
|
||||||
ccache_name = None
|
|
||||||
try:
|
|
||||||
if not valid_creds(ansible_module, base_vars["ipaadmin_principal"]):
|
|
||||||
ccache_dir, ccache_name = temp_kinit(
|
|
||||||
base_vars["ipaadmin_principal"],
|
|
||||||
base_vars["ipaadmin_password"])
|
|
||||||
api_connect()
|
|
||||||
|
|
||||||
|
# Connect to IPA API
|
||||||
|
with ansible_module.ipa_connect():
|
||||||
commands = []
|
commands = []
|
||||||
for name in names:
|
for name in names:
|
||||||
# Make sure location exists
|
# Make sure location exists
|
||||||
@@ -194,8 +176,7 @@ def main():
|
|||||||
|
|
||||||
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
|
||||||
@@ -205,12 +186,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