Skip to main content

azure_virtual_machine resource

Use the azure_virtual_machine InSpec audit resource to test the properties related to a virtual machine.

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

resource_group and virtual machine name, or the resource_id are required parameters.

describe azure_virtual_machine(resource_group: 'RESOURCE_GROUP', name: 'VM_NAME') do
  it { should exist }
end
describe azure_virtual_machine(resource_id: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vmName}') do
  it { should exist }
end

Parameters

resource_group
Azure resource group where the targeted resource resides.
name
Name of the Azure resource to test.
resource_id
The unique resource ID.

Either one of the parameter sets can be provided for a valid query:

  • resource_id
  • resource_group and name

Properties

admin_username
The admin user name.
resources
The virtual machine child extension resources.
zones
The virtual machine’s availability zones. its('zones') should include('zone1', 'zone2').
installed_extensions_types
List of all installed extensions’ types for the virtual machine. its('installed_extensions_types') { should include('ExtensionType') }.
installed_extensions_names
List of all installed extensions’ names for the virtual machine. its('installed_extensions_names') { should include('ExtensionName') }.
has_monitoring_agent_installed?
Indicates whether a monitoring agent is installed.
has_endpoint_protection_installed?
Indicates whether a list of endpoint protection extension types are installed. it { should have_endpoint_protection_installed(%w{ep_type_1 ep_type_2}) }.
has_only_approved_extensions?
Indicates whether only provided extension types are installed. it { should have_only_approved_extensions(%w{extension_type_1 extension_type_2}) }.
os_disk_name
The virtual machine’s operating system disk name. its('os_disk_name') { should cmp 'OsDiskName' }.
data_disk_names
The virtual machine’s data disk names. its('data_disk_names') { should include('DataDisk1') }.

For properties applicable to all resources, such as type, name, id, and properties, refer to azure_generic_resource.

Also, see the Azure documentation for other available properties. You can access any attribute in the response with the key names separated by dots (.).

Examples

Ensure that the virtual machine has the expected data Disks:

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  its('data_disk_names') { should include('DataDisk1') }
end

Ensure that the Virtual Machine has the Expected Monitoring Agent Installed:

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_monitoring_agent_installed }
end

Matchers

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

exists

# If a virtual machine is found, it will exist.

describe azure_virtual_machine(resource_group: 'RESOURCE_GROUP', name: 'VM_NAME') do
  it { should exist }
end

# virtual machines that are not found, will not exist.

describe azure_virtual_machine(resource_group: 'RESOURCE_GROUP', name: 'VM_NAME') do
  it { should_not exist }
end

have_only_approved_extensions

# Check if a virtual machine has only approved extensions. The check will fail if an extension is used that's not on the list.

describe azure_virtual_machine(resource_group: 'RESOURCE_GROUP', name: 'VM_NAME') do
  it { should have_only_approved_extensions(['ApprovedExtension', 'OtherApprovedExtensions']) }
end

have_monitoring_agent_installed

# Will be true if the MicrosoftMonitoringAgent is installed (Windows only).

describe azure_virtual_machine(resource_group: 'MyResourceGroup', name: 'MyVmName') do
  it { should have_monitoring_agent_installed }
end

have_endpoint_protection_installed

# Will be true if any of the given extensions are installed.

describe azure_virtual_machine(resource_group: 'RESOURCE_GROUP', name: 'VM_NAME') do
  it { should have_endpoint_protection_installed(['Extension1', 'Extension2']) }
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!

×