Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for custom data in vmss #51380

Merged
merged 12 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@
- 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
Expand Down Expand Up @@ -365,6 +373,7 @@

import random
import re
import base64

try:
from msrestazure.azure_exceptions import CloudError
Expand All @@ -375,6 +384,7 @@
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'
Expand Down Expand Up @@ -414,7 +424,8 @@ def __init__(self):
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
Expand Down Expand Up @@ -445,6 +456,7 @@ def __init__(self):
self.security_group = None
self.overprovision = None
self.zones = None
self.custom_data = None

required_if = [
('state', 'present', [
Expand Down Expand Up @@ -498,6 +510,9 @@ def exec_module(self, **kwargs):
# Set default location
self.location = resource_group.location

if self.custom_data:
self.custom_data = to_native(base64.b64encode(to_bytes(self.custom_data)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from code, it's script content, but i think url or filename should be supported for long script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's limited to 16384 bytes.
users can use Ansible lookups to read the content from file or uri.
i don't think we should make it more complicated here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense


if self.state == 'present':
# Verify parameters and resolve any defaults

Expand Down Expand Up @@ -627,6 +642,12 @@ def exec_module(self, **kwargs):
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':
Expand Down Expand Up @@ -698,6 +719,7 @@ def exec_module(self, **kwargs):
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
tier: Standard
managed_disk_type: Standard_LRS
os_disk_caching: ReadWrite
custom_data: "#cloud-config"
image:
offer: CoreOS
publisher: CoreOS
Expand Down Expand Up @@ -184,6 +185,7 @@
tier: Standard
managed_disk_type: Standard_LRS
os_disk_caching: ReadWrite
custom_data: "#cloud-config"
image:
offer: CoreOS
publisher: CoreOS
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down