1
+ # Import the needed credential and management objects from the libraries.
2
+ from azure .identity import AzureCliCredential
3
+ from azure .mgmt .resource import ResourceManagementClient
4
+ from azure .mgmt .network import NetworkManagementClient
5
+ from azure .mgmt .compute import ComputeManagementClient
6
+ import os
7
+
8
+ print (f"Provisioning a virtual machine...some operations might take a minute or two." )
9
+
10
+ # Acquire a credential object using CLI-based authentication.
11
+ credential = AzureCliCredential ()
12
+
13
+ # Retrieve subscription ID from environment variable.
14
+ subscription_id = os .environ ["AZURE_SUBSCRIPTION_ID" ]
15
+
16
+
17
+ # Step 1: Provision a resource group
18
+
19
+ # Obtain the management object for resources, using the credentials from the CLI login.
20
+ resource_client = ResourceManagementClient (credential , subscription_id )
21
+
22
+ # Constants we need in multiple places: the resource group name and the region
23
+ # in which we provision resources. You can change these values however you want.
24
+ RESOURCE_GROUP_NAME = "PythonAzureExample-VM-rg"
25
+ LOCATION = "westus2"
26
+
27
+ # Provision the resource group.
28
+ rg_result = resource_client .resource_groups .create_or_update (RESOURCE_GROUP_NAME ,
29
+ {
30
+ "location" : LOCATION
31
+ }
32
+ )
33
+
34
+
35
+ print (f"Provisioned resource group { rg_result .name } in the { rg_result .location } region" )
36
+
37
+ # For details on the previous code, see Example: Provision a resource group
38
+ # at https://docs.microsoft.com/azure/developer/python/azure-sdk-example-resource-group
39
+
40
+
41
+ # Step 2: provision a virtual network
42
+
43
+ # A virtual machine requires a network interface client (NIC). A NIC requires
44
+ # a virtual network and subnet along with an IP address. Therefore we must provision
45
+ # these downstream components first, then provision the NIC, after which we
46
+ # can provision the VM.
47
+
48
+ # Network and IP address names
49
+ VNET_NAME = "python-example-vnet"
50
+ SUBNET_NAME = "python-example-subnet"
51
+ IP_NAME = "python-example-ip"
52
+ IP_CONFIG_NAME = "python-example-ip-config"
53
+ NIC_NAME = "python-example-nic"
54
+
55
+ # Obtain the management object for networks
56
+ network_client = NetworkManagementClient (credential , subscription_id )
57
+
58
+ # Provision the virtual network and wait for completion
59
+ poller = network_client .virtual_networks .begin_create_or_update (RESOURCE_GROUP_NAME ,
60
+ VNET_NAME ,
61
+ {
62
+ "location" : LOCATION ,
63
+ "address_space" : {
64
+ "address_prefixes" : ["10.0.0.0/16" ]
65
+ }
66
+ }
67
+ )
68
+
69
+ vnet_result = poller .result ()
70
+
71
+ print (f"Provisioned virtual network { vnet_result .name } with address prefixes { vnet_result .address_space .address_prefixes } " )
72
+
73
+ # Step 3: Provision the subnet and wait for completion
74
+ poller = network_client .subnets .begin_create_or_update (RESOURCE_GROUP_NAME ,
75
+ VNET_NAME , SUBNET_NAME ,
76
+ { "address_prefix" : "10.0.0.0/24" }
77
+ )
78
+ subnet_result = poller .result ()
79
+
80
+ print (f"Provisioned virtual subnet { subnet_result .name } with address prefix { subnet_result .address_prefix } " )
81
+
82
+ # Step 4: Provision an IP address and wait for completion
83
+ poller = network_client .public_ip_addresses .begin_create_or_update (RESOURCE_GROUP_NAME ,
84
+ IP_NAME ,
85
+ {
86
+ "location" : LOCATION ,
87
+ "sku" : { "name" : "Standard" },
88
+ "public_ip_allocation_method" : "Static" ,
89
+ "public_ip_address_version" : "IPV4"
90
+ }
91
+ )
92
+
93
+ ip_address_result = poller .result ()
94
+
95
+ print (f"Provisioned public IP address { ip_address_result .name } with address { ip_address_result .ip_address } " )
96
+
97
+ # Step 5: Provision the network interface client
98
+ poller = network_client .network_interfaces .begin_create_or_update (RESOURCE_GROUP_NAME ,
99
+ NIC_NAME ,
100
+ {
101
+ "location" : LOCATION ,
102
+ "ip_configurations" : [ {
103
+ "name" : IP_CONFIG_NAME ,
104
+ "subnet" : { "id" : subnet_result .id },
105
+ "public_ip_address" : {"id" : ip_address_result .id }
106
+ }]
107
+ }
108
+ )
109
+
110
+ nic_result = poller .result ()
111
+
112
+ print (f"Provisioned network interface client { nic_result .name } " )
113
+
114
+ # Step 6: Provision the virtual machine
115
+
116
+ # Obtain the management object for virtual machines
117
+ compute_client = ComputeManagementClient (credential , subscription_id )
118
+
119
+ VM_NAME = "ExampleVM"
120
+ USERNAME = "azureuser"
121
+ PASSWORD = "ChangePa$$w0rd24"
122
+
123
+ print (f"Provisioning virtual machine { VM_NAME } ; this operation might take a few minutes." )
124
+
125
+ # Provision the VM specifying only minimal arguments, which defaults to an Ubuntu 18.04 VM
126
+ # on a Standard DS1 v2 plan with a public IP address and a default virtual network/subnet.
127
+
128
+ poller = compute_client .virtual_machines .begin_create_or_update (RESOURCE_GROUP_NAME , VM_NAME ,
129
+ {
130
+ "location" : LOCATION ,
131
+ "storage_profile" : {
132
+ "image_reference" : {
133
+ "publisher" : 'Canonical' ,
134
+ "offer" : "UbuntuServer" ,
135
+ "sku" : "16.04.0-LTS" ,
136
+ "version" : "latest"
137
+ }
138
+ },
139
+ "hardware_profile" : {
140
+ "vm_size" : "Standard_DS1_v2"
141
+ },
142
+ "os_profile" : {
143
+ "computer_name" : VM_NAME ,
144
+ "admin_username" : USERNAME ,
145
+ "admin_password" : PASSWORD
146
+ },
147
+ "network_profile" : {
148
+ "network_interfaces" : [{
149
+ "id" : nic_result .id ,
150
+ }]
151
+ }
152
+ }
153
+ )
154
+
155
+ vm_result = poller .result ()
156
+
157
+ print (f"Provisioned virtual machine { vm_result .name } " )
0 commit comments