Skip to content

Commit

Permalink
Azure VM: Adds feature to choose Availability Zone for VM
Browse files Browse the repository at this point in the history
Fixes #40596
  • Loading branch information
kamotos committed Oct 12, 2018
1 parent a4961ff commit acfd0a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
Expand Up @@ -122,6 +122,14 @@
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.
version_added: "2.5"
availability_zone:
description:
- Availability Zone where the vm should be available.
choices:
- 1
- 2
- 3
version_added: "2.8"
storage_account_name:
description:
- Name of an existing storage account that supports creation of VHD blobs. If not specified for a new VM,
Expand Down Expand Up @@ -694,6 +702,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
def __init__(self):

self.module_arg_spec = dict(
availability_zone=dict(type='str', choices=['1', '2', '3']),
resource_group=dict(type='str', required=True),
name=dict(type='str', required=True),
custom_data=dict(type='str'),
Expand Down Expand Up @@ -732,6 +741,7 @@ def __init__(self):
accept_terms=dict(type='bool', default=False)
)

self.availability_zone = None
self.resource_group = None
self.name = None
self.custom_data = None
Expand Down Expand Up @@ -978,6 +988,8 @@ def exec_module(self, **kwargs):
if changed:
if self.state == 'present':
default_storage_account = None
zones = [self.availability_zone] if self.availability_zone else []

if not vm:
# Create the VM
self.log("Create virtual machine {0}".format(self.name))
Expand Down Expand Up @@ -1068,7 +1080,8 @@ def exec_module(self, **kwargs):
network_interfaces=nics
),
availability_set=availability_set_resource,
plan=plan
plan=plan,
zones=zones
)

if self.admin_password:
Expand Down Expand Up @@ -1224,6 +1237,7 @@ def exec_module(self, **kwargs):
network_profile=self.compute_models.NetworkProfile(
network_interfaces=nics
),
zones=zones
)

if vm_dict.get('tags'):
Expand Down
Expand Up @@ -3,6 +3,7 @@
storage_account: "{{ resource_group | hash('md5') | truncate(24, True, '') }}"
vm_name1: "vm1{{ resource_group | hash('md5') | truncate(5, True, '') }}"
vm_name2: "vm2{{ resource_group | hash('md5') | truncate(5, True, '') }}"
vm_name3: "vm3{{ resource_group | hash('md5') | truncate(5, True, '') }}"
abs_name1: "avbs1{{ resource_group | hash('md5') | truncate(3, True, '') }}"
abs_name2: "avbs2{{ resource_group | hash('md5') | truncate(3, True, '') }}"

Expand Down Expand Up @@ -64,6 +65,35 @@
public_ip_name: "{{ vm_name1 }}"
security_group: "{{ vm_name1 }}"

- name: Create virtual machine In Availability zone
azure_rm_virtualmachine:
admin_username: adminuser
admin_password: Password123!
resource_group: "{{ resource_group }}"
name: "{{ vm_name3 }}"
password: PASSWORD
vm_size: Standard_A0
availability_zone: 1
public_ip_allocation_method: Disabled
location: centralus
image:
offer: UbuntuServer
publisher: Canonical
sku: 16.04-LTS
version: latest
managed_disk_type: Standard_LRS

- assert:
that:
- azure_vm.zones == ["1"]

- name: Delete VM
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name3 }}"
state: absent
vm_size: Standard_A0

- name: Create virtual machine with a single NIC
register: output
azure_rm_virtualmachine:
Expand Down

0 comments on commit acfd0a0

Please sign in to comment.