From 735351d32f5a4ebcfb2c16bbfd01594ff0e7cb18 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 27 Jul 2024 17:56:57 +0800 Subject: [PATCH] Support wapi function call as payload of ipv4addr when creating a host record --- plugins/module_utils/api.py | 21 +++++++++++++++++++++ plugins/modules/nios_host_record.py | 27 ++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index c95c8df6..a0ed03f0 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -439,6 +439,8 @@ def run(self, ib_obj_type, ib_spec): # Checks if nios_next_ip param is passed in ipv4addrs/ipv4addr args proposed_object = self.check_if_nios_next_ip_exists(proposed_object) + # Checks if a wapi function call is the payload of ipv4addrs/ipv4addr args + proposed_object = self.check_if_ipv4addr_object_function_exists(proposed_object) if state == 'present': if ref is None: @@ -548,6 +550,23 @@ def check_if_nios_next_ip_exists(self, proposed_object): return proposed_object + def check_if_ipv4addr_object_function_exists(self, proposed_object): + ''' Check if _object_function argument is passed in ipaddr + while creating host record, if yes then format proposed object ipv4addrs + to create hostrecord with next available ip in one call to avoid any race condition + This format support the exclude parameters unlike nextavailableip + It also supports DHCP ranges, dynamic selection of ranges and networks ''' + + if 'ipv4addrs' in proposed_object: + for ipv4addr_payload in proposed_object['ipv4addrs']: + if '_object_function' in ipv4addr_payload['ipv4addr']: + ipv4addr_payload['ipv4addr'] = check_type_dict(ipv4addr_payload['ipv4addr']) + elif 'ipv4addr' in proposed_object: + if '_object_function' in proposed_object['ipv4addr']: + proposed_object['ipv4addr'] = check_type_dict(proposed_object['ipv4addr']) + + return proposed_object + def check_for_new_ipv4addr(self, proposed_object): ''' Checks if new_ipv4addr parameter is passed in the argument while updating the record with new ipv4addr with static allocation''' @@ -592,6 +611,8 @@ def check_next_ip_status(self, obj_filter): if 'ipv4addr' in obj_filter: if 'nios_next_ip' in obj_filter['ipv4addr']: return True + if '_object_function' in obj_filter['ipv4addr'] and check_type_dict(obj_filter['ipv4addr'])['_object_function'] == 'next_available_ip': + return True return False def issubset(self, item, objects): diff --git a/plugins/modules/nios_host_record.py b/plugins/modules/nios_host_record.py index 31f7f513..8c85a07c 100644 --- a/plugins/modules/nios_host_record.py +++ b/plugins/modules/nios_host_record.py @@ -59,7 +59,9 @@ description: - Configures the IPv4 address for the host record. Users can dynamically allocate ipv4 address to host record by passing dictionary containing, - I(nios_next_ip) and I(CIDR network range). If user wants to add or + I(nios_next_ip) and I(CIDR network range). It supports _object_function + calls to dynamic select an ipv4address from a network/dhcp-range and + exclude a list of IPs from the selection. If user wants to add or remove the ipv4 address from existing record, I(add/remove) params need to be used. See examples. type: str @@ -258,6 +260,29 @@ password: admin connection: local +- name: > + Dynamically add host record to next available ip in + a network and excluding a list of IPs + see https://ipam.illinois.edu/wapidoc/objects/record.host_ipv4addr.html + infoblox.nios_modules.nios_host_record: + name: host.ansible.com + ipv4: + - address: + _object_function: next_available_ip + _parameters: + exclude: ['192.168.10.1', '192.168.10.2', '192.168.10.3'], + _result_fields: ips + _object: network + _object_parameters: + network: 192.168.10.0/24 + comment: this is a test comment + state: present + provider: + host: "{{ inventory_hostname_short }}" + username: admin + password: admin + connection: local + - name: Add ip to host record infoblox.nios_modules.nios_host_record: name: host.ansible.com