azure_rm_virtualmachine - Manage Azure virtual machines.

New in version 2.1.

Synopsis

  • Create, update, stop and start a virtual machine. Provide an existing storage account and network interface or allow the module to create these for you. If you choose not to provide a network interface, the resource group must contain a virtual network with at least one subnet.
  • Currently requires an image found in the Azure Marketplace. Use azure_rm_virtualmachineimage_facts module to discover the publisher, offer, sku and version of a particular image.

Requirements (on host that executes module)

  • python >= 2.7
  • azure >= 2.0.0

Options

parameter required default choices comments
ad_user
no
Active Directory username. Use when authenticating with an Active Directory user rather than service principal.
admin_password
no
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
no
Admin username used to access the host after it is created. Required when creating a VM.
allocated
no True
Toggle that controls if the machine is allocated/deallocated, only useful with state='present'.
append_tags
no True
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.
client_id
no
Azure client ID. Use when authenticating with a Service Principal.
cloud_environment
no 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.
data_disks
(added in 2.4)
no
Describes list of data disks.
Dictionary object data_disks
parameter required default choices comments
managed_disk_type
(added in 2.4)
no
  • Standard_LRS
  • Premium_LRS
Managed data disk type
disk_size_gb
(added in 2.4)
no
The initial disk size in GB for blank data disks
storage_container_name
(added in 2.4)
no 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.
caching
(added in 2.4)
no ReadOnly
  • ReadOnly
  • ReadWrite
Type of data disk caching.
storage_blob_name
(added in 2.4)
no
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_account_name
(added in 2.4)
no
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'.
lun
(added in 2.4)
no
The logical unit number for data disk
image
yes
A dictionary describing the Marketplace image used to build the VM. Will contain keys: publisher, offer, sku and version. NOTE: set image.version to 'latest' to get the most recent version of a given image.
location
no
Valid Azure location. Defaults to location of the resource group.
managed_disk_type
(added in 2.4)
no
  • Standard_LRS
  • Premium_LRS
Managed OS disk type
name
yes
Name of the virtual machine.
network_interface_names
no
List of existing network interface names to add to the VM. 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 network interface, at least one Virtual Network with one Subnet must exist.
open_ports
no
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
no ReadOnly
  • ReadOnly
  • ReadWrite
Type of OS disk caching.
aliases: disk_caching
os_type
no [u'Linux']
  • Windows
  • Linux
Base type of operating system.
password
no
Active Directory user password. Use when authenticating with an Active Directory user rather than service principal.
profile
no
Security profile found in ~/.azure/credentials file.
public_ip_allocation_method
no [u'Static']
  • Dynamic
  • Static
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.
aliases: public_ip_allocation
remove_on_absent
no [u'all']
When removing a VM using state 'absent', also remove associated resources
It can be 'all' or a list with any of the following: ['network_interfaces', 'virtual_storage', 'public_ips']
Any other input will be ignored
resource_group
yes
Name of the resource group containing the virtual machine.
restarted
no
Use with state 'present' to restart a running VM.
secret
no
Azure client secret. Use when authenticating with a Service Principal.
short_hostname
no
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
no True
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
no
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
no True
Use with state 'present' to start the machine. Set to false to have the machine be 'stopped'.
state
no present
  • 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
no
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
no
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'
aliases: storage_blob
storage_container_name
no 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.
subnet_name
no
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.
aliases: subnet
subscription_id
no
Your Azure subscription Id.
tags
no
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
no
Azure tenant ID. Use when authenticating with a Service Principal.
virtual_network_name
no
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 virtual network found in the resource group. Use this parameter to provide a specific virtual network instead.
aliases: virtual_network
virtual_network_resource_group
(added in 2.4)
no
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_size
no
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.

Examples

- name: Create VM with defaults
  azure_rm_virtualmachine:
    resource_group: Testing
    name: testvm10
    admin_username: chouseknecht
    admin_password: <your password here>
    image:
      offer: CentOS
      publisher: OpenLogic
      sku: '7.1'
      version: latest

- name: Create a VM with managed disk
  azure_rm_virtualmachine:
    resource_group: Testing
    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

- name: Create a VM with existing storage account and NIC
  azure_rm_virtualmachine:
    resource_group: Testing
    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: Testing
    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: Testing
    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: Power Off
  azure_rm_virtualmachine:
    resource_group: Testing
    name: testvm002
    started: no

- name: Deallocate
  azure_rm_virtualmachine:
    resource_group: Testing
    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: remove vm and all resources except public ips
  azure_rm_virtualmachine:
    resource_group: Testing
    name: testvm002
    state: absent
    remove_on_absent:
        - network_interfaces
        - virtual_storage

Return Values

Common return values are documented here Return Values, the following are the fields unique to this module:

name description returned type sample
azure_vm
Facts about the current state of the object. Note that facts are not part of the registered output but available directly.
always complex
contains:
name description returned type sample
type
properties
deleted_network_interfaces
List of deleted NICs.
on delete list
deleted_public_ips
List of deleted public IP address names.
on delete list
deleted_vhd_uris
List of deleted Virtual Hard Disk URIs.
on delete list
powerstate
Indicates if the state is running, stopped, deallocated
always string

Notes

Note

  • For authentication with Azure you can pass parameters, set environment variables or use a profile stored in ~/.azure/credentials. Authentication is 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.

Status

This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.4/azure_rm_virtualmachine_module.html