azure_rm_virtualmachine – Manage Azure virtual machines

New in version 2.1.

Synopsis

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.7

  • azure >= 2.0.0

Parameters

Parameter Choices/Defaults Comments
accept_terms
boolean
added in 2.7
    Choices:
  • no ←
  • yes
Accept terms for marketplace images that require it
Only Azure service admin/account admin users can purchase images from the marketplace
ad_user
string
Active Directory username. Use when authenticating with an Active Directory user rather than service principal.
adfs_authority_url
string
added in 2.6
Azure AD authority url. Use when authenticating with Username/password, and has your own ADFS authority.
admin_password
-
Password for the admin username. Not required if the os_type is Linux and SSH password authentication is disabled by setting ssh_password_enabled to false.
admin_username
-
Admin username used to access the host after it is created. Required when creating a VM.
allocated
boolean
    Choices:
  • no
  • yes ←
Toggle that controls if the machine is allocated/deallocated, only useful with state='present'.
api_profile
string
added in 2.5
Default:
"latest"
Selects an API profile to use when communicating with Azure services. Default value of latest is appropriate for public clouds; future values will allow use with Azure Stack.
append_tags
boolean
    Choices:
  • no
  • yes ←
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.
auth_source
string
added in 2.5
    Choices:
  • auto
  • cli
  • credential_file
  • env
  • msi
Controls the source of the credentials to use for authentication.
If not specified, ANSIBLE_AZURE_AUTH_SOURCE environment variable will be used and default to auto if variable is not defined.
auto will follow the default precedence of module parameters -> environment variables -> default profile in credential file ~/.azure/credentials.
When set to cli, the credentials will be sources from the default Azure CLI profile.
Can also be set via the ANSIBLE_AZURE_AUTH_SOURCE environment variable.
When set to msi, the host machine must be an azure resource with an enabled MSI extension. subscription_id or the environment variable AZURE_SUBSCRIPTION_ID can be used to identify the subscription ID if the resource is granted access to more than one subscription, otherwise the first subscription is chosen.
The msi was added in Ansible 2.6.
availability_set
-
added in 2.5
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.
cert_validation_mode
string
added in 2.5
    Choices:
  • ignore
  • validate
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 ignore. Can also be set via credential file profile or the AZURE_CERT_VALIDATION environment variable.
client_id
string
Azure client ID. Use when authenticating with a Service Principal.
cloud_environment
string
added in 2.4
Default:
"AzureCloud"
For cloud environments other than the US public cloud, the environment name (as defined by Azure Python SDK, eg, AzureChinaCloud, AzureUSGovernment), or a metadata discovery endpoint URL (required for Azure Stack). Can also be set via credential file profile or the AZURE_CLOUD_ENVIRONMENT environment variable.
custom_data
-
added in 2.5
Data which is made available to the virtual machine and used by e.g., cloud-init.
data_disks
-
added in 2.4
Describes list of data disks.
caching
-
added in 2.4
    Choices:
  • ReadOnly ←
  • ReadWrite
Type of data disk caching.
disk_size_gb
-
added in 2.4
The initial disk size in GB for blank data disks
lun
-
added in 2.4
Default:
0
The logical unit number for data disk
managed_disk_type
-
added in 2.4
    Choices:
  • Standard_LRS
  • StandardSSD_LRS
  • Premium_LRS
Managed data disk type
storage_account_name
-
added in 2.4
Name of an existing 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 'Standard_LRS'.
storage_blob_name
-
added in 2.4
Name fo the storage blob used to hold the VM's OS disk image. If no name is provided, defaults to the VM name + '.vhd'. If you provide a name, it must end with '.vhd'
storage_container_name
-
added in 2.4
Default:
"vhds"
Name of the container to use within the storage account to store VHD blobs. If no name is specified a default container will created.
generalized
boolean
added in 2.8
    Choices:
  • no
  • yes
Use with state present to generalize the machine. Set to true to generalize the machine.
Please note that this operation is irreversible.
image
- / required
Specifies the image used to build the VM.
If a string, the image is sourced from a custom image based on the name.
If a dict with the keys publisher, offer, sku, and version, the image is sourced from a Marketplace image. NOTE: set image.version to latest to get the most recent version of a given image.
If a dict with the keys name and resource_group, the image is sourced from a custom image based on the name and resource_group set. NOTE: the key resource_group is optional and if omitted, all images in the subscription will be searched for by name.
Custom image support was added in Ansible 2.5
license_type
-
added in 2.8
    Choices:
  • Windows_Server
  • Windows_Client
Specifies that the image or disk that is being used was licensed on-premises. This element is only used for images that contain the Windows Server operating system.
Note: To unset this value, it has to be set to the string 'None'.
location
-
Valid Azure location. Defaults to location of the resource group.
managed_disk_type
-
added in 2.4
    Choices:
  • Standard_LRS
  • StandardSSD_LRS
  • Premium_LRS
Managed OS disk type
name
- / required
Name of the virtual machine.
network_interface_names
-
List of existing network interface names to add to the VM.
Item can be a str of name or resource id of the network interface.
Item can also be a dict contains 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.
In order for the module to create a new network interface, at least one Virtual Network with one Subnet must exist.

aliases: network_interfaces
open_ports
-
If a network interface is created when creating the VM, a security group will be created as well. For Linux hosts a rule will be added to the security group allowing inbound TCP connections to the default SSH port 22, and for Windows hosts ports 3389 and 5986 will be opened. Override the default open ports by providing a list of ports.
os_disk_caching
-
    Choices:
  • ReadOnly ←
  • ReadWrite
Type of OS disk caching.

aliases: disk_caching
os_disk_name
-
added in 2.8
OS disk name
os_disk_size_gb
-
added in 2.7
Type of OS disk size in GB.
os_type
-
    Choices:
  • Windows
  • Linux ←
Base type of operating system.
password
-
Active Directory user password. Use when authenticating with an Active Directory user rather than service principal.
plan
-
added in 2.5
A dictionary describing a third-party billing plan for an instance
name
- / required
billing plan name
product
- / required
product name
promotion_code
-
optional promotion code
publisher
- / required
publisher offering the plan
profile
string
Security profile found in ~/.azure/credentials file.
public_ip_allocation_method
-
    Choices:
  • Dynamic
  • Static ←
  • Disabled
If a public IP address is created when creating the VM (because a Network Interface was not provided), determines if the public IP address remains permanently associated with the Network Interface. If set to 'Dynamic' the public IP address may change any time the VM is rebooted or power cycled.
The Disabled choice was added in Ansible 2.6.

aliases: public_ip_allocation
remove_on_absent
-
Default:
["all"]
When removing a VM using state 'absent', also remove associated resources.
It can be 'all' or 'all_autocreated' or a list with any of the following: ['network_interfaces', 'virtual_storage', 'public_ips'].
To remove all resources referred by VM use 'all'.
To remove all resources that were automatically created while provisioning VM use 'all_autocreated'.
Any other input will be ignored.
resource_group
- / required
Name of the resource group containing the virtual machine.
restarted
boolean
    Choices:
  • no
  • yes
Use with state present to restart a running VM.
secret
string
Azure client secret. Use when authenticating with a Service Principal.
short_hostname
-
Name assigned internally to the host. On a linux VM this is the name returned by the `hostname` command. When creating a virtual machine, short_hostname defaults to name.
ssh_password_enabled
boolean
    Choices:
  • no
  • yes ←
When the os_type is Linux, setting ssh_password_enabled to false will disable SSH password authentication and require use of SSH keys.
ssh_public_keys
-
For os_type Linux provide a list of SSH keys. Each item in the list should be a dictionary where the dictionary contains two keys: path and key_data. Set the path to the default location of the authorized_keys files. On an Enterprise Linux host, for example, the path will be /home/<admin username>/.ssh/authorized_keys. Set key_data to the actual value of the public key.
started
boolean
    Choices:
  • no
  • yes ←
Use with state present to start the machine. Set to false to have the machine be 'stopped'.
state
-
    Choices:
  • absent
  • present ←
Assert the state of the virtual machine.
State present will check that the machine exists with the requested configuration. If the configuration of the existing machine does not match, the machine will be updated. Use options started, allocated and restarted to change the machine's power state.
State absent will remove the virtual machine.
storage_account_name
-
Name of an existing 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 'Standard_LRS'.

aliases: storage_account
storage_blob_name
-
Name of the storage blob used to hold the VM's OS disk image. If no name is provided, defaults to the VM name + '.vhd'. If you provide a name, it must end with '.vhd'

aliases: storage_blob
storage_container_name
-
Default:
"vhds"
Name of the container to use within the storage account to store VHD blobs. If no name is specified a default container will created.

aliases: storage_container
subnet_name
-
When creating a virtual machine, if a network interface name is not provided, one will be created.
The new network interface will be assigned to the first subnet found in the virtual network.
Use this parameter to provide a specific subnet instead.
If the subnet is in another resource group, specific resource group by virtual_network_resource_group.

aliases: subnet
subscription_id
string
Your Azure subscription Id.
tags
dictionary
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.
tenant
string
Azure tenant ID. Use when authenticating with a Service Principal.
virtual_network_name
-
When creating a virtual machine, if a network interface name is not provided, one will be created.
The network interface will be assigned to the first virtual network found in the resource group.
Use this parameter to provide a specific virtual network instead.
If the virtual network in in another resource group, specific resource group by virtual_network_resource_group.

aliases: virtual_network
virtual_network_resource_group
-
added in 2.4
When creating a virtual machine, if a specific virtual network from another resource group should be used, use this parameter to specify the resource group to use.
vm_identity
-
added in 2.8
    Choices:
  • SystemAssigned
Identity for the virtual machine.
vm_size
-
A valid Azure VM size value. For example, 'Standard_D4'. The list of choices varies depending on the subscription and location. Check your subscription for available choices. Required when creating a VM.
winrm
-
added in 2.8
List of Windows Remote Management configurations of the VM.
certificate_store
-
Specifies the certificate store on the Virtual Machine to which the certificate should be added. The specified certificate store is implicitly in the LocalMachine account.
certificate_url
-
This is the URL of a certificate that has been uploaded to Key Vault as a secret.
protocol
- / required
    Choices:
  • http
  • https
Specifies the protocol of listener
source_vault
-
The relative URL of the Key Vault containing the certificate
zones
list
added in 2.8
A list of Availability Zones for your virtual machine

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: chouseknecht
    admin_password: <your password here>
    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: adminUser
    availability_set: avs-managed-disk
    managed_disk_type: Standard_LRS
    image:
      offer: CoreOS
      publisher: CoreOS
      sku: Stable
      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: adminUser
    ssh_public_keys:
      - path: /home/adminUser/.ssh/authorized_keys
        key_data: < insert yor 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: adminUser
    ssh_public_keys:
      - path: /home/adminUser/.ssh/authorized_keys
        key_data: < insert yor ssh public key here... >
    image:
      offer: CoreOS
      publisher: CoreOS
      sku: Stable
      version: latest
    data_disks:
        - lun: 0
          disk_size_gb: 64
          managed_disk_type: Standard_LRS
        - 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: adminUser
    ssh_password_enabled: false
    ssh_public_keys:
    - path: /home/adminUser/.ssh/authorized_keys
      key_data: < insert yor ssh public key here... >
    network_interfaces: testvm001
    storage_container: osdisk
    storage_blob: osdisk.vhd
    image:
      offer: CoreOS
      publisher: CoreOS
      sku: Stable
      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: adminUser
    admin_password: password01
    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: adminUser
    admin_password: password01
    image:
      name: customimage001
      resource_group: myResourceGroup

- name: Create VM with spcified OS disk size
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: big-os-disk
    admin_username: chouseknecht
    admin_password: <your password here>
    os_disk_size_gb: 512
    image:
      offer: CentOS
      publisher: OpenLogic
      sku: '7.1'
      version: latest

- name: Create VM with OS and Plan, accepting the terms
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: f5-nva
    admin_username: chouseknecht
    admin_password: <your password here>
    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: Power Off
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: testvm002
    started: no

- name: Deallocate
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: testvm002
    allocated: no

- name: Power On
  azure_rm_virtualmachine:
    resource_group:
    name: testvm002

- name: Restart
  azure_rm_virtualmachine:
    resource_group:
    name: testvm002
    restarted: yes

- name: Create a VM with an Availability Zone
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: testvm001
    vm_size: Standard_DS1_v2
    admin_username: adminUser
    admin_password: password01
    image: customimage001
    zones: [1]

- 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 Returned Description
azure_vm
complex
always
Facts about the current state of the object. Note that facts are not part of the registered output but available directly.

  properties
-

  type
-

deleted_network_interfaces
list
on delete
List of deleted NICs.

deleted_public_ips
list
on delete
List of deleted public IP address names.

deleted_vhd_uris
list
on delete
List of deleted Virtual Hard Disk URIs.

powerstate
string
always
Indicates if the state is running, stopped, deallocated, generalized



Status

Authors

  • Chris Houseknecht (@chouseknecht)

  • Matt Davis (@nitzmahone)

  • Christopher Perrin (@cperrin88)

Hint

If you notice any issues in this documentation you can edit this document to improve it.