To resolve NIOS race condition when ip allocated via NIOS next available ip function (#49818)

Signed-off-by: Sumit Jaiswal <sjaiswal@redhat.com>

* resolves bug 45218

Signed-off-by: Sumit Jaiswal <sjaiswal@redhat.com>

* fixing review comments
This commit is contained in:
Sumit Jaiswal 2019-01-10 09:50:27 +05:30 committed by GitHub
parent 4915920d71
commit 3784abc96a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View file

@ -57,6 +57,7 @@ NIOS_TXT_RECORD = 'record:txt'
NIOS_NSGROUP = 'nsgroup' NIOS_NSGROUP = 'nsgroup'
NIOS_IPV4_FIXED_ADDRESS = 'fixedaddress' NIOS_IPV4_FIXED_ADDRESS = 'fixedaddress'
NIOS_IPV6_FIXED_ADDRESS = 'ipv6fixedaddress' NIOS_IPV6_FIXED_ADDRESS = 'ipv6fixedaddress'
NIOS_NEXT_AVAILABLE_IP = 'func:nextavailableip'
NIOS_PROVIDER_SPEC = { NIOS_PROVIDER_SPEC = {
'host': dict(), 'host': dict(),
@ -248,6 +249,10 @@ class WapiModule(WapiBase):
modified = not self.compare_objects(current_object, proposed_object) modified = not self.compare_objects(current_object, proposed_object)
if 'extattrs' in proposed_object: if 'extattrs' in proposed_object:
proposed_object['extattrs'] = normalize_extattrs(proposed_object['extattrs']) 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 state == 'present':
if ref is None: if ref is None:
if not self.module.check_mode: 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: if obj_host_name == ref_host_name and current_ip_addr != proposed_ip_addr:
self.create_object(ib_obj_type, proposed_object) 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): def issubset(self, item, objects):
''' Checks if item is a subset of objects ''' Checks if item is a subset of objects
:args item: the subset item to validate :args item: the subset item to validate

View file

@ -39,7 +39,9 @@ options:
- dns_view - dns_view
ipv4addr: ipv4addr:
description: 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 required: true
aliases: aliases:
- ipv4 - ipv4
@ -113,6 +115,17 @@ EXAMPLES = '''
username: admin username: admin
password: admin password: admin
connection: local 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 = ''' # ''' RETURN = ''' # '''

View file

@ -58,7 +58,9 @@ options:
suboptions: suboptions:
ipv4addr: ipv4addr:
description: 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 required: true
aliases: aliases:
- address - address
@ -198,6 +200,18 @@ EXAMPLES = '''
username: admin username: admin
password: admin password: admin
connection: local 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 = ''' # ''' RETURN = ''' # '''