Skip to main content

azure_virtual_machines resource

Use the azure_virtual_machines InSpec audit resource to test the properties related to virtual machines for a resource group or the entire subscription.

Azure REST API version, endpoint, and HTTP client parameters

This resource interacts with API versions supported by the resource provider. You can specify the api_version as a resource parameter to use a specific version of the Azure REST API. If you don’t specify an API version, this resource uses the latest version available. For more information about API versioning, see the azure_generic_resource.

By default, this resource uses the azure_cloud global endpoint and default HTTP client settings. You can override these settings if you need to connect to a different Azure environment (such as Azure Government or Azure China). For more information about configuration options, see the resource pack README.

Syntax

An azure_virtual_machines resource block returns all Azure virtual machines within a resource group (if provided) or an entire subscription.

describe azure_virtual_machines do
  #...
end

Or

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP') do
  #...
end

Parameters

resource_group (optional)

The name of the resource group.

Properties

ids
A list of the unique resource IDs.

Field: id

os_disks
A list of OS disk names for all the virtual machines.

Field: os_disk

data_disks
A list of data disks for all the virtual machines.

Field: data_disks

vm_names
A list of all the virtual machine names.

Field: name

platforms
A list of virtual machine operation system platforms. Supported values are windows and linux.

Field: platform

tags
A list of tag:value pairs defined on the resources.

Field: tags

Note

For information on using filter criteria on plural resources, see the documentation on FilterTable

Examples

Test if any virtual machines exist in the resource group:

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP') do
  it { should exist }
end

Filters Based on Platform:

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP').where(platform: 'windows') do
  it { should exist }
end

Loop through virtual machines by their IDs:

azure_virtual_machines.ids.each do |id|
  describe azure_virtual_machine(resource_id: id) do
    it { should exist }
  end
end

Test if there are Windows virtual machines:

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP').where(platform: 'windows') do
  it { should exist }
end

Test that there are virtual machines that includes a certain string in their names (client-side filtering):

describe azure_virtual_machines(resource_group: 'MyResourceGroup').where { name.include?('WindowsVm') } do
  it { should exist }
end

Test that there are virtual machine that includes a certain string in their names (Server Side Filtering via Generic Resource - Recommended):

describe azure_generic_resources(resource_group: 'RESOURCE_GROUP', resource_provider: 'Microsoft.Compute/virtualMachine', substring_of_name: 'WindowsVm') do
  it { should exist }
end

Matchers

For a full list of available matchers, see our Universal Matchers page.

This resource has the following special matchers.

exists

# Should not exist if no virtual machines are in the resource group.

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP') do
  it { should_not exist }
end

not_exists

# Should exist if the filter returns a single virtual machine.

describe azure_virtual_machines(resource_group: 'RESOURCE_GROUP').where(platform: 'windows') do
  it { should exist }
end

Azure permissions

Your Service Principal must be set up with at least a contributor role on the subscription you wish to test.

Thank you for your feedback!

×