From 9487815a26645994e7ff81cb4043d6f38eced8e0 Mon Sep 17 00:00:00 2001 From: Yuwei Zhou Date: Fri, 11 Jan 2019 16:33:13 +0800 Subject: [PATCH] azure virtualmachine scaleset supports loadbalancer update (#50132) --- .../azure/azure_rm_virtualmachine_scaleset.py | 42 ++++++++++++++----- .../tasks/main.yml | 22 +++++++++- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py index 14e0c841209..2a63174b30a 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py @@ -485,6 +485,10 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): subnet = None image_reference = None custom_image = False + load_balancer_backend_address_pools = None + load_balancer_inbound_nat_pools = None + load_balancer = None + support_lb_change = True resource_group = self.get_resource_group(self.resource_group) if not self.location: @@ -539,6 +543,15 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): disable_ssh_password = not self.ssh_password_enabled + if self.load_balancer: + load_balancer = self.get_load_balancer(self.load_balancer) + load_balancer_backend_address_pools = ([self.compute_models.SubResource(id=resource.id) + for resource in load_balancer.backend_address_pools] + if load_balancer.backend_address_pools else None) + load_balancer_inbound_nat_pools = ([self.compute_models.SubResource(id=resource.id) + for resource in load_balancer.inbound_nat_pools] + if load_balancer.inbound_nat_pools else None) + try: self.log("Fetching virtual machine scale set {0}".format(self.name)) vmss = self.compute_client.virtual_machine_scale_sets.get(self.resource_group, self.name) @@ -599,6 +612,18 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): changed = True vmss_dict['zones'] = self.zones + nicConfigs = vmss_dict['properties']['virtualMachineProfile']['networkProfile']['networkInterfaceConfigurations'] + backend_address_pool = nicConfigs[0]['properties']['ipConfigurations'][0]['properties'].get('loadBalancerBackendAddressPools', []) + if (len(nicConfigs) != 1 or len(backend_address_pool) != 1): + support_lb_change = False # Currently not support for the vmss contains more than one loadbalancer + self.module.warn('Updating more than one load balancer on VMSS is currently not supported') + else: + load_balancer_id = "{0}/".format(load_balancer.id) if load_balancer else None + backend_address_pool_id = backend_address_pool[0].get('id') + if bool(load_balancer_id) != bool(backend_address_pool_id) or not backend_address_pool_id.startswith(load_balancer_id): + differences.append('load_balancer') + changed = True + self.differences = differences elif self.state == 'absent': @@ -641,17 +666,6 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): if self.subnet_name: subnet = self.get_subnet(self.virtual_network_name, self.subnet_name) - load_balancer_backend_address_pools = None - load_balancer_inbound_nat_pools = None - if self.load_balancer: - load_balancer = self.get_load_balancer(self.load_balancer) - load_balancer_backend_address_pools = ([self.compute_models.SubResource(id=resource.id) - for resource in load_balancer.backend_address_pools] - if load_balancer.backend_address_pools else None) - load_balancer_inbound_nat_pools = ([self.compute_models.SubResource(id=resource.id) - for resource in load_balancer.inbound_nat_pools] - if load_balancer.inbound_nat_pools else None) - if not self.short_hostname: self.short_hostname = self.name @@ -764,6 +778,12 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): vmss_resource.sku.capacity = self.capacity vmss_resource.overprovision = self.overprovision + if support_lb_change: + vmss_resource.virtual_machine_profile.network_profile.network_interface_configurations[0] \ + .ip_configurations[0].load_balancer_backend_address_pools = load_balancer_backend_address_pools + vmss_resource.virtual_machine_profile.network_profile.network_interface_configurations[0] \ + .ip_configurations[0].load_balancer_inbound_nat_pools = load_balancer_inbound_nat_pools + if self.data_disks is not None: data_disks = [] for data_disk in self.data_disks: diff --git a/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml b/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml index a31ce990cd5..78169a0960f 100644 --- a/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml +++ b/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml @@ -28,6 +28,18 @@ name: testLB public_ip_address_name: testPublicIP +- name: Create public IP address 1 + azure_rm_publicipaddress: + resource_group: "{{ resource_group }}" + allocation_method: Static + name: testPublicIP1 + +- name: Create load balancer 1 + azure_rm_loadbalancer: + resource_group: "{{ resource_group }}" + name: testLB1 + public_ip_address_name: testPublicIP1 + - name: Create network security group within same resource group of VMSS. azure_rm_securitygroup: resource_group: "{{ resource_group }}" @@ -136,6 +148,7 @@ virtual_network_name: testVnet subnet_name: testSubnet upgrade_policy: Manual + load_balancer: testLB tier: Standard managed_disk_type: Standard_LRS os_disk_caching: ReadWrite @@ -155,7 +168,7 @@ assert: that: results.changed -- name: Create VMSS -- test upgrade_policy idempotence +- name: Create VMSS -- test upgrade_policy idempotence and load balancer azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }} @@ -167,6 +180,7 @@ virtual_network_name: testVnet subnet_name: testSubnet upgrade_policy: Automatic + load_balancer: testLB1 tier: Standard managed_disk_type: Standard_LRS os_disk_caching: ReadWrite @@ -180,7 +194,6 @@ disk_size_gb: 64 caching: ReadWrite managed_disk_type: Standard_LRS - check_mode: yes register: results - name: Assert that VMSS was created @@ -194,6 +207,10 @@ format: curated register: output_scaleset +- assert: + that: + - output_scaleset.vmss[0].load_balancer == "testLB1" + - name: Retrieve scaleset VMs facts azure_rm_virtualmachinescalesetinstance_facts: resource_group: "{{ resource_group }}" @@ -228,6 +245,7 @@ virtual_network_name: "{{ body.virtual_network_name }}" subnet_name: "{{ body.subnet_name }}" upgrade_policy: "{{ body.upgrade_policy }}" + load_balancer: "{{ body.load_balancer }}" tier: "{{ body.tier }}" managed_disk_type: "{{ body.managed_disk_type }}" os_disk_caching: "{{ body.os_disk_caching }}"