Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 72 additions & 10 deletions plugins/modules/ipaautomember.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@
"""

from ansible.module_utils.ansible_freeipa_module import (
IPAAnsibleModule, compare_args_ipa, gen_add_del_lists, ipalib_errors, DN
IPAAnsibleModule, compare_args_ipa, gen_add_del_lists, ipalib_errors, DN,
IPADiffTracker, gen_args_diff
)


Expand Down Expand Up @@ -444,9 +445,9 @@ def main():
changed = False
exit_args = {}
res_find = None
diff_tracker = IPADiffTracker()

with ansible_module.ipa_connect():

commands = []

for name in names:
Expand All @@ -459,7 +460,6 @@ def main():

# Check inclusive and exclusive conditions
if inclusive is not None or exclusive is not None:
# automember_type is either "group" or "hostgorup"
if automember_type == "group":
_type = u"user"
elif automember_type == "hostgroup":
Expand Down Expand Up @@ -490,20 +490,23 @@ def main():
res_find,
ignore=['type']):
commands.append([name, 'automember_mod', args])
before, after = gen_args_diff(args, res_find, ignore=['type'])
diff_tracker.add_entry_diff(name, before, after)
else:
commands.append([name, 'automember_add', args])
diff_tracker.add_entry_diff(name, {}, args)
res_find = {}

if inclusive is not None:
inclusive_add, inclusive_del = gen_add_del_lists(
transform_conditions(inclusive),
res_find.get("automemberinclusiveregex", [])
(res_find or {}).get("automemberinclusiveregex", [])
)

if exclusive is not None:
exclusive_add, exclusive_del = gen_add_del_lists(
transform_conditions(exclusive),
res_find.get("automemberexclusiveregex", [])
(res_find or {}).get("automemberexclusiveregex", [])
)

elif action == "member":
Expand All @@ -520,33 +523,68 @@ def main():
automember_type, key, inclusiveregex=regex)
commands.append([name, 'automember_add_condition',
condition_args])
# For diff: show the regex being added
before_list = (res_find or {}).get("automemberinclusiveregex", [])
after_list = before_list + [f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"inclusive": before_list},
{"inclusive": after_list}
)

for _inclusive in inclusive_del:
key, regex = _inclusive.split("=", 1)
condition_args = gen_condition_args(
automember_type, key, inclusiveregex=regex)
commands.append([name, 'automember_remove_condition',
condition_args])
# For diff: show the regex being removed
before_list = (res_find or {}).get("automemberinclusiveregex", [])
after_list = [r for r in before_list if r != f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"inclusive": before_list},
{"inclusive": after_list}
)

for _exclusive in exclusive_add:
key, regex = _exclusive.split("=", 1)
condition_args = gen_condition_args(
automember_type, key, exclusiveregex=regex)
commands.append([name, 'automember_add_condition',
condition_args])
before_list = (res_find or {}).get("automemberexclusiveregex", [])
after_list = before_list + [f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"exclusive": before_list},
{"exclusive": after_list}
)

for _exclusive in exclusive_del:
key, regex = _exclusive.split("=", 1)
condition_args = gen_condition_args(
automember_type, key, exclusiveregex=regex)
commands.append([name, 'automember_remove_condition',
condition_args])
before_list = (res_find or {}).get("automemberexclusiveregex", [])
after_list = [r for r in before_list if r != f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"exclusive": before_list},
{"exclusive": after_list}
)

elif state == 'absent':
if action == "automember":
if res_find is not None:
commands.append([name, 'automember_del',
{'type': automember_type}])
diff_tracker.add_entry_diff(
name,
{"state": "present"},
{"state": "absent"}
)

elif action == "member":
if res_find is None:
Expand All @@ -561,6 +599,13 @@ def main():
commands.append(
[name, 'automember_remove_condition',
condition_args])
before_list = (res_find or {}).get("automemberinclusiveregex", [])
after_list = [r for r in before_list if r != f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"inclusive": before_list},
{"inclusive": after_list}
)

if exclusive is not None:
for _exclusive in transform_conditions(exclusive):
Expand All @@ -570,6 +615,13 @@ def main():
commands.append([name,
'automember_remove_condition',
condition_args])
before_list = (res_find or {}).get("automemberexclusiveregex", [])
after_list = [r for r in before_list if r != f"{key}={regex}"]
diff_tracker.add_entry_diff(
name,
{"exclusive": before_list},
{"exclusive": after_list}
)

if len(names) == 0:
if state == "rebuilt":
Expand All @@ -580,7 +632,7 @@ def main():
elif state == "orphans_removed":
res_find = find_automember_orphans(ansible_module,
automember_type)
if res_find["count"] > 0:
if (res_find or {}).get("count", 0) > 0:
commands.append([None, 'automember_find_orphans',
{'type': automember_type,
'remove': True}])
Expand All @@ -590,28 +642,37 @@ def main():
automember_type)

if default_group == "":
if isinstance(res_find["automemberdefaultgroup"], list):
if isinstance((res_find or {}).get("automemberdefaultgroup", None), list):
commands.append([None,
'automember_default_group_remove',
{'type': automember_type}])
diff_tracker.add_entry_diff(
None,
{"default_group": (res_find or {}).get("automemberdefaultgroup", [])},
{"default_group": []}
)

else:
dn_default_group = [DN(('cn', default_group),
('cn', '%ss' % automember_type),
('cn', 'accounts'),
ansible_module.ipa_get_basedn())]
if repr(res_find["automemberdefaultgroup"]) != \
if repr((res_find or {}).get("automemberdefaultgroup", None)) != \
repr(dn_default_group):
commands.append(
[None, 'automember_default_group_set',
{'type': automember_type,
'automemberdefaultgroup': default_group}])
diff_tracker.add_entry_diff(
None,
{"default_group": (res_find or {}).get("automemberdefaultgroup", None)},
{"default_group": dn_default_group}
)

else:
ansible_module.fail_json(msg="Invalid operation")

# Execute commands

changed = ansible_module.execute_ipa_commands(commands)

# result["failed"] is used only for INCLUDE_RE, EXCLUDE_RE
Expand All @@ -622,7 +683,8 @@ def main():
# in other modules.

# Done
ansible_module.exit_json(changed=changed, **exit_args)
_exit_kwargs = dict(exit_args, **diff_tracker.build_diff())
ansible_module.exit_json(changed=changed, **_exit_kwargs)


if __name__ == "__main__":
Expand Down
13 changes: 7 additions & 6 deletions plugins/modules/ipadnsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"""

from ansible.module_utils.ansible_freeipa_module import \
IPAAnsibleModule, compare_args_ipa, is_ipv4_addr, is_ipv6_addr
IPAAnsibleModule, compare_args_ipa, is_ipv4_addr, is_ipv6_addr, IPADiffTracker, gen_args_diff


def find_dnsconfig(module):
Expand Down Expand Up @@ -242,30 +242,31 @@ def main():

# Init


changed = False
diff_tracker = IPADiffTracker()

# Connect to IPA API
with ansible_module.ipa_connect():

res_find = find_dnsconfig(ansible_module)
args = gen_args(ansible_module, state, action, res_find, forwarders,
forward_policy, allow_sync_ptr)

# Execute command only if configuration changes.
if not compare_args_ipa(ansible_module, args, res_find):
before, after = gen_args_diff(args, res_find)
diff_tracker.add_entry_diff("dnsconfig", before, after)
try:
if not ansible_module.check_mode:
ansible_module.ipa_command_no_name('dnsconfig_mod', args)
# If command did not fail, something changed.
changed = True

except Exception as e:
msg = str(e)
ansible_module.fail_json(msg="dnsconfig_mod: %s" % msg)

# Done

ansible_module.exit_json(changed=changed)
_exit_kwargs = dict(diff_tracker.build_diff())
ansible_module.exit_json(changed=changed, **_exit_kwargs)


if __name__ == "__main__":
Expand Down
39 changes: 37 additions & 2 deletions plugins/modules/ipadnsrecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,8 @@

from ansible.module_utils._text import to_text
from ansible.module_utils.ansible_freeipa_module import \
IPAAnsibleModule, is_ipv4_addr, is_ipv6_addr, ipalib_errors
IPAAnsibleModule, is_ipv4_addr, is_ipv6_addr, ipalib_errors, \
IPADiffTracker, gen_args_diff
try:
import dns.reversename
import dns.resolver
Expand Down Expand Up @@ -1586,6 +1587,7 @@ def main():

changed = False
exit_args = {}
diff_tracker = IPADiffTracker()

# Connect to IPA API
with ansible_module.ipa_connect():
Expand All @@ -1608,13 +1610,45 @@ def main():
res_find = find_dnsrecord(ansible_module, zone_name, name)

cmds = []
diff_key = "%s/%s" % (zone_name, name)

if state == 'present':
cmds = define_commands_for_present_state(
ansible_module, zone_name, entry, res_find)
if cmds:
args = gen_args(entry)
if res_find is None:
diff_tracker.add_entry_diff(diff_key, {}, args)
else:
before, after = gen_args_diff(
args, res_find, ignore=['idnsname'])
diff_tracker.add_entry_diff(diff_key, before, after)
elif state == 'absent':
cmds = define_commands_for_absent_state(
ansible_module, zone_name, entry, res_find)
if cmds:
args = gen_args(entry)
if args.get('del_all', False):
diff_tracker.add_entry_diff(
diff_key,
{"state": "present"}, {"state": "absent"})
else:
records_to_delete = {
k: v for k, v in args.items()
if k.endswith('record')
}
actually_removed = {}
if res_find:
for rec, values in records_to_delete.items():
del_list = [
v for v in values
if rec in res_find and v in res_find[rec]
]
if del_list:
actually_removed[rec] = del_list
if actually_removed:
diff_tracker.add_entry_diff(
diff_key, actually_removed, {})
else:
ansible_module.fail_json(msg="Unkown state '%s'" % state)

Expand All @@ -1626,7 +1660,8 @@ def main():
commands, exception_handler=exception_handler)

# Done
ansible_module.exit_json(changed=changed, host=exit_args)
_exit_kwargs = dict(host=exit_args, **diff_tracker.build_diff())
ansible_module.exit_json(changed=changed, **_exit_kwargs)


if __name__ == "__main__":
Expand Down
Loading