From f0f23378dbb74b707d3ac30fbe2aed4b68e0d28d Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Wed, 30 Jan 2019 12:09:29 +0800 Subject: [PATCH] support for custom data in vmss (#51380) --- .../azure/azure_rm_virtualmachine_scaleset.py | 24 ++++++++++++++++++- .../tasks/main.yml | 12 ++++++---- 2 files changed, 30 insertions(+), 6 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 38071f81748..a422314c2d1 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset.py @@ -206,6 +206,14 @@ options: - A list of Availability Zones for your virtual machine scale set type: list version_added: "2.8" + custom_data: + description: + - Data which is made available to the virtual machine and used by e.g., cloud-init. + - Many images in the marketplace are not cloud-init ready. Thus, data + sent to I(custom_data) would be ignored. If the image you are attempting to use is not listed in + U(https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#cloud-init-overview), + follow these steps U(https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cloudinit-prepare-custom-image). + version_added: "2.8" extends_documentation_fragment: - azure @@ -365,6 +373,7 @@ azure_vmss: import random import re +import base64 try: from msrestazure.azure_exceptions import CloudError @@ -375,6 +384,7 @@ except ImportError: pass from ansible.module_utils.azure_rm_common import AzureRMModuleBase, azure_id_to_dict, format_resource_id +from ansible.module_utils.basic import to_native, to_bytes AZURE_OBJECT_CLASS = 'VirtualMachineScaleSet' @@ -414,7 +424,8 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): enable_accelerated_networking=dict(type='bool'), security_group=dict(type='raw', aliases=['security_group_name']), overprovision=dict(type='bool', default=True), - zones=dict(type='list') + zones=dict(type='list'), + custom_data=dict(type='str') ) self.resource_group = None @@ -445,6 +456,7 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): self.security_group = None self.overprovision = None self.zones = None + self.custom_data = None required_if = [ ('state', 'present', [ @@ -498,6 +510,9 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): # Set default location self.location = resource_group.location + if self.custom_data: + self.custom_data = to_native(base64.b64encode(to_bytes(self.custom_data))) + if self.state == 'present': # Verify parameters and resolve any defaults @@ -627,6 +642,12 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): differences.append('load_balancer') changed = True + if self.custom_data: + if self.custom_data != vmss_dict['properties']['virtualMachineProfile']['osProfile'].get('customData'): + differences.append('custom_data') + changed = True + vmss_dict['properties']['virtualMachineProfile']['osProfile']['customData'] = self.custom_data + self.differences = differences elif self.state == 'absent': @@ -698,6 +719,7 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): os_profile=self.compute_models.VirtualMachineScaleSetOSProfile( admin_username=self.admin_username, computer_name_prefix=self.short_hostname, + custom_data=self.custom_data ), storage_profile=self.compute_models.VirtualMachineScaleSetStorageProfile( os_disk=self.compute_models.VirtualMachineScaleSetOSDisk( 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 9c66113722f..053785a196a 100644 --- a/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml +++ b/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml @@ -152,6 +152,7 @@ tier: Standard managed_disk_type: Standard_LRS os_disk_caching: ReadWrite + custom_data: "#cloud-config" image: offer: CoreOS publisher: CoreOS @@ -184,6 +185,7 @@ tier: Standard managed_disk_type: Standard_LRS os_disk_caching: ReadWrite + custom_data: "#cloud-config" image: offer: CoreOS publisher: CoreOS @@ -353,7 +355,7 @@ resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }}2 vm_size: Standard_D3_v2 - capacity: 1 + capacity: 0 virtual_network_name: testVnet subnet_name: testSubnet admin_username: testuser @@ -377,7 +379,7 @@ resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }}2 vm_size: Standard_D3_v2 - capacity: 1 + capacity: 0 virtual_network_name: testVnet subnet_name: testSubnet admin_username: testuser @@ -403,7 +405,7 @@ resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }}2 vm_size: Standard_D3_v2 - capacity: 1 + capacity: 0 virtual_network_name: testVnet subnet_name: testSubnet admin_username: testuser @@ -427,7 +429,7 @@ resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }}2 vm_size: Standard_D3_v2 - capacity: 1 + capacity: 0 virtual_network_name: testVnet subnet_name: testSubnet admin_username: testuser @@ -451,7 +453,7 @@ resource_group: "{{ resource_group }}" name: testVMSS{{ rpfx }}2 vm_size: Standard_B1s - capacity: 1 + capacity: 0 virtual_network_name: testVnet subnet_name: testSubnet admin_username: testuser