From 05fa31e14865cc9b46a7579f4ea8a1a0e10d880a Mon Sep 17 00:00:00 2001 From: Jeenitkumar Khatri Date: Thu, 26 Sep 2024 17:09:38 +0530 Subject: [PATCH] [FIX] Updated default value of use_for_ea_inheritance to False and updated validations. --- plugins/module_utils/api.py | 13 +++++++++++-- plugins/modules/nios_host_record.py | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 39a698cd..58fdf62c 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -438,6 +438,8 @@ def run(self, ib_obj_type, ib_spec): check_remove = [] if (ib_obj_type == NIOS_HOST_RECORD): + if sum(addr.get('use_for_ea_inheritance', False) for addr in proposed_object['ipv4addrs']) > 1: + raise AnsibleError('Only one address allowed to be used for extensible attributes inheritance') # this check is for idempotency, as if the same ip address shall be passed # add param will be removed, and same exists true for remove case as well. if 'ipv4addrs' in [current_object and proposed_object]: @@ -510,7 +512,13 @@ def run(self, ib_obj_type, ib_spec): result['changed'] = True if not self.module.check_mode and res is None: proposed_object = self.on_update(proposed_object, ib_spec) - res = self.update_object(ref, proposed_object) + if ib_obj_type == NIOS_HOST_RECORD: + # Remove 'use_for_ea_inheritance' from each dictionary in 'ipv4addrs' + update_proposed = {**proposed_object, 'ipv4addrs': [ + {k: v for k, v in addr.items() if k != 'use_for_ea_inheritance'} for addr in proposed_object['ipv4addrs']]} + res = self.update_object(ref, update_proposed) + else: + res = self.update_object(ref, proposed_object) result['changed'] = True if ib_obj_type == NIOS_HOST_RECORD and res: @@ -521,7 +529,8 @@ def run(self, ib_obj_type, ib_spec): if host_ref: # Create a dictionary for quick lookups ref_dict = {obj['ipv4addr']: obj['_ref'] for obj in host_ref['ipv4addrs']} - for proposed in proposed_object['ipv4addrs']: + sorted_ipv4addrs = sorted(proposed_object['ipv4addrs'], key=lambda x: x.get('use_for_ea_inheritance', False)) + for proposed in sorted_ipv4addrs: ipv4addr = proposed['ipv4addr'] if ipv4addr in ref_dict and 'use_for_ea_inheritance' in proposed: self.update_object(ref_dict[ipv4addr], {'use_for_ea_inheritance': proposed['use_for_ea_inheritance']}) diff --git a/plugins/modules/nios_host_record.py b/plugins/modules/nios_host_record.py index cbe9274c..38185d31 100644 --- a/plugins/modules/nios_host_record.py +++ b/plugins/modules/nios_host_record.py @@ -64,9 +64,9 @@ use_for_ea_inheritance: version_added: "1.7.0" description: - - When use_for_ea_inheritance is True, the EA is inherited from Host address. The default value is True. + - When use_for_ea_inheritance is True, the EA is inherited from Host address. The default value is False. type: bool - default: true + default: false required: false ipv4addr: description: @@ -378,7 +378,7 @@ def main(): add=dict(type='bool', required=False), use_nextserver=dict(type='bool', required=False, aliases=['use_pxe']), nextserver=dict(required=False, aliases=['pxe']), - use_for_ea_inheritance=dict(type='bool', required=False, default=True), + use_for_ea_inheritance=dict(type='bool', required=False, default=False), remove=dict(type='bool', required=False) )