azure.azcollection.azure_rm_virtualmachine module – Manage Azure virtual machines
Note
This module is part of the azure.azcollection collection (version 3.1.0).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install azure.azcollection
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: azure.azcollection.azure_rm_virtualmachine
.
New in azure.azcollection 0.1.2
Synopsis
Manage and configure virtual machines (VMs) and associated resources on Azure.
Requires a resource group containing at least one virtual network with at least one subnet.
Supports images from the Azure Marketplace, which can be discovered with azure.azcollection.azure_rm_virtualmachineimage_info.
Supports custom images since Ansible 2.5.
To use custom_data on a Linux image, the image must have cloud-init enabled. If cloud-init is not enabled, custom_data is ignored.
Requirements
The below requirements are needed on the host that executes this module.
python >= 2.7
The host that executes this module must have the azure.azcollection collection installed via galaxy
All python packages listed in collection’s requirements.txt must be installed via pip on the host that executes modules from azure.azcollection
Full installation instructions may be found https://galaxy.ansible.com/azure/azcollection
Parameters
Parameter |
Comments |
---|---|
Accept terms for Marketplace images that require it. Only Azure service admin/account admin users can purchase images from the Marketplace. Only valid when a plan is specified. Choices:
|
|
Active Directory username. Use when authenticating with an Active Directory user rather than service principal. |
|
Enables or disables a capability on the virtual machine. |
|
The flag that enables or disables a capability to have one or more managed data disks with UltraSSD_LRS storage account type on the VM. Managed disks with storage account type UltraSSD_LRS can be added to a virtual machine set only if this property is enabled. Choices:
|
|
Azure AD authority url. Use when authenticating with Username/password, and has your own ADFS authority. |
|
Password for the admin username. Not required if the os_type=Linux and SSH password authentication is disabled by setting ssh_password_enabled=false. |
|
Admin username used to access the VM after it is created. Required when creating a VM. |
|
Whether the VM is allocated or deallocated, only useful with state=present. Choices:
|
|
Selects an API profile to use when communicating with Azure services. Default value of Default: |
|
Use to control if tags field is canonical or just appends to existing tags. When canonical, any tags not found in the tags parameter will be removed from the object’s metadata. Choices:
|
|
Controls the source of the credentials to use for authentication. Can also be set via the When set to When set to When set to When set to When set to The Choices:
|
|
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. |
|
Manage boot diagnostics settings for a VM. Boot diagnostics includes a serial console and remote console screenshots. |
|
Flag indicating if boot diagnostics are enabled. Choices:
|
|
Resource group where the storage account is located. It is mutually exclusive with type |
|
The name of an existing storage account to use for boot diagnostics. If not specified, uses storage_account_name defined one level up. If storage account is not specified anywhere, and It is mutually exclusive with type |
|
Should the storage account be managed by azure or a custom storage account It is mutually exclusive with suboption storage_account and resource_group Choices:
|
|
The name or ID of the capacity reservation group to be associated with. |
|
The ID of the capacity reservation group to be associated with. |
|
The Name of the capacity reservation group to be associated with. |
|
The resource group of capcacity reservation group to be associated with. |
|
Controls the certificate validation behavior for Azure endpoints. By default, all modules will validate the server certificate, but when an HTTPS proxy is in use, or against Azure Stack, it may be necessary to disable this behavior by passing Choices:
|
|
Azure client ID. Use when authenticating with a Service Principal or Managed Identity (msi). Can also be set via the |
|
For cloud environments other than the US public cloud, the environment name (as defined by Azure Python SDK, eg, Default: |
|
Whether network security group created and attached to network interface or not. Choices:
|
|
Data made available to the VM and used by Only used on Linux images with Consult https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#cloud-init-overview for cloud-init ready images. To enable cloud-init on a Linux image, follow https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cloudinit-prepare-custom-image. |
|
Describes list of data disks. Use azure.azcollection.azure_rm_manageddisk to manage the specific disk. |
|
Type of data disk caching. Choices:
|
|
ID of disk encryption set for data disk. |
|
The initial disk size in GB for blank data disks. This value cannot be larger than Size can be changed only when the virtual machine is deallocated. Not sure when managed_disk_id defined. |
|
The logical unit number for data disk. This value is used to identify data disks within the VM and therefore must be unique for each data disk attached to a VM. |
|
The ID of the existing data disk. If specified, attach mode will be chosen. |
|
Managed data disk type. Only used when OS disk created with managed disk. Choices:
|
|
Name of an existing storage account that supports creation of VHD blobs. If not specified for a new VM, a new storage account started with name will be created using storage type Only used when OS disk created with virtual hard disk (VHD). Used when managed_disk_type not defined. Cannot be updated unless lun updated. |
|
Name of the storage blob used to hold the OS disk image of the VM. Must end with ‘.vhd’. Default to the name + timestamp + lun + ‘.vhd’. Only used when OS disk created with virtual hard disk (VHD). Used when managed_disk_type not defined. Cannot be updated unless lun updated. |
|
Name of the container to use within the storage account to store VHD blobs. If no name is specified a default container named ‘vhds’ will created. Only used when OS disk created with virtual hard disk (VHD). Used when managed_disk_type not defined. Cannot be updated unless lun updated. Default: |
|
Determines whether or not instance discovery is performed when attempting to authenticate. Setting this to true will completely disable both instance discovery and authority validation. This functionality is intended for use in scenarios where the metadata endpoint cannot be reached such as in private clouds or Azure Stack. The process of instance discovery entails retrieving authority metadata from https://login.microsoft.com/ to validate the authority. By setting this to **True**, the validation of the authority is disabled. As a result, it is crucial to ensure that the configured authority host is valid and trustworthy. Set via credential file profile or the Choices:
|
|
Parameters of ephemeral disk settings that can be specified for operating system disk. Ephemeral OS disk is only supported for VMS Instances using Managed Disk. Choices:
|
|
Specifies the eviction policy for the Azure Spot virtual machine. Requires priority to be set to Spot. Choices:
|
|
Use more force during stop of VM Set to Set to Choices:
|
|
Whether the VM is generalized or not. Set to Generalizing a VM is irreversible. Choices:
|
|
The image used to build the VM. For custom images, the name of the image. To narrow the search to a specific resource group, a dict with the keys name and resource_group. For community images, a dict with the keys community_gallery_image_id. Specified the community gallery image unique id for vm demployment. The community_gallery_image_id can be fetched from shared gallery image GET call. For Marketplace images, a dict with the keys publisher, offer, sku, and version. Set version=latest to get the most recent version of a given image. Required when creating. |
|
On-premise license for the image or disk. Only used for images that contain the Windows Server operating system. To remove all license type settings, set to the string Choices:
|
|
Specifies the Linux operating system settings on the virtual machine. |
|
Specifies whether password authentication should be disabled. Choices:
|
|
Valid Azure location for the VM. Defaults to location of the resource group. |
|
Parent argument. |
|
Parent argument. |
|
Managed OS disk type. Create OS disk with managed disk if defined. If not defined, the OS disk will be created with virtual hard disk (VHD). Choices:
|
|
Specifies the maximum price you are willing to pay for a Azure Spot VM/VMSS. This price is in US Dollars.
Requires priority to be set to Spot. Default: |
|
Name of the VM. |
|
Network interface names to add to the VM. Can be a string of name or resource ID of the network interface. Can be a dict containing resource_group and name of the network interface. If a network interface name is not provided when the VM is created, a default network interface will be created. To create a new network interface, at least one Virtual Network with one Subnet must exist. |
|
List of ports to open in the security group for the VM, when a security group and network interface are created with a VM. For Linux hosts, defaults to allowing inbound TCP connections to port 22. For Windows hosts, defaults to opening ports 3389 and 5986. |
|
Type of OS disk caching. Choices:
|
|
ID of disk encryption set for OS disk. |
|
OS disk name. |
|
Size of OS disk in GB. |
|
Base type of operating system. Choices:
|
|
Active Directory user password. Use when authenticating with an Active Directory user rather than service principal. |
|
Third-party billing plan for the VM. |
|
Billing plan name. |
|
Product name. |
|
Optional promotion code. |
|
Publisher offering the plan. |
|
Priority of the VM.
Choices:
|
|
Security profile found in ~/.azure/credentials file. |
|
The name or ID of the proximity placement group to be associated with. |
|
The ID of the proximity placement group to be associated with. |
|
The Name of the proximity placement group to be associated with. |
|
The resource group of the proximity placement group to be associated with. |
|
Allocation method for the public IP of the VM. Used only if a network interface is not specified. When set to The Choices:
|
|
Associated resources to remove when removing a VM using state=absent. To remove all resources related to the VM being removed, including auto-created resources, set to To remove only resources that were automatically created while provisioning the VM being removed, set to To remove only specific resources, set to Any other input will be ignored. Default: |
|
Name of the resource group containing the VM. |
|
Set to Choices:
|
|
Azure client secret. Use when authenticating with a Service Principal. |
|
Specifies the Security related profile settings for the virtual machine. |
|
This property can be used by user in the request to enable or disable the Host Encryption for the virtual machine. This will enable the encryption for all the disks including Resource/Temp disk at host itself. Choices:
|
|
Specifies the SecurityType of the virtual machine. It is set as TrustedLaunch to enable UefiSettings. Choices:
|
|
Specifies the security settings like secure boot and vTPM used while creating the virtual machine. |
|
Specifies whether secure boot should be enabled on the virtual machine. Choices:
|
|
Specifies whether vTPM should be enabled on the virtual machine. Choices:
|
|
Name assigned internally to the host. On a Linux VM this is the name returned by the When creating a VM, short_hostname defaults to name. |
|
Whether to enable or disable SSH passwords. When os_type=Linux, set to Choices:
|
|
For os_type=Linux provide a list of SSH keys. Accepts a list of dicts where each dictionary contains two keys, path and key_data. Set path to the default location of the authorized_keys files. For example, path=/home/<admin username>/.ssh/authorized_keys. Set key_data to the actual value of the public key. |
|
Whether the VM is started or stopped. Set to Set to Choices:
|
|
State of the VM. Set to Set to Does not affect power state. Use started/allocated/restarted parameters to change the power state of a VM. Choices:
|
|
Name of a storage account that supports creation of VHD blobs. If not specified for a new VM, a new storage account named <vm name>01 will be created using storage type |
|
Name of the storage blob used to hold the OS disk image of the VM. Must end with ‘.vhd’. If not specified, defaults to the VM name + ‘.vhd’. |
|
Name of the container to use within the storage account to store VHD blobs. If not specified, a default container will be created. Default: |
|
Subnet for the VM. Defaults to the first subnet found in the virtual network or the subnet of the network_interface_name, if provided. If the subnet is in another resource group, specify the resource group with virtual_network_resource_group. |
|
Your Azure subscription Id. |
|
The swap OS disk parameters. |
|
The swap OS disk’s ID. |
|
The swap OS disk’s name. |
|
The swap OS disk’s resource group. |
|
Dictionary of string:string pairs to assign as metadata to the object. Metadata tags on the object will be updated with any provided values. To remove tags set append_tags option to false. Currently, Azure DNS zones and Traffic Manager services also don’t allow the use of spaces in the tag. Azure Front Door doesn’t support the use of Azure Automation and Azure CDN only support 15 tags on resources. |
|
Azure tenant ID. Use when authenticating with a Service Principal. |
|
The thumbprint of the private key specified in x509_certificate_path. Use when authenticating with a Service Principal. Required if x509_certificate_path is defined. |
|
The virtual network to use when creating a VM. If not specified, a new network interface will be created and assigned to the first virtual network found in the resource group. Use with virtual_network_resource_group to place the virtual network in another resource group. |
|
The resource group to use when creating a VM with another resource group’s virtual network. |
|
Identity for the VM. |
|
Type of the managed identity Choices:
|
|
User Assigned Managed Identities and its options Default: |
|
If the list of identities has to be appended to current identities (true) or if it has to replace current identities (false) Choices:
|
|
List of the user assigned identities IDs associated to the VM Default: |
|
A valid Azure VM size value. For example, Choices vary depending on the subscription and location. Check your subscription for available choices. Required when creating a VM. |
|
Specifies Windows operating system settings on the virtual machine. |
|
Indicates whether Automatic Updates is enabled for the Windows virtual machine. Choices:
|
|
Indicates whether virtual machine agent should be provisioned on the virtual machine. Choices:
|
|
List of Windows Remote Management configurations of the VM. |
|
The certificate store on the VM to which the certificate should be added. The specified certificate store is implicitly in the LocalMachine account. |
|
The URL of a certificate that has been uploaded to Key Vault as a secret. |
|
The protocol of the winrm listener. Choices:
|
|
The relative URL of the Key Vault containing the certificate. |
|
Path to the X509 certificate used to create the service principal in PEM format. The certificate must be appended to the private key. Use when authenticating with a Service Principal. |
|
A list of Availability Zones for your VM. A maximum of one zone can be configured. |
Notes
Note
For authentication with Azure you can pass parameters, set environment variables, use a profile stored in ~/.azure/credentials, or log in before you run your tasks or playbook with
az login
.Authentication is also possible using a service principal or Active Directory user.
To authenticate via service principal, pass subscription_id, client_id, secret and tenant or set environment variables AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_SECRET and AZURE_TENANT.
To authenticate via Active Directory user, pass ad_user and password, or set AZURE_AD_USER and AZURE_PASSWORD in the environment.
Alternatively, credentials can be stored in ~/.azure/credentials. This is an ini file containing a [default] section and the following keys: subscription_id, client_id, secret and tenant or subscription_id, ad_user and password. It is also possible to add additional profiles. Specify the profile by passing profile or setting AZURE_PROFILE in the environment.
See Also
See also
- Sign in with Azure CLI
How to authenticate using the
az login
command.
Examples
- name: Create VM with defaults
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm10
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Create an availability set for managed disk vm
azure_rm_availabilityset:
name: avs-managed-disk
resource_group: myResourceGroup
platform_update_domain_count: 5
platform_fault_domain_count: 2
sku: Aligned
- name: Create a VM with managed disk
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: vm-managed-disk
admin_username: "{{ username }}"
availability_set: avs-managed-disk
managed_disk_type: Standard_LRS
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
version: latest
vm_size: Standard_D4
- name: Create a VM with existing storage account and NIC
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
vm_size: Standard_D4
storage_account: testaccount001
admin_username: "{{ username }}"
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your ssh public key here... >
network_interfaces: testvm001
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Create a VM with OS and multiple data managed disks
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_D4
managed_disk_type: Standard_LRS
admin_username: "{{ username }}"
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your ssh public key here... >
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
version: latest
data_disks:
- lun: 0
managed_disk_id: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/myDisk"
- lun: 1
disk_size_gb: 128
managed_disk_type: Premium_LRS
- name: Create a VM with OS and multiple data storage accounts
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
ssh_password_enabled: false
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your ssh public key here... >
network_interfaces: testvm001
storage_container: osdisk
storage_blob: osdisk.vhd
boot_diagnostics:
enabled: true
type: managed
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
version: latest
data_disks:
- lun: 0
disk_size_gb: 64
storage_container_name: datadisk1
storage_blob_name: datadisk1.vhd
- lun: 1
disk_size_gb: 128
storage_container_name: datadisk2
storage_blob_name: datadisk2.vhd
- name: Create a VM with a custom image
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image: customimage001
- name: Create a VM with a custom image from a particular resource group
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
name: customimage001
resource_group: myResourceGroup
- name: Create a VM with an image id
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
id: '{{image_id}}'
- name: Create a VM with spcified OS disk size
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: big-os-disk
admin_username: "{{ username }}"
admin_password: "{{ password }}"
os_disk_size_gb: 512
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Create a VM with OS and Plan, accepting the terms
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: f5-nva
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
publisher: f5-networks
offer: f5-big-ip-best
sku: f5-bigip-virtual-edition-200m-best-hourly
version: latest
plan:
name: f5-bigip-virtual-edition-200m-best-hourly
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: "{{ username }}"
admin_password: "{{ password }}"
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Update VM to swap OS disk
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "vmforimage{{ rpfx }}"
admin_username: testuser
ssh_password_enabled: false
managed_disk_type: Premium_LRS
ssh_public_keys:
- path: /home/testuser/.ssh/authorized_keys
key_data: "ssh-rsa *******************@qq.com"
vm_size: Standard_D4s_v3
swap_os_disk:
os_disk_name: "{{ os_disk_name }}"
os_disk_resource_group: "{{ os_disk_resource_group }}"
image:
offer: 0001-com-ubuntu-server-focal
publisher: Canonical
sku: 20_04-lts
version: latest
- name: Create VM with cummunity gallery image ID
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "communit-image-vm"
admin_username: testuser
ssh_password_enabled: false
managed_disk_type: Premium_LRS
open_ports:
- 33
ssh_public_keys:
- path: /home/testuser/.ssh/authorized_keys
key_data: "ssh-rsa ************* @qq.com"
vm_size: Standard_B1ms
image:
community_gallery_image_id: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
- name: Power Off
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
started: false
- name: Deallocate
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
allocated: false
- name: Power On
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
- name: Restart
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
restarted: true
- name: Create a VM with an Availability Zone
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image: customimage001
zones: [1]
- name: Create a VM with security profile
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
vm_size: Standard_D4s_v3
managed_disk_type: Standard_LRS
admin_username: "{{ username }}"
admin_password: "{{ password }}"
security_profile:
uefi_settings:
secure_boot_enabled: true
v_tpm_enabled: true
encryption_at_host: true
security_type: TrustedLaunch
ssh_public_keys:
- path: /home/azureuser/.ssh/authorized_keys
key_data: "ssh-rsa *****"
image:
offer: 0001-com-ubuntu-server-jammy
publisher: Canonical
sku: 22_04-lts-gen2
version: latest
- name: Remove a VM and all resources that were autocreated
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
remove_on_absent: all_autocreated
state: absent
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Facts about the current state of the object. Note that facts are not part of the registered output but available directly. Returned: always Sample: |
|
List of deleted NICs. Returned: on delete Sample: |
|
List of deleted public IP address names. Returned: on delete Sample: |
|
List of deleted Virtual Hard Disk URIs. Returned: on delete Sample: |
|
Indicates if the state is Returned: always Sample: |