Backport: Fixes some NIC bugs (#39213) (#39266)

* cherry-pick 1ecd2c9dcdb6df9baada84d220dddda3423137c2

* Create azure_rm_networkinterface-fix-creation-ignore-nsg-name.yaml
This commit is contained in:
Yuwei Zhou 2018-05-10 17:43:26 +08:00 committed by Jordan Borean
parent fb669f76fb
commit 3de89da6f0
4 changed files with 52 additions and 26 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- azure_rm_networkinterface - Network interface can attach an existing NSG or create a new NSG with specified name in Ansible v2.5.0.

View file

@ -767,8 +767,15 @@ class AzureRMModuleBase(object):
priority += 1 priority += 1
rule_name = "Rule_{0}".format(priority) rule_name = "Rule_{0}".format(priority)
parameters.security_rules.append( parameters.security_rules.append(
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', source_port_range='*', self.network_models.SecurityRule(protocol='Tcp',
destination_port_range=str(port), priority=priority, name=rule_name) source_address_prefix='*',
destination_address_prefix='*',
access='Allow',
direction='Inbound',
source_port_range='*',
destination_port_range=str(port),
priority=priority,
name=rule_name)
) )
self.log('Creating default security group {0}'.format(security_group_name)) self.log('Creating default security group {0}'.format(security_group_name))

View file

@ -2,6 +2,7 @@
# #
# Copyright (c) 2016 Matt Davis, <mdavis@ansible.com> # Copyright (c) 2016 Matt Davis, <mdavis@ansible.com>
# Chris Houseknecht, <house@redhat.com> # Chris Houseknecht, <house@redhat.com>
# Yuwei ZHou, <yuwzho@microsoft.com>
# #
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
@ -168,6 +169,7 @@ extends_documentation_fragment:
author: author:
- "Chris Houseknecht (@chouseknecht)" - "Chris Houseknecht (@chouseknecht)"
- "Matt Davis (@nitzmahone)" - "Matt Davis (@nitzmahone)"
- "Yuwei Zhou (@yuwzho)"
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -260,7 +262,8 @@ state:
"id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/publicIPAddresses/publicip001", "id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/publicIPAddresses/publicip001",
"name": "publicip001" "name": "publicip001"
}, },
"subnet": {} "subnet": {},
"load_balancer_backend_address_pools": []
}], }],
"location": "eastus2", "location": "eastus2",
"mac_address": null, "mac_address": null,
@ -334,16 +337,6 @@ def nic_to_dict(nic):
) )
def construct_ip_configuration_set(raw):
configurations = [str(dict(
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'),
name=to_native(item.get('name'))
)) for item in raw]
return set(configurations)
ip_configuration_spec = dict( ip_configuration_spec = dict(
name=dict(type='str', required=True), name=dict(type='str', required=True),
private_ip_address=dict(type='str'), private_ip_address=dict(type='str'),
@ -427,6 +420,9 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
virtual_network_name = virtual_network_dict.get('name') virtual_network_name = virtual_network_dict.get('name')
virtual_network_resource_group = virtual_network_dict.get('resource_group', self.resource_group) virtual_network_resource_group = virtual_network_dict.get('resource_group', self.resource_group)
# if not set the security group name, use nic name for default
self.security_group_name = self.security_group_name or self.name
if self.state == 'present' and not self.ip_configurations: if self.state == 'present' and not self.ip_configurations:
# construct the ip_configurations array for compatiable # construct the ip_configurations array for compatiable
self.deprecate('Setting ip_configuration flatten is deprecated and will be removed.' self.deprecate('Setting ip_configuration flatten is deprecated and will be removed.'
@ -437,7 +433,8 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
private_ip_allocation_method=self.private_ip_allocation_method, private_ip_allocation_method=self.private_ip_allocation_method,
public_ip_address_name=self.public_ip_address_name if self.public_ip else None, public_ip_address_name=self.public_ip_address_name if self.public_ip else None,
public_ip_allocation_method=self.public_ip_allocation_method, public_ip_allocation_method=self.public_ip_allocation_method,
name='default' name='default',
primary=True
) )
] ]
@ -457,11 +454,10 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
if update_tags: if update_tags:
changed = True changed = True
if self.security_group_name: nsg = self.get_security_group(self.security_group_name)
nsg = self.get_security_group(self.security_group_name) if nsg and results.get('network_security_group') and results['network_security_group'].get('id') != nsg.id:
if nsg and results['network_security_group'].get('id') != nsg.id: self.log("CHANGED: network interface {0} network security group".format(self.name))
self.log("CHANGED: network interface {0} network security group".format(self.name)) changed = True
changed = True
if results['ip_configurations'][0]['subnet']['virtual_network_name'] != virtual_network_name: if results['ip_configurations'][0]['subnet']['virtual_network_name'] != virtual_network_name:
self.log("CHANGED: network interface {0} virtual network name".format(self.name)) self.log("CHANGED: network interface {0} virtual network name".format(self.name))
@ -479,8 +475,8 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
# construct two set with the same structure and then compare # construct two set with the same structure and then compare
# the list should contains: # the list should contains:
# name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name # name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name
ip_configuration_result = construct_ip_configuration_set(results['ip_configurations']) ip_configuration_result = self.construct_ip_configuration_set(results['ip_configurations'])
ip_configuration_request = construct_ip_configuration_set(self.ip_configurations) ip_configuration_request = self.construct_ip_configuration_set(self.ip_configurations)
if ip_configuration_result != ip_configuration_request: if ip_configuration_result != ip_configuration_request:
self.log("CHANGED: network interface {0} ip configurations".format(self.name)) self.log("CHANGED: network interface {0} ip configurations".format(self.name))
changed = True changed = True
@ -516,7 +512,11 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
) for ip_config in self.ip_configurations ) for ip_config in self.ip_configurations
] ]
nsg = nsg or self.create_default_securitygroup(self.resource_group, self.location, self.name, self.os_type, self.open_ports) nsg = self.create_default_securitygroup(self.resource_group,
self.location,
self.security_group_name,
self.os_type,
self.open_ports)
self.log('Creating or updating network interface {0}'.format(self.name)) self.log('Creating or updating network interface {0}'.format(self.name))
nic = self.network_models.NetworkInterface( nic = self.network_models.NetworkInterface(
id=results['id'] if results else None, id=results['id'] if results else None,
@ -590,6 +590,16 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
except Exception as exc: except Exception as exc:
return None return None
def construct_ip_configuration_set(self, raw):
configurations = [str(dict(
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'),
name=to_native(item.get('name'))
)) for item in raw]
return set(configurations)
def main(): def main():
AzureRMNetworkInterface() AzureRMNetworkInterface()

View file

@ -12,6 +12,11 @@
address_prefix: "10.10.0.0/24" address_prefix: "10.10.0.0/24"
virtual_network: testnic001 virtual_network: testnic001
- name: create public ip
azure_rm_publicipaddress:
name: ansiblepip3
resource_group: '{{ resource_group }}'
- name: Create NIC (check mode) - name: Create NIC (check mode)
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
@ -121,8 +126,8 @@
virtual_network: "{{ vn.state.id }}" virtual_network: "{{ vn.state.id }}"
subnet: testnic001 subnet: testnic001
ip_configurations: ip_configurations:
- name: ipconfig-add - name: ipconfig1
public_ip_name: testnic002 public_ip_name: testnic003
- name: default - name: default
public_ip_name: testnic001 public_ip_name: testnic001
public_ip_allocation_method: Static public_ip_allocation_method: Static
@ -138,7 +143,6 @@
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: testnic001noip name: testnic001noip
security_group: testnic001
virtual_network: "{{ vn.state.id }}" virtual_network: "{{ vn.state.id }}"
subnet: testnic001 subnet: testnic001
ip_configurations: ip_configurations:
@ -165,8 +169,11 @@
- name: Delete the NIC - name: Delete the NIC
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: testnic001 name: "{{ item }}"
state: absent state: absent
with_items:
- testnic001
- testnic001noip
register: output register: output
- assert: - assert: