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

Addition of Spot instance support for VM and VMSS #559

Merged
merged 18 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions plugins/modules/azure_rm_virtualmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@
- A valid Azure VM size value. For example, C(Standard_D4).
- Choices vary depending on the subscription and location. Check your subscription for available choices.
- Required when creating a VM.
priority:
description:
- Priority of the VM.
choices:
- Regular
- Spot
default: Regular
ElPrincidente marked this conversation as resolved.
Show resolved Hide resolved
eviction_policy:
description:
- Specifies the eviction policy for the Azure Spot virtual machine.
- Requires priority to be set to Spot.
choices:
- Deallocate
- Delete
default: Deallocate
ElPrincidente marked this conversation as resolved.
Show resolved Hide resolved
max_price:
description:
- Specifies the maximum price you are willing to pay for a Azure Spot VM/VMSS.
- This price is in US Dollars.
- C(-1) indicates default price to be up-to on-demand.
- Requires priority to be set to Spot.
default: -1
admin_username:
description:
- Admin username used to access the VM after it is created.
Expand Down Expand Up @@ -539,6 +561,21 @@
product: f5-big-ip-best
publisher: f5-networks

- name: Create a VM with Spot Instance
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm10
vm_size: Standard_D4
priority: Spot
eviction_policy: Deallocate
admin_username: chouseknecht
admin_password: <your password here>
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest

- name: Power Off
azure_rm_virtualmachine:
resource_group: myResourceGroup
Expand Down Expand Up @@ -797,6 +834,9 @@ def __init__(self):
location=dict(type='str'),
short_hostname=dict(type='str'),
vm_size=dict(type='str'),
priority=dict(type='str', choices=['Regular', 'Spot']),
eviction_policy=dict(type='str', choices=['Deallocate', 'Delete']),
max_price=dict(type='float', default=-1),
admin_username=dict(type='str'),
admin_password=dict(type='str', no_log=True),
ssh_password_enabled=dict(type='bool', default=True),
Expand Down Expand Up @@ -841,6 +881,8 @@ def __init__(self):
self.location = None
self.short_hostname = None
self.vm_size = None
self.priority = None
self.eviction_policy = None
self.admin_username = None
self.admin_password = None
self.ssh_password_enabled = None
Expand Down Expand Up @@ -1303,6 +1345,13 @@ def exec_module(self, **kwargs):
zones=self.zones,
)

if self.priority == 'Spot':
vm_resource.priority = self.priority
vm_resource.eviction_policy = self.eviction_policy
vm_resource.billing_profile = self.compute_models.BillingProfile(
max_price=self.max_price
)

if self.license_type is not None:
vm_resource.license_type = self.license_type

Expand Down
51 changes: 51 additions & 0 deletions plugins/modules/azure_rm_virtualmachinescaleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@
choices:
- Manual
- Automatic
priority:
description:
- Priority of the VMSS.
choices:
- Regular
- Spot
default: Regular
eviction_policy:
description:
- Specifies the eviction policy for the Azure Spot virtual machine.
- Requires priority to be set to Spot.
choices:
- Deallocate
- Delete
default: Deallocate
ElPrincidente marked this conversation as resolved.
Show resolved Hide resolved
max_price:
description:
- Specifies the maximum price you are willing to pay for a Azure Spot VM/VMSS.
- This price is in US Dollars.
- C(-1) indicates default price to be up-to on-demand.
- Requires priority to be set to Spot.
default: -1
admin_username:
description:
- Admin username used to access the host after it is created. Required when creating a VM.
Expand Down Expand Up @@ -355,6 +377,23 @@
image:
name: customimage001
resource_group: myResourceGroup

- name: Create a VMSS with Spot Instance
azure_rm_virtualmachinescaleset:
resource_group: myResourceGroup
name: testvmss
vm_size: Standard_DS1_v2
capacity: 5
priority: Spot
eviction_policy: Deallocate
virtual_network_name: testvnet
upgrade_policy: Manual
subnet_name: testsubnet
admin_username: adminUser
admin_password: password01
managed_disk_type: Standard_LRS
image: customimage001

'''

RETURN = '''
Expand Down Expand Up @@ -495,6 +534,9 @@ def __init__(self):
tier=dict(type='str', choices=['Basic', 'Standard']),
capacity=dict(type='int', default=1),
upgrade_policy=dict(type='str', choices=['Automatic', 'Manual']),
priority=dict(type='str', choices=['Regular', 'Spot']),
eviction_policy=dict(type='str', choices=['Deallocate', 'Delete']),
max_price=dict(type='float', default=-1),
admin_username=dict(type='str'),
admin_password=dict(type='str', no_log=True),
ssh_password_enabled=dict(type='bool', default=True),
Expand Down Expand Up @@ -535,6 +577,8 @@ def __init__(self):
self.capacity = None
self.tier = None
self.upgrade_policy = None
self.priority = None
self.eviction_policy = None
self.admin_username = None
self.admin_password = None
self.ssh_password_enabled = None
Expand Down Expand Up @@ -922,6 +966,13 @@ def exec_module(self, **kwargs):
zones=self.zones
)

if self.priority == 'Spot':
vmss_resource.virtual_machine_profile.priority = self.priority
vmss_resource.virtual_machine_profile.eviction_policy = self.eviction_policy
vmss_resource.virtual_machine_profile.billing_profile = self.compute_models.BillingProfile(
max_price=self.max_price
)

if self.scale_in_policy:
vmss_resource.scale_in_policy = self.gen_scale_in_policy()

Expand Down