diff --git a/plugins/modules/azure_rm_networkinterface.py b/plugins/modules/azure_rm_networkinterface.py index beba7810b..7d6759175 100644 --- a/plugins/modules/azure_rm_networkinterface.py +++ b/plugins/modules/azure_rm_networkinterface.py @@ -479,8 +479,8 @@ # This is handled in azure_rm_common pass -from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import (AzureRMModuleBase, - azure_id_to_dict, +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import (azure_id_to_dict, normalize_location_name, format_resource_id ) @@ -560,7 +560,7 @@ def nic_to_dict(nic): ) -class AzureRMNetworkInterface(AzureRMModuleBase): +class AzureRMNetworkInterface(AzureRMModuleBaseExt): def __init__(self): @@ -634,7 +634,10 @@ def exec_module(self, **kwargs): # if application security groups set, convert to resource id format if self.ip_configurations: + primary_flag = False for config in self.ip_configurations: + if config.get('primary'): + primary_flag = True if config.get('application_security_groups'): asgs = [] for asg in config['application_security_groups']: @@ -650,10 +653,14 @@ def exec_module(self, **kwargs): asgs.append(asg_resource_id) if len(asgs) > 0: config['application_security_groups'] = asgs + if not primary_flag: + self.ip_configurations[0]['primary'] = True # If ip_confiurations is not specified then provide the default # private interface + skip_compare = False if self.state == 'present' and not self.ip_configurations: + skip_compare = True self.ip_configurations = [ dict( name='default', @@ -728,19 +735,8 @@ def exec_module(self, **kwargs): # name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name ip_configuration_result = self.construct_ip_configuration_set(results['ip_configurations']) ip_configuration_request = self.construct_ip_configuration_set(self.ip_configurations) - ip_configuration_result_name = [item['name'] for item in ip_configuration_result] - for item_request in ip_configuration_request: - if item_request['name'] not in ip_configuration_result_name: - changed = True - break - else: - for item_result in ip_configuration_result: - if len(ip_configuration_request) == 1 and len(ip_configuration_result) == 1: - item_request['primary'] = True - if item_request['name'] == item_result['name'] and item_request != item_result: - changed = True - break - + if not skip_compare and not self.default_compare({}, ip_configuration_request, ip_configuration_result, '', dict(compare=[])): + changed = True elif self.state == 'absent': self.log("CHANGED: network interface {0} exists but requested state is 'absent'".format(self.name)) changed = True @@ -898,7 +894,6 @@ def construct_ip_configuration_set(self, raw): private_ip_allocation_method=to_native(item.get('private_ip_allocation_method')), public_ip_address_name=(to_native(item.get('public_ip_address').get('name')) if item.get('public_ip_address') else to_native(item.get('public_ip_address_name'))), - primary=item.get('primary'), load_balancer_backend_address_pools=(set([to_native(self.backend_addr_pool_id(id)) for id in item.get('load_balancer_backend_address_pools')]) if item.get('load_balancer_backend_address_pools') else None), @@ -907,7 +902,10 @@ def construct_ip_configuration_set(self, raw): if item.get('application_gateway_backend_address_pools') else None), application_security_groups=(set([to_native(asg_id) for asg_id in item.get('application_security_groups')]) if item.get('application_security_groups') else None), - name=to_native(item.get('name')) + name=to_native(item.get('name')), + private_ip_address=to_native(item.get('private_ip_address')), + private_ip_address_version=to_native(item.get('private_ip_address_version')), + public_ip_allocation_method=to_native(item.get('public_ip_allocation_method', 'Dynamic')) ) for item in raw] return configurations diff --git a/tests/integration/targets/azure_rm_networkinterface/tasks/main.yml b/tests/integration/targets/azure_rm_networkinterface/tasks/main.yml index 4955230f2..02d8c12d7 100644 --- a/tests/integration/targets/azure_rm_networkinterface/tasks/main.yml +++ b/tests/integration/targets/azure_rm_networkinterface/tasks/main.yml @@ -307,19 +307,21 @@ private_ip_address: "{{ facts.networkinterfaces[0].ip_configurations[0].private_ip_address }}" private_ip_allocation_method: "{{ facts.networkinterfaces[0].ip_configurations[0].private_ip_allocation_method }}" primary: "{{ facts.networkinterfaces[0].ip_configurations[0].primary }}" - - name: ipconfig1 - public_ip_name: "tn{{ rpfx }}" - load_balancer_backend_address_pools: - - "{{ lb.state.backend_address_pools[0].id }}" - - name: backendaddrpool1 - load_balancer: "lb{{ rpfx }}" - name: ipconfig2 + private_ip_address: "{{ output.state.ip_configurations[1].private_ip_address }}" public_ip_name: "tn{{ rpfx }}v6" private_ip_address_version: 'IPv6' load_balancer_backend_address_pools: - "{{ lb.state.backend_address_pools[2].id }}" - name: backendaddrpool2 load_balancer: "lb{{ rpfx }}" + - name: ipconfig1 + public_ip_name: "tn{{ rpfx }}" + private_ip_address: "{{ output.state.ip_configurations[2].private_ip_address }}" + load_balancer_backend_address_pools: + - "{{ lb.state.backend_address_pools[0].id }}" + - name: backendaddrpool1 + load_balancer: "lb{{ rpfx }}" register: output - name: Assert the NIC no change @@ -481,6 +483,8 @@ create_with_security_group: true ip_configurations: - name: ipconfig1 + private_ip_address: "{{ output.state.ip_configurations[0].private_ip_address }}" + private_ip_allocation_method: "{{ output.state.ip_configurations[0].private_ip_allocation_method }}" application_security_groups: - "{{ asg.id }}" - "{{ applicationsecuritygroup_name1 }}" @@ -615,6 +619,8 @@ ip_configurations: - name: "default" primary: true + private_ip_address: "{{ output.state.ip_configurations[0].private_ip_address }}" + private_ip_allocation_method: "{{ output.state.ip_configurations[0].private_ip_allocation_method }}" application_gateway_backend_address_pools: - name: "test_backend_address_pool" application_gateway: "appgateway{{ rpfx }}"