From 3784abc96a8f468cb9d356524bb3f16fafafe61f Mon Sep 17 00:00:00 2001 From: Sumit Jaiswal Date: Thu, 10 Jan 2019 09:50:27 +0530 Subject: [PATCH] To resolve NIOS race condition when ip allocated via NIOS next available ip function (#49818) Signed-off-by: Sumit Jaiswal * resolves bug 45218 Signed-off-by: Sumit Jaiswal * fixing review comments --- .../module_utils/net_tools/nios/api.py | 22 +++++++++++++++++++ .../modules/net_tools/nios/nios_a_record.py | 15 ++++++++++++- .../net_tools/nios/nios_host_record.py | 16 +++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/net_tools/nios/api.py b/lib/ansible/module_utils/net_tools/nios/api.py index 072b7944706..7e452edba3c 100644 --- a/lib/ansible/module_utils/net_tools/nios/api.py +++ b/lib/ansible/module_utils/net_tools/nios/api.py @@ -57,6 +57,7 @@ NIOS_TXT_RECORD = 'record:txt' NIOS_NSGROUP = 'nsgroup' NIOS_IPV4_FIXED_ADDRESS = 'fixedaddress' NIOS_IPV6_FIXED_ADDRESS = 'ipv6fixedaddress' +NIOS_NEXT_AVAILABLE_IP = 'func:nextavailableip' NIOS_PROVIDER_SPEC = { 'host': dict(), @@ -248,6 +249,10 @@ class WapiModule(WapiBase): modified = not self.compare_objects(current_object, proposed_object) if 'extattrs' in proposed_object: proposed_object['extattrs'] = normalize_extattrs(proposed_object['extattrs']) + + # Checks if nios_next_ip param is passed in ipv4addrs/ipv4addr args + proposed_object = self.check_if_nios_next_ip_exists(proposed_object) + if state == 'present': if ref is None: if not self.module.check_mode: @@ -296,6 +301,23 @@ class WapiModule(WapiBase): if obj_host_name == ref_host_name and current_ip_addr != proposed_ip_addr: self.create_object(ib_obj_type, proposed_object) + def check_if_nios_next_ip_exists(self, proposed_object): + ''' Check if nios_next_ip argument is passed in ipaddr while creating + host record, if yes then format proposed object ipv4addrs and pass + func:nextavailableip and ipaddr range to create hostrecord with next + available ip in one call to avoid any race condition ''' + + if 'ipv4addrs' in proposed_object: + if 'nios_next_ip' in proposed_object['ipv4addrs'][0]['ipv4addr']: + ip_range = self.module._check_type_dict(proposed_object['ipv4addrs'][0]['ipv4addr'])['nios_next_ip'] + proposed_object['ipv4addrs'][0]['ipv4addr'] = NIOS_NEXT_AVAILABLE_IP + ':' + ip_range + elif 'ipv4addr' in proposed_object: + if 'nios_next_ip' in proposed_object['ipv4addr']: + ip_range = self.module._check_type_dict(proposed_object['ipv4addr'])['nios_next_ip'] + proposed_object['ipv4addr'] = NIOS_NEXT_AVAILABLE_IP + ':' + ip_range + + return proposed_object + def issubset(self, item, objects): ''' Checks if item is a subset of objects :args item: the subset item to validate diff --git a/lib/ansible/modules/net_tools/nios/nios_a_record.py b/lib/ansible/modules/net_tools/nios/nios_a_record.py index 8d7564c74c3..312346dac24 100644 --- a/lib/ansible/modules/net_tools/nios/nios_a_record.py +++ b/lib/ansible/modules/net_tools/nios/nios_a_record.py @@ -39,7 +39,9 @@ options: - dns_view ipv4addr: description: - - Configures the IPv4 address for this A record. + - Configures the IPv4 address for this A record. Users can dynamically + allocate ipv4 address to A record by passing dictionary containing, + I(nios_next_ip) and I(CIDR network range). See example required: true aliases: - ipv4 @@ -113,6 +115,17 @@ EXAMPLES = ''' username: admin password: admin connection: local + +- name: dynamically add a record to next available ip + nios_a_record: + name: a.ansible.com + ipv4: {nios_next_ip: 192.168.10.0/24} + state: present + provider: + host: "{{ inventory_hostname_short }}" + username: admin + password: admin + connection: local ''' RETURN = ''' # ''' diff --git a/lib/ansible/modules/net_tools/nios/nios_host_record.py b/lib/ansible/modules/net_tools/nios/nios_host_record.py index f2ab189af4f..41b2fb32153 100644 --- a/lib/ansible/modules/net_tools/nios/nios_host_record.py +++ b/lib/ansible/modules/net_tools/nios/nios_host_record.py @@ -58,7 +58,9 @@ options: suboptions: ipv4addr: description: - - Configures the IPv4 address for the host record + - 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). See example required: true aliases: - address @@ -198,6 +200,18 @@ EXAMPLES = ''' username: admin password: admin connection: local +- name: dynamically add host record to next available ip + nios_host_record: + name: host.ansible.com + ipv4: + - address: {nios_next_ip: 192.168.10.0/24} + comment: this is a test comment + state: present + provider: + host: "{{ inventory_hostname_short }}" + username: admin + password: admin + connection: local ''' RETURN = ''' # '''