Azure create virtual machine with an Availability Set associate (#32535)
* add availability set for vm * add test * add import * fix * add test assert and output * fix lint and version added * add null check * fix mirror * fix mirror
This commit is contained in:
parent
3129cbb9e1
commit
e4194b20db
2 changed files with 43 additions and 1 deletions
|
@ -106,6 +106,11 @@ options:
|
||||||
for by C(name).'
|
for by C(name).'
|
||||||
- Custom image support was added in Ansible 2.5
|
- Custom image support was added in Ansible 2.5
|
||||||
required: true
|
required: true
|
||||||
|
availability_set:
|
||||||
|
description:
|
||||||
|
- Name or ID of an existing availability set to add the VM to. The availability_set should be in the same resource group as VM.
|
||||||
|
default: null
|
||||||
|
version_added: "2.5"
|
||||||
storage_account_name:
|
storage_account_name:
|
||||||
description:
|
description:
|
||||||
- Name of an existing storage account that supports creation of VHD blobs. If not specified for a new VM,
|
- Name of an existing storage account that supports creation of VHD blobs. If not specified for a new VM,
|
||||||
|
@ -448,6 +453,9 @@ azure_vm:
|
||||||
type: complex
|
type: complex
|
||||||
contains: {
|
contains: {
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"availabilitySet": {
|
||||||
|
"id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Compute/availabilitySets/MYAVAILABILITYSET"
|
||||||
|
},
|
||||||
"hardwareProfile": {
|
"hardwareProfile": {
|
||||||
"vmSize": "Standard_D1"
|
"vmSize": "Standard_D1"
|
||||||
},
|
},
|
||||||
|
@ -588,13 +596,14 @@ import re
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from msrestazure.azure_exceptions import CloudError
|
from msrestazure.azure_exceptions import CloudError
|
||||||
|
from msrestazure.tools import parse_resource_id
|
||||||
from azure.mgmt.compute.models import NetworkInterfaceReference, \
|
from azure.mgmt.compute.models import NetworkInterfaceReference, \
|
||||||
VirtualMachine, HardwareProfile, \
|
VirtualMachine, HardwareProfile, \
|
||||||
StorageProfile, OSProfile, OSDisk, DataDisk, \
|
StorageProfile, OSProfile, OSDisk, DataDisk, \
|
||||||
VirtualHardDisk, ManagedDiskParameters, \
|
VirtualHardDisk, ManagedDiskParameters, \
|
||||||
ImageReference, NetworkProfile, LinuxConfiguration, \
|
ImageReference, NetworkProfile, LinuxConfiguration, \
|
||||||
SshConfiguration, SshPublicKey, VirtualMachineSizeTypes, \
|
SshConfiguration, SshPublicKey, VirtualMachineSizeTypes, \
|
||||||
DiskCreateOptionTypes, Plan
|
DiskCreateOptionTypes, Plan, SubResource
|
||||||
from azure.mgmt.network.models import PublicIPAddress, NetworkSecurityGroup, NetworkInterface, \
|
from azure.mgmt.network.models import PublicIPAddress, NetworkSecurityGroup, NetworkInterface, \
|
||||||
NetworkInterfaceIPConfiguration, Subnet
|
NetworkInterfaceIPConfiguration, Subnet
|
||||||
from azure.mgmt.storage.models import StorageAccountCreateParameters, Sku
|
from azure.mgmt.storage.models import StorageAccountCreateParameters, Sku
|
||||||
|
@ -637,6 +646,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
ssh_password_enabled=dict(type='bool', default=True),
|
ssh_password_enabled=dict(type='bool', default=True),
|
||||||
ssh_public_keys=dict(type='list'),
|
ssh_public_keys=dict(type='list'),
|
||||||
image=dict(type='raw'),
|
image=dict(type='raw'),
|
||||||
|
availability_set=dict(type='str'),
|
||||||
storage_account_name=dict(type='str', aliases=['storage_account']),
|
storage_account_name=dict(type='str', aliases=['storage_account']),
|
||||||
storage_container_name=dict(type='str', aliases=['storage_container'], default='vhds'),
|
storage_container_name=dict(type='str', aliases=['storage_container'], default='vhds'),
|
||||||
storage_blob_name=dict(type='str', aliases=['storage_blob']),
|
storage_blob_name=dict(type='str', aliases=['storage_blob']),
|
||||||
|
@ -670,6 +680,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
self.ssh_password_enabled = None
|
self.ssh_password_enabled = None
|
||||||
self.ssh_public_keys = None
|
self.ssh_public_keys = None
|
||||||
self.image = None
|
self.image = None
|
||||||
|
self.availability_set = None
|
||||||
self.storage_account_name = None
|
self.storage_account_name = None
|
||||||
self.storage_container_name = None
|
self.storage_container_name = None
|
||||||
self.storage_blob_name = None
|
self.storage_blob_name = None
|
||||||
|
@ -893,6 +904,13 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
if not image_reference:
|
if not image_reference:
|
||||||
self.fail("Parameter error: an image is required when creating a virtual machine.")
|
self.fail("Parameter error: an image is required when creating a virtual machine.")
|
||||||
|
|
||||||
|
availability_set_resource = None
|
||||||
|
if self.availability_set:
|
||||||
|
parsed_availability_set = parse_resource_id(self.availability_set)
|
||||||
|
availability_set = self.get_availability_set(parsed_availability_set.get('resource_group', self.resource_group),
|
||||||
|
parsed_availability_set.get('name'))
|
||||||
|
availability_set_resource = SubResource(availability_set.id)
|
||||||
|
|
||||||
# Get defaults
|
# Get defaults
|
||||||
if not self.network_interface_names:
|
if not self.network_interface_names:
|
||||||
default_nic = self.create_default_nic()
|
default_nic = self.create_default_nic()
|
||||||
|
@ -956,6 +974,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
network_profile=NetworkProfile(
|
network_profile=NetworkProfile(
|
||||||
network_interfaces=nics
|
network_interfaces=nics
|
||||||
),
|
),
|
||||||
|
availability_set=availability_set_resource,
|
||||||
plan=plan
|
plan=plan
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1052,6 +1071,13 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
storage_account_type=vm_dict['properties']['storageProfile']['osDisk']['managedDisk']['storageAccountType']
|
storage_account_type=vm_dict['properties']['storageProfile']['osDisk']['managedDisk']['storageAccountType']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
availability_set_resource = None
|
||||||
|
try:
|
||||||
|
availability_set_resource = SubResource(vm_dict['properties']['availabilitySet']['id'])
|
||||||
|
except Exception:
|
||||||
|
# pass if the availability set is not set
|
||||||
|
pass
|
||||||
|
|
||||||
vm_resource = VirtualMachine(
|
vm_resource = VirtualMachine(
|
||||||
vm_dict['location'],
|
vm_dict['location'],
|
||||||
os_profile=OSProfile(
|
os_profile=OSProfile(
|
||||||
|
@ -1077,6 +1103,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
version=vm_dict['properties']['storageProfile']['imageReference']['version']
|
version=vm_dict['properties']['storageProfile']['imageReference']['version']
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
availability_set=availability_set_resource,
|
||||||
network_profile=NetworkProfile(
|
network_profile=NetworkProfile(
|
||||||
network_interfaces=nics
|
network_interfaces=nics
|
||||||
),
|
),
|
||||||
|
@ -1434,6 +1461,11 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
|
||||||
return ImageReference(id=vm_image.id)
|
return ImageReference(id=vm_image.id)
|
||||||
|
|
||||||
self.fail("Error could not find image with name {0}".format(name))
|
self.fail("Error could not find image with name {0}".format(name))
|
||||||
|
def get_availability_set(self, resource_group, name):
|
||||||
|
try:
|
||||||
|
return self.compute_client.availability_sets.get(resource_group, name)
|
||||||
|
except Exception as exc:
|
||||||
|
self.fail("Error fetching availability set {0} - {1}".format(name, str(exc)))
|
||||||
|
|
||||||
def get_storage_account(self, name):
|
def get_storage_account(self, name):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -16,6 +16,11 @@
|
||||||
name: "{{ storage_account }}"
|
name: "{{ storage_account }}"
|
||||||
account_type: Standard_LRS
|
account_type: Standard_LRS
|
||||||
|
|
||||||
|
- name: Create an availability set
|
||||||
|
azure_rm_availabilityset:
|
||||||
|
name: "avbs{{ resource_group | hash('md5') | truncate(7, True, '') }}"
|
||||||
|
resource_group: "{{ resource_group }}"
|
||||||
|
|
||||||
- name: Create virtual network
|
- name: Create virtual network
|
||||||
azure_rm_virtualnetwork:
|
azure_rm_virtualnetwork:
|
||||||
resource_group: "{{ resource_group }}"
|
resource_group: "{{ resource_group }}"
|
||||||
|
@ -62,6 +67,7 @@
|
||||||
short_hostname: testvm
|
short_hostname: testvm
|
||||||
os_type: Linux
|
os_type: Linux
|
||||||
network_interfaces: testvm001
|
network_interfaces: testvm001
|
||||||
|
availability_set: "avbs{{ resource_group | hash('md5') | truncate(7, True, '') }}"
|
||||||
image:
|
image:
|
||||||
offer: UbuntuServer
|
offer: UbuntuServer
|
||||||
publisher: Canonical
|
publisher: Canonical
|
||||||
|
@ -69,6 +75,10 @@
|
||||||
version: latest
|
version: latest
|
||||||
register: output
|
register: output
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- azure_vm.properties.availabilitySet.id
|
||||||
|
|
||||||
- name: Restart the virtual machine
|
- name: Restart the virtual machine
|
||||||
azure_rm_virtualmachine:
|
azure_rm_virtualmachine:
|
||||||
resource_group: "{{ resource_group }}"
|
resource_group: "{{ resource_group }}"
|
||||||
|
|
Loading…
Reference in a new issue