mirror of
https://github.com/freeipa/ansible-freeipa.git
synced 2026-07-30 19:34:45 +00:00
host: 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: ipahost
|
module: ipahost
|
||||||
short description: Manage FreeIPA hosts
|
short description: Manage FreeIPA hosts
|
||||||
description: Manage FreeIPA hosts
|
description: Manage FreeIPA hosts
|
||||||
|
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 full qualified domain name.
|
description: The full qualified domain name.
|
||||||
aliases: ["fqdn"]
|
aliases: ["fqdn"]
|
||||||
@@ -380,7 +376,7 @@ EXAMPLES = """
|
|||||||
|
|
||||||
# Ensure host is absent
|
# Ensure host is absent
|
||||||
- ipahost:
|
- ipahost:
|
||||||
ipaadmin_password: password1
|
ipaadmin_password: SomeADMINpassword
|
||||||
name: host01.example.com
|
name: host01.example.com
|
||||||
state: absent
|
state: absent
|
||||||
"""
|
"""
|
||||||
@@ -404,15 +400,10 @@ host:
|
|||||||
returned: always
|
returned: always
|
||||||
"""
|
"""
|
||||||
|
|
||||||
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, gen_add_del_lists, \
|
||||||
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
|
encode_certificate, is_ipv4_addr, is_ipv6_addr, ipalib_errors
|
||||||
temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa, \
|
|
||||||
module_params_get, gen_add_del_lists, encode_certificate, api_get_realm, \
|
|
||||||
is_ipv4_addr, is_ipv6_addr, ipalib_errors
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
unicode = str
|
unicode = str
|
||||||
|
|
||||||
@@ -423,7 +414,7 @@ def find_host(module, name):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_result = api_command(module, "host_show", to_text(name), _args)
|
_result = module.ipa_command("host_show", name, _args)
|
||||||
except ipalib_errors.NotFound as e:
|
except ipalib_errors.NotFound as e:
|
||||||
msg = str(e)
|
msg = str(e)
|
||||||
if "host not found" in msg:
|
if "host not found" in msg:
|
||||||
@@ -450,17 +441,16 @@ def find_dnsrecord(module, name):
|
|||||||
|
|
||||||
_args = {
|
_args = {
|
||||||
"all": True,
|
"all": True,
|
||||||
"idnsname": to_text(host_name)
|
"idnsname": host_name
|
||||||
}
|
}
|
||||||
|
|
||||||
_result = api_command(module, "dnsrecord_show", to_text(domain_name),
|
_result = module.ipa_command("dnsrecord_show", domain_name, _args)
|
||||||
_args)
|
|
||||||
|
|
||||||
return _result["result"]
|
return _result["result"]
|
||||||
|
|
||||||
|
|
||||||
def show_host(module, name):
|
def show_host(module, name):
|
||||||
_result = api_command(module, "host_show", to_text(name), {})
|
_result = module.ipa_command("host_show", name, {})
|
||||||
return _result["result"]
|
return _result["result"]
|
||||||
|
|
||||||
|
|
||||||
@@ -663,12 +653,9 @@ def main():
|
|||||||
# krbprincipalname
|
# krbprincipalname
|
||||||
)
|
)
|
||||||
|
|
||||||
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", no_log=True),
|
|
||||||
|
|
||||||
name=dict(type="list", aliases=["fqdn"], default=None,
|
name=dict(type="list", aliases=["fqdn"], default=None,
|
||||||
required=False),
|
required=False),
|
||||||
|
|
||||||
@@ -705,56 +692,52 @@ def main():
|
|||||||
# Get parameters
|
# Get parameters
|
||||||
|
|
||||||
# general
|
# general
|
||||||
ipaadmin_principal = module_params_get(ansible_module,
|
names = ansible_module.params_get("name")
|
||||||
"ipaadmin_principal")
|
hosts = ansible_module.params_get("hosts")
|
||||||
ipaadmin_password = module_params_get(ansible_module,
|
|
||||||
"ipaadmin_password")
|
|
||||||
names = module_params_get(ansible_module, "name")
|
|
||||||
hosts = module_params_get(ansible_module, "hosts")
|
|
||||||
|
|
||||||
# present
|
# present
|
||||||
description = module_params_get(ansible_module, "description")
|
description = ansible_module.params_get("description")
|
||||||
locality = module_params_get(ansible_module, "locality")
|
locality = ansible_module.params_get("locality")
|
||||||
location = module_params_get(ansible_module, "location")
|
location = ansible_module.params_get("location")
|
||||||
platform = module_params_get(ansible_module, "platform")
|
platform = ansible_module.params_get("platform")
|
||||||
os = module_params_get(ansible_module, "os")
|
os = ansible_module.params_get("os")
|
||||||
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")
|
||||||
certificate = module_params_get(ansible_module, "certificate")
|
certificate = ansible_module.params_get("certificate")
|
||||||
managedby_host = module_params_get(ansible_module, "managedby_host")
|
managedby_host = ansible_module.params_get("managedby_host")
|
||||||
principal = module_params_get(ansible_module, "principal")
|
principal = ansible_module.params_get("principal")
|
||||||
allow_create_keytab_user = module_params_get(
|
allow_create_keytab_user = ansible_module.params_get(
|
||||||
ansible_module, "allow_create_keytab_user")
|
"allow_create_keytab_user")
|
||||||
allow_create_keytab_group = module_params_get(
|
allow_create_keytab_group = ansible_module.params_get(
|
||||||
ansible_module, "allow_create_keytab_group")
|
"allow_create_keytab_group")
|
||||||
allow_create_keytab_host = module_params_get(
|
allow_create_keytab_host = ansible_module.params_get(
|
||||||
ansible_module, "allow_create_keytab_host")
|
"allow_create_keytab_host")
|
||||||
allow_create_keytab_hostgroup = module_params_get(
|
allow_create_keytab_hostgroup = ansible_module.params_get(
|
||||||
ansible_module, "allow_create_keytab_hostgroup")
|
"allow_create_keytab_hostgroup")
|
||||||
allow_retrieve_keytab_user = module_params_get(
|
allow_retrieve_keytab_user = ansible_module.params_get(
|
||||||
ansible_module, "allow_retrieve_keytab_user")
|
"allow_retrieve_keytab_user")
|
||||||
allow_retrieve_keytab_group = module_params_get(
|
allow_retrieve_keytab_group = ansible_module.params_get(
|
||||||
ansible_module, "allow_retrieve_keytab_group")
|
"allow_retrieve_keytab_group")
|
||||||
allow_retrieve_keytab_host = module_params_get(
|
allow_retrieve_keytab_host = ansible_module.params_get(
|
||||||
ansible_module, "allow_retrieve_keytab_host")
|
"allow_retrieve_keytab_host")
|
||||||
allow_retrieve_keytab_hostgroup = module_params_get(
|
allow_retrieve_keytab_hostgroup = ansible_module.params_get(
|
||||||
ansible_module, "allow_retrieve_keytab_hostgroup")
|
"allow_retrieve_keytab_hostgroup")
|
||||||
mac_address = module_params_get(ansible_module, "mac_address")
|
mac_address = ansible_module.params_get("mac_address")
|
||||||
sshpubkey = module_params_get(ansible_module, "sshpubkey")
|
sshpubkey = ansible_module.params_get("sshpubkey")
|
||||||
userclass = module_params_get(ansible_module, "userclass")
|
userclass = ansible_module.params_get("userclass")
|
||||||
auth_ind = module_params_get(ansible_module, "auth_ind")
|
auth_ind = ansible_module.params_get("auth_ind")
|
||||||
requires_pre_auth = module_params_get(ansible_module, "requires_pre_auth")
|
requires_pre_auth = ansible_module.params_get("requires_pre_auth")
|
||||||
ok_as_delegate = module_params_get(ansible_module, "ok_as_delegate")
|
ok_as_delegate = ansible_module.params_get("ok_as_delegate")
|
||||||
ok_to_auth_as_delegate = module_params_get(ansible_module,
|
ok_to_auth_as_delegate = ansible_module.params_get(
|
||||||
"ok_to_auth_as_delegate")
|
"ok_to_auth_as_delegate")
|
||||||
force = module_params_get(ansible_module, "force")
|
force = ansible_module.params_get("force")
|
||||||
reverse = module_params_get(ansible_module, "reverse")
|
reverse = ansible_module.params_get("reverse")
|
||||||
ip_address = module_params_get(ansible_module, "ip_address")
|
ip_address = ansible_module.params_get("ip_address")
|
||||||
update_dns = module_params_get(ansible_module, "update_dns")
|
update_dns = ansible_module.params_get("update_dns")
|
||||||
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
|
||||||
|
|
||||||
@@ -786,17 +769,13 @@ 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()
|
||||||
|
|
||||||
commands = []
|
commands = []
|
||||||
host_set = set()
|
host_set = set()
|
||||||
@@ -973,7 +952,7 @@ def main():
|
|||||||
# Principals are not returned as utf8 for IPA using
|
# Principals are not returned as utf8 for IPA using
|
||||||
# python2 using host_show, therefore we need to
|
# python2 using host_show, therefore we need to
|
||||||
# convert the principals that we should remove.
|
# convert the principals that we should remove.
|
||||||
principal_del = [to_text(x) for x in principal_del]
|
principal_del = [unicode(x) for x in principal_del]
|
||||||
|
|
||||||
(allow_create_keytab_user_add,
|
(allow_create_keytab_user_add,
|
||||||
allow_create_keytab_user_del) = \
|
allow_create_keytab_user_del) = \
|
||||||
@@ -1373,8 +1352,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, to_text(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
|
||||||
@@ -1428,12 +1406,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, host=exit_args)
|
ansible_module.exit_json(changed=changed, host=exit_args)
|
||||||
|
|||||||
Reference in New Issue
Block a user