-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-vm-deployment-template.py
203 lines (175 loc) · 5.41 KB
/
azure-vm-deployment-template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Microsoft Azure Python Script - Resource Group Creation Example
Mickal Speller for THATCLIGUY.com
Sept 2019, version 1.0
Note: Script is free to use without any warranty and/or support from Mickal Speller (thatcliguy.com).
For full and complete details, please reference the Azure SDK for Python documentation from Microsoft at https://docs.microsoft.com/en-us/azure/python/
"""
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import DiskCreateOption
SUBSCRIPTION_ID = 'INPUT_YOUR_AZURE_SUBSCRIPTION_ID'
GROUP_NAME = 'INPUT_GROUP_NAME'
LOCATION = 'INPUT_LOCATION'
VM_NAME = 'INPUT_VM_NAME'
def get_credentials():
credentials = ServicePrincipalCredentials(
client_id = 'INPUT_YOUR_AZURE_CLIENT_ID',
secret = 'INPUT_YOUR_AZURE_SECRET',
tenant = 'INPUT_YOUR_AZURE_TENANT_ID'
)
return credentials
def create_resource_group(resource_group_client):
resource_group_params = { 'location':LOCATION }
resource_group_result = resource_group_client.resource_groups.create_or_update(
GROUP_NAME,
resource_group_params
)
def create_availability_set(compute_client):
avset_params = {
'location': LOCATION,
'sku': { 'name': 'Aligned' },
'platform_fault_domain_count': 3
}
availability_set_result = compute_client.availability_sets.create_or_update(
GROUP_NAME,
'myAVSet',
avset_params
)
def create_public_ip_address(network_client):
public_ip_addess_params = {
'location': LOCATION,
'public_ip_allocation_method': 'Dynamic'
}
creation_result = network_client.public_ip_addresses.create_or_update(
GROUP_NAME,
'myIPAddress',
public_ip_addess_params
)
return creation_result.result()
def create_vnet(network_client):
vnet_params = {
'location': LOCATION,
'address_space': {
'address_prefixes': ['10.0.0.0/16']
}
}
creation_result = network_client.virtual_networks.create_or_update(
GROUP_NAME,
'myVNet',
vnet_params
)
return creation_result.result()
def create_subnet(network_client):
subnet_params = {
'address_prefix': '10.0.0.0/24'
}
creation_result = network_client.subnets.create_or_update(
GROUP_NAME,
'myVNet',
'mySubnet',
subnet_params
)
return creation_result.result()
def create_nic(network_client):
subnet_info = network_client.subnets.get(
GROUP_NAME,
'myVNet',
'mySubnet'
)
publicIPAddress = network_client.public_ip_addresses.get(
GROUP_NAME,
'myIPAddress'
)
nic_params = {
'location': LOCATION,
'ip_configurations': [{
'name': 'myIPConfig',
'public_ip_address': publicIPAddress,
'subnet': {
'id': subnet_info.id
}
}]
}
creation_result = network_client.network_interfaces.create_or_update(
GROUP_NAME,
'myNic',
nic_params
)
return creation_result.result()
def create_vm(network_client, compute_client):
nic = network_client.network_interfaces.get(
GROUP_NAME,
'myNic'
)
avset = compute_client.availability_sets.get(
GROUP_NAME,
'myAVSet'
)
vm_parameters = {
'location': LOCATION,
'os_profile': {
'computer_name': VM_NAME,
'admin_username': 'yourusername',
'admin_password': 'Yourpassword01!'
},
'hardware_profile': {
'vm_size': 'Standard_DS1'
},
'storage_profile': {
'image_reference': {
'publisher': 'Canonical',
'offer': 'UbuntuServer',
'sku': '18.04-LTS',
'version': 'latest'
}
},
'network_profile': {
'network_interfaces': [{
'id': nic.id
}]
},
'availability_set': {
'id': avset.id
}
}
creation_result = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
VM_NAME,
vm_parameters
)
return creation_result.result()
def start_vm(compute_client):
compute_client.virtual_machines.start(GROUP_NAME, VM_NAME)
if __name__ == "__main__":
credentials = get_credentials()
resource_group_client = ResourceManagementClient(
credentials,
SUBSCRIPTION_ID
)
network_client = NetworkManagementClient(
credentials,
SUBSCRIPTION_ID
)
compute_client = ComputeManagementClient(
credentials,
SUBSCRIPTION_ID
)
create_resource_group(resource_group_client)
print("\nCreating Resource Group")
create_availability_set(compute_client)
print("\nCreating Availability Set")
creation_result = create_public_ip_address(network_client)
print("\nCreating Public IP")
creation_result = create_vnet(network_client)
print("\nCreating VNET")
creation_result = create_subnet(network_client)
print("\nCreating Subnet")
creation_result = create_nic(network_client)
print("\nCreating NIC")
creation_result = create_vm(network_client, compute_client)
print("\nCreating Virtual Machine")
start_vm(compute_client)
print("\nComplete! Starting Virtual Machine")