Skip to content

Commit

Permalink
ipa_host: Fix enabled and disabled states (ansible-collections#8920)
Browse files Browse the repository at this point in the history
* Fix ipa_host

* PR Fixes

* PR Fixes

* PR Doc fixes

* PR Doc fixes 2

* Fix default value
  • Loading branch information
abakanovskii authored and Massl123 committed Feb 7, 2025
1 parent 6aa842e commit 9db3f1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8920-ipa-host-fix-state.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ipa_host - add ``force_create``, fix ``enabled`` and ``disabled`` states (https://github.com/ansible-collections/community.general/issues/1094, https://github.com/ansible-collections/community.general/pull/8920).
28 changes: 20 additions & 8 deletions plugins/modules/ipa_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@
type: list
elements: str
state:
description: State to ensure.
description:
- State to ensure.
default: present
choices: ["absent", "disabled", "enabled", "present"]
type: str
force_creation:
description:
- Create host if O(state=disabled) or O(state=enabled) but not present.
default: true
type: bool
version_added: 9.5.0
update_dns:
description:
- If set V(true) with O(state=absent), then removes DNS records of the host managed by FreeIPA DNS.
Expand Down Expand Up @@ -233,26 +240,31 @@ def get_host_diff(client, ipa_host, module_host):
def ensure(module, client):
name = module.params['fqdn']
state = module.params['state']
force_creation = module.params['force_creation']

ipa_host = client.host_find(name=name)
module_host = get_host_dict(description=module.params['description'],
force=module.params['force'], ip_address=module.params['ip_address'],
force=module.params['force'],
ip_address=module.params['ip_address'],
ns_host_location=module.params['ns_host_location'],
ns_hardware_platform=module.params['ns_hardware_platform'],
ns_os_version=module.params['ns_os_version'],
user_certificate=module.params['user_certificate'],
mac_address=module.params['mac_address'],
random_password=module.params.get('random_password'),
random_password=module.params['random_password'],
)
changed = False
if state in ['present', 'enabled', 'disabled']:
if not ipa_host:
if not ipa_host and (force_creation or state == 'present'):
changed = True
if not module.check_mode:
# OTP password generated by FreeIPA is visible only for host_add command
# so, return directly from here.
return changed, client.host_add(name=name, host=module_host)
else:
if state in ['disabled', 'enabled']:
module.fail_json(msg="No host with name " + ipa_host + " found")

diff = get_host_diff(client, ipa_host, module_host)
if len(diff) > 0:
changed = True
Expand All @@ -261,11 +273,10 @@ def ensure(module, client):
for key in diff:
data[key] = module_host.get(key)
ipa_host_show = client.host_show(name=name)
if ipa_host_show.get('has_keytab', False) and module.params.get('random_password'):
if ipa_host_show.get('has_keytab', True) and (state == 'disabled' or module.params.get('random_password')):
client.host_disable(name=name)
return changed, client.host_mod(name=name, host=data)

else:
elif state == 'absent':
if ipa_host:
changed = True
update_dns = module.params.get('update_dns', False)
Expand All @@ -288,7 +299,8 @@ def main():
mac_address=dict(type='list', aliases=['macaddress'], elements='str'),
update_dns=dict(type='bool'),
state=dict(type='str', default='present', choices=['present', 'absent', 'enabled', 'disabled']),
random_password=dict(type='bool', no_log=False),)
random_password=dict(type='bool', no_log=False),
force_creation=dict(type='bool', default=True),)

module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
Expand Down

0 comments on commit 9db3f1a

Please sign in to comment.