Allows modification of forward policy in existing DNS Forward Zone.

This patch allows the modification of the forward zone policy in
an existing DNS Forward Zone, and fixes some issues with `enable`
and `disable` state that prevented correct behavior of `forwardpolicy`.
This commit is contained in:
Rafael Guterres Jeffman
2020-06-15 23:40:35 -03:00
parent bf864469a1
commit 857fb82eb9
2 changed files with 96 additions and 88 deletions

View File

@@ -217,10 +217,20 @@ def main():
else: else:
operation = "add" operation = "add"
if state == "disabled": if state in ["enabled", "disabled"]:
wants_enable = False if action == "member":
else: ansible_module.fail_json(
wants_enable = True msg="Action `member` cannot be used with state `%s`"
% (state))
invalid = [
"forwarders", "forwardpolicy", "skip_overlap_check", "permission"
]
for x in invalid:
if vars()[x] is not None:
ansible_module.fail_json(
msg="Argument '%s' can not be used with action "
"'%s', state `%s`" % (x, action, state))
wants_enable = (state == "enabled")
if operation == "del": if operation == "del":
invalid = [ invalid = [
@@ -230,7 +240,7 @@ def main():
if vars()[x] is not None: if vars()[x] is not None:
ansible_module.fail_json( ansible_module.fail_json(
msg="Argument '%s' can not be used with action " msg="Argument '%s' can not be used with action "
"'%s'" % (x, action)) "'%s', state `%s`" % (x, action, state))
changed = False changed = False
exit_args = {} exit_args = {}
@@ -262,7 +272,27 @@ def main():
if existing_resource is None and not forwarders: if existing_resource is None and not forwarders:
ansible_module.fail_json(msg='No forwarders specified.') ansible_module.fail_json(msg='No forwarders specified.')
if existing_resource is not None: if existing_resource is None:
if operation == "add":
# does not exist but should be present
# determine args
args = gen_args(forwarders, forwardpolicy,
skip_overlap_check)
# set command
command = "dnsforwardzone_add"
# enabled or disabled?
elif operation == "update":
# does not exist and is updating
# trying to update something that doesn't exist, so error
ansible_module.fail_json(
msg="dnsforwardzone '%s' not found." % (name))
elif operation == "del":
# there's nothnig to do.
continue
else: # existing_resource is not None
if state != "absent": if state != "absent":
if forwarders: if forwarders:
forwarders = list( forwarders = list(
@@ -274,66 +304,51 @@ def main():
set(existing_resource["idnsforwarders"]) set(existing_resource["idnsforwarders"])
- set(forwarders)) - set(forwarders))
if existing_resource is None and operation == "update": if operation == "add":
# does not exist and is updating # exists and should be present, has it changed?
# trying to update something that doesn't exist, so error # determine args
ansible_module.fail_json(msg="""dnsforwardzone '%s' is not args = gen_args(
valid""" % (name)) forwarders, forwardpolicy, skip_overlap_check)
elif existing_resource is None and operation == "del": if 'skip_overlap_check' in args:
# does not exists and should be absent del args['skip_overlap_check']
# enabled or disabled?
is_enabled = "IGNORE" # set command
elif existing_resource is not None and operation == "del": if not compare_args_ipa(
# exists but should be absent ansible_module, args, existing_resource):
# set command command = "dnsforwardzone_mod"
command = "dnsforwardzone_del"
args = {} elif operation == "del":
# enabled or disabled? # exists but should be absent
is_enabled = "IGNORE" # set command
elif forwarders is None: command = "dnsforwardzone_del"
# forwarders are not defined its not a delete, update state? args = {}
# enabled or disabled?
elif operation == "update":
# exists and is updating
# calculate the new forwarders and mod
args = gen_args(
forwarders, forwardpolicy, skip_overlap_check)
if "skip_overlap_check" in args:
del args['skip_overlap_check']
# command
if not compare_args_ipa(
ansible_module, args, existing_resource):
command = "dnsforwardzone_mod"
if state in ['enabled', 'disabled']:
if existing_resource is not None: if existing_resource is not None:
is_enabled = existing_resource["idnszoneactive"][0] is_enabled = existing_resource["idnszoneactive"][0]
else: else:
is_enabled = "IGNORE" ansible_module.fail_json(
elif existing_resource is not None and operation == "update": msg="dnsforwardzone '%s' not found." % (name))
# exists and is updating
# calculate the new forwarders and mod
args = gen_args(forwarders, forwardpolicy, skip_overlap_check)
if "skip_overlap_check" in args:
del args['skip_overlap_check']
# command # does the enabled state match what we want (if we care)
if not compare_args_ipa(ansible_module, args, existing_resource): if is_enabled != "IGNORE":
command = "dnsforwardzone_mod" if wants_enable and is_enabled != "TRUE":
commands.append([name, "dnsforwardzone_enable", {}])
# enabled or disabled? elif not wants_enable and is_enabled != "FALSE":
is_enabled = existing_resource["idnszoneactive"][0] commands.append([name, "dnsforwardzone_disable", {}])
elif existing_resource is None and operation == "add":
# does not exist but should be present
# determine args
args = gen_args(forwarders, forwardpolicy,
skip_overlap_check)
# set command
command = "dnsforwardzone_add"
# enabled or disabled?
is_enabled = "TRUE"
elif existing_resource is not None and operation == "add":
# exists and should be present, has it changed?
# determine args
args = gen_args(forwarders, forwardpolicy, skip_overlap_check)
if 'skip_overlap_check' in args:
del args['skip_overlap_check']
# set command
if not compare_args_ipa(ansible_module, args, existing_resource):
command = "dnsforwardzone_mod"
# enabled or disabled?
is_enabled = existing_resource["idnszoneactive"][0]
# if command is set... # if command is set...
if command is not None: if command is not None:
@@ -354,20 +369,9 @@ def main():
) )
for name, command, args in commands: for name, command, args in commands:
result = api_command(ansible_module, command, name, args) api_command(ansible_module, command, name, args)
changed = True changed = True
# does the enabled state match what we want (if we care)
if is_enabled != "IGNORE":
if wants_enable and is_enabled != "TRUE":
api_command(ansible_module, "dnsforwardzone_enable",
name, {})
changed = True
elif not wants_enable and is_enabled != "FALSE":
api_command(ansible_module, "dnsforwardzone_disable",
name, {})
changed = True
except Exception as e: except Exception as e:
ansible_module.fail_json(msg=str(e)) ansible_module.fail_json(msg=str(e))

View File

@@ -106,6 +106,22 @@
register: result register: result
failed_when: not result.changed failed_when: not result.changed
- name: change zone forward policy
ipadnsforwardzone:
ipaadmin_password: SomeADMINpassword
name: example.com
forwardpolicy: first
register: result
failed_when: not result.changed
- name: change zone forward policy, again
ipadnsforwardzone:
ipaadmin_password: SomeADMINpassword
name: example.com
forwardpolicy: first
register: result
failed_when: result.changed
- name: ensure forwardzone example.com is absent. - name: ensure forwardzone example.com is absent.
ipadnsforwardzone: ipadnsforwardzone:
ipaadmin_password: SomeADMINpassword ipaadmin_password: SomeADMINpassword
@@ -256,27 +272,15 @@
action: member action: member
skip_overlap_check: true skip_overlap_check: true
register: result register: result
failed_when: result.changed failed_when: not result.failed or "not found" not in result.msg
- name: try to create a new forwarder with disabled state - name: try to create a new forwarder with disabled state
ipadnsforwardzone:
ipaadmin_password: SomeADMINpassword
state: disabled
name: example.com
forwarders:
- ip_address: 4.4.4.4
port: 8053
skip_overlap_check: yes
register: result
failed_when: not result.changed
- name: ensure it stays disabled
ipadnsforwardzone: ipadnsforwardzone:
ipaadmin_password: SomeADMINpassword ipaadmin_password: SomeADMINpassword
name: example.com name: example.com
state: disabled state: disabled
register: result register: result
failed_when: result.changed failed_when: not result.failed or "not found" not in result.msg
- name: Ensure forwardzone is not added without forwarders, with correct message. - name: Ensure forwardzone is not added without forwarders, with correct message.
ipadnsforwardzone: ipadnsforwardzone: