-
Notifications
You must be signed in to change notification settings - Fork 335
/
azure_rm_virtualmachine.py
2697 lines (2436 loc) · 132 KB
/
azure_rm_virtualmachine.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Copyright (c) 2016 Matt Davis, <[email protected]>
# Chris Houseknecht, <[email protected]>
# Copyright (c) 2018 James E. King, III (@jeking3) <[email protected]>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: azure_rm_virtualmachine
version_added: "0.1.2"
short_description: Manage Azure virtual machines
description:
- Manage and configure virtual machines (VMs) and associated resources on Azure.
- Requires a resource group containing at least one virtual network with at least one subnet.
- Supports images from the Azure Marketplace, which can be discovered with M(azure.azcollection.azure_rm_virtualmachineimage_info).
- Supports custom images since Ansible 2.5.
- To use I(custom_data) on a Linux image, the image must have cloud-init enabled. If cloud-init is not enabled, I(custom_data) is ignored.
options:
resource_group:
description:
- Name of the resource group containing the VM.
required: true
name:
description:
- Name of the VM.
required: true
custom_data:
description:
- Data made available to the VM and used by C(cloud-init).
- Only used on Linux images with C(cloud-init) enabled.
- Consult U(https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#cloud-init-overview) for cloud-init ready images.
- To enable cloud-init on a Linux image, follow U(https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cloudinit-prepare-custom-image).
state:
description:
- State of the VM.
- Set to C(present) to create a VM with the configuration specified by other options, or to update the configuration of an existing VM.
- Set to C(absent) to remove a VM.
- Does not affect power state. Use I(started)/I(allocated)/I(restarted) parameters to change the power state of a VM.
default: present
choices:
- absent
- present
started:
description:
- Whether the VM is started or stopped.
- Set to C(true) with I(state=present) to start the VM.
- Set to C(false) to stop the VM.
type: bool
force:
description:
- Use more force during stop of VM
- Set to C(true) with I(started=false) to stop the VM forcefully (hard poweroff -- skip_shutdown)
- Set to C(false) to power off gracefully
default: false
type: bool
allocated:
description:
- Whether the VM is allocated or deallocated, only useful with I(state=present).
default: True
type: bool
generalized:
description:
- Whether the VM is generalized or not.
- Set to C(true) with I(state=present) to generalize the VM.
- Generalizing a VM is irreversible.
type: bool
default: False
restarted:
description:
- Set to C(true) with I(state=present) to restart a running VM.
default: False
type: bool
location:
description:
- Valid Azure location for the VM. Defaults to location of the resource group.
short_hostname:
description:
- Name assigned internally to the host. On a Linux VM this is the name returned by the C(hostname) command.
- When creating a VM, short_hostname defaults to I(name).
vm_size:
description:
- A valid Azure VM size value. For example, C(Standard_D4).
- Choices vary depending on the subscription and location. Check your subscription for available choices.
- Required when creating a VM.
priority:
description:
- Priority of the VM.
- C(None) is the equivalent of Regular VM.
choices:
- None
- Spot
eviction_policy:
description:
- Specifies the eviction policy for the Azure Spot virtual machine.
- Requires priority to be set to Spot.
choices:
- Deallocate
- Delete
max_price:
description:
- Specifies the maximum price you are willing to pay for a Azure Spot VM/VMSS.
- This price is in US Dollars.
- C(-1) indicates default price to be up-to on-demand.
- Requires priority to be set to Spot.
default: -1
admin_username:
description:
- Admin username used to access the VM after it is created.
- Required when creating a VM.
admin_password:
description:
- Password for the admin username.
- Not required if the I(os_type=Linux) and SSH password authentication is disabled by setting I(ssh_password_enabled=false).
ssh_password_enabled:
description:
- Whether to enable or disable SSH passwords.
- When I(os_type=Linux), set to C(false) to disable SSH password authentication and require use of SSH keys.
default: true
type: bool
ssh_public_keys:
description:
- For I(os_type=Linux) provide a list of SSH keys.
- Accepts a list of dicts where each dictionary contains two keys, I(path) and I(key_data).
- Set I(path) to the default location of the authorized_keys files. For example, I(path=/home/<admin username>/.ssh/authorized_keys).
- Set I(key_data) to the actual value of the public key.
image:
description:
- The image used to build the VM.
- For custom images, the name of the image. To narrow the search to a specific resource group, a dict with the keys I(name) and I(resource_group).
- For Marketplace images, a dict with the keys I(publisher), I(offer), I(sku), and I(version).
- Set I(version=latest) to get the most recent version of a given image.
required: true
availability_set:
description:
- Name or ID of an existing availability set to add the VM to. The I(availability_set) should be in the same resource group as VM.
proximity_placement_group:
description:
- The name or ID of the proximity placement group the VM should be associated with.
type: dict
suboptions:
id:
description:
- The ID of the proximity placement group the VM should be associated with.
type: str
name:
description:
- The Name of the proximity placement group the VM should be associated with.
type: str
resource_group:
description:
- The resource group of the proximity placement group the VM should be associated with.
type: str
storage_account_name:
description:
- Name of a 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 C(Standard_LRS).
aliases:
- storage_account
storage_container_name:
description:
- Name of the container to use within the storage account to store VHD blobs.
- If not specified, a default container will be created.
default: vhds
aliases:
- storage_container
storage_blob_name:
description:
- Name of the storage blob used to hold the OS disk image of the VM.
- Must end with '.vhd'.
- If not specified, defaults to the VM name + '.vhd'.
aliases:
- storage_blob
managed_disk_type:
description:
- Managed OS disk type.
- Create OS disk with managed disk if defined.
- If not defined, the OS disk will be created with virtual hard disk (VHD).
choices:
- Standard_LRS
- StandardSSD_LRS
- StandardSSD_ZRS
- Premium_LRS
- Premium_ZRS
- UltraSSD_LRS
os_disk_name:
description:
- OS disk name.
os_disk_caching:
description:
- Type of OS disk caching.
choices:
- ReadOnly
- ReadWrite
aliases:
- disk_caching
os_disk_size_gb:
description:
- Size of OS disk in GB.
os_type:
description:
- Base type of operating system.
choices:
- Windows
- Linux
default: Linux
ephemeral_os_disk:
description:
- Parameters of ephemeral disk settings that can be specified for operating system disk.
- Ephemeral OS disk is only supported for VMS Instances using Managed Disk.
type: bool
data_disks:
description:
- Describes list of data disks.
- Use M(azure.azcollection.azure_rm_mangeddisk) to manage the specific disk.
suboptions:
lun:
description:
- The logical unit number for data disk.
- This value is used to identify data disks within the VM and therefore must be unique for each data disk attached to a VM.
required: true
disk_size_gb:
description:
- The initial disk size in GB for blank data disks.
- This value cannot be larger than C(1023) GB.
- Size can be changed only when the virtual machine is deallocated.
- Not sure when I(managed_disk_id) defined.
managed_disk_type:
description:
- Managed data disk type.
- Only used when OS disk created with managed disk.
choices:
- Standard_LRS
- StandardSSD_LRS
- StandardSSD_ZRS
- Premium_LRS
- Premium_ZRS
- UltraSSD_LRS
storage_account_name:
description:
- Name of an existing storage account that supports creation of VHD blobs.
- If not specified for a new VM, a new storage account started with I(name) will be created using storage type C(Standard_LRS).
- Only used when OS disk created with virtual hard disk (VHD).
- Used when I(managed_disk_type) not defined.
- Cannot be updated unless I(lun) updated.
storage_container_name:
description:
- Name of the container to use within the storage account to store VHD blobs.
- If no name is specified a default container named 'vhds' will created.
- Only used when OS disk created with virtual hard disk (VHD).
- Used when I(managed_disk_type) not defined.
- Cannot be updated unless I(lun) updated.
default: vhds
storage_blob_name:
description:
- Name of the storage blob used to hold the OS disk image of the VM.
- Must end with '.vhd'.
- Default to the I(name) + timestamp + I(lun) + '.vhd'.
- Only used when OS disk created with virtual hard disk (VHD).
- Used when I(managed_disk_type) not defined.
- Cannot be updated unless I(lun) updated.
caching:
description:
- Type of data disk caching.
choices:
- ReadOnly
- ReadWrite
default: ReadOnly
public_ip_allocation_method:
description:
- Allocation method for the public IP of the VM.
- Used only if a network interface is not specified.
- When set to C(Dynamic), the public IP address may change any time the VM is rebooted or power cycled.
- The C(Disabled) choice was added in Ansible 2.6.
choices:
- Dynamic
- Static
- Disabled
default: Static
aliases:
- public_ip_allocation
open_ports:
description:
- List of ports to open in the security group for the VM, when a security group and network interface are created with a VM.
- For Linux hosts, defaults to allowing inbound TCP connections to port 22.
- For Windows hosts, defaults to opening ports 3389 and 5986.
network_interface_names:
description:
- Network interface names to add to the VM.
- Can be a string of name or resource ID of the network interface.
- Can be a dict containing I(resource_group) and I(name) of the network interface.
- If a network interface name is not provided when the VM is created, a default network interface will be created.
- To create a new network interface, at least one Virtual Network with one Subnet must exist.
type: list
aliases:
- network_interfaces
virtual_network_resource_group:
description:
- The resource group to use when creating a VM with another resource group's virtual network.
virtual_network_name:
description:
- The virtual network to use when creating a VM.
- If not specified, a new network interface will be created and assigned to the first virtual network found in the resource group.
- Use with I(virtual_network_resource_group) to place the virtual network in another resource group.
aliases:
- virtual_network
subnet_name:
description:
- Subnet for the VM.
- Defaults to the first subnet found in the virtual network or the subnet of the I(network_interface_name), if provided.
- If the subnet is in another resource group, specify the resource group with I(virtual_network_resource_group).
aliases:
- subnet
created_nsg:
description:
- Whether network security group created and attached to network interface or not.
type: bool
default: True
version_added: '1.16.0'
remove_on_absent:
description:
- Associated resources to remove when removing a VM using I(state=absent).
- To remove all resources related to the VM being removed, including auto-created resources, set to C(all).
- To remove only resources that were automatically created while provisioning the VM being removed, set to C(all_autocreated).
- To remove only specific resources, set to C(network_interfaces), C(virtual_storage) or C(public_ips).
- Any other input will be ignored.
type: list
default: ['all']
plan:
description:
- Third-party billing plan for the VM.
type: dict
suboptions:
name:
description:
- Billing plan name.
required: true
product:
description:
- Product name.
required: true
publisher:
description:
- Publisher offering the plan.
required: true
promotion_code:
description:
- Optional promotion code.
accept_terms:
description:
- Accept terms for Marketplace images that require it.
- Only Azure service admin/account admin users can purchase images from the Marketplace.
- Only valid when a I(plan) is specified.
type: bool
default: false
zones:
description:
- A list of Availability Zones for your VM.
type: list
license_type:
description:
- On-premise license for the image or disk.
- Only used for images that contain the Windows Server operating system.
- To remove all license type settings, set to the string C(None).
choices:
- Windows_Server
- Windows_Client
- RHEL_BYOS
- SLES_BYOS
vm_identity:
description:
- Identity for the VM.
type: dict
suboptions:
type:
description:
- Type of the managed identity
required: true
choices:
- SystemAssigned
- UserAssigned
- SystemAssigned, UserAssigned
- None
type: str
user_assigned_identities:
description:
- User Assigned Managed Identities and its options
required: false
type: dict
suboptions:
id:
description:
- List of the user assigned identities IDs associated to the VM
required: false
type: list
default: []
append:
description:
- If the list of identities has to be appended to current identities (true) or if it has to replace current identities (false)
required: false
type: bool
default: True
winrm:
description:
- List of Windows Remote Management configurations of the VM.
suboptions:
protocol:
description:
- The protocol of the winrm listener.
required: true
choices:
- http
- https
source_vault:
description:
- The relative URL of the Key Vault containing the certificate.
certificate_url:
description:
- The URL of a certificate that has been uploaded to Key Vault as a secret.
certificate_store:
description:
- The certificate store on the VM to which the certificate should be added.
- The specified certificate store is implicitly in the LocalMachine account.
boot_diagnostics:
description:
- Manage boot diagnostics settings for a VM.
- Boot diagnostics includes a serial console and remote console screenshots.
suboptions:
enabled:
description:
- Flag indicating if boot diagnostics are enabled.
required: true
type: bool
type:
description:
- Should the storage account be managed by azure or a custom storage account
required: false
type: str
choices:
- managed
storage_account:
description:
- Only used if I(type) is not set
- The name of an existing storage account to use for boot diagnostics.
- If not specified, uses I(storage_account_name) defined one level up.
- If storage account is not specified anywhere, and C(enabled) is C(true), a default storage account is created for boot diagnostics data.
required: false
resource_group:
description:
- Only used if I(type) is not set
- Resource group where the storage account is located.
type: str
linux_config:
description:
- Specifies the Linux operating system settings on the virtual machine.
suboptions:
disable_password_authentication:
description:
- Specifies whether password authentication should be disabled.
type: bool
windows_config:
description:
- Specifies Windows operating system settings on the virtual machine.
suboptions:
provision_vm_agent:
description:
- Indicates whether virtual machine agent should be provisioned on the virtual machine.
type: bool
required: True
enable_automatic_updates:
description:
- Indicates whether Automatic Updates is enabled for the Windows virtual machine.
type: bool
required: True
security_profile:
description:
- Specifies the Security related profile settings for the virtual machine.
type: dict
suboptions:
encryption_at_host:
description:
- This property can be used by user in the request to enable or disable the Host Encryption for the virtual machine.
- This will enable the encryption for all the disks including Resource/Temp disk at host itself.
type: bool
security_type:
description:
- Specifies the SecurityType of the virtual machine.
- It is set as TrustedLaunch to enable UefiSettings.
type: str
choices:
- TrustedLaunch
uefi_settings:
description:
- Specifies the security settings like secure boot and vTPM used while creating the virtual machine.
type: dict
suboptions:
secure_boot_enabled:
description:
- Specifies whether secure boot should be enabled on the virtual machine.
type: bool
v_tpm_enabled:
description:
- Specifies whether vTPM should be enabled on the virtual machine.
type: bool
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Chris Houseknecht (@chouseknecht)
- Matt Davis (@nitzmahone)
- Christopher Perrin (@cperrin88)
- James E. King III (@jeking3)
'''
EXAMPLES = '''
- name: Create VM with defaults
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm10
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Create an availability set for managed disk vm
azure_rm_availabilityset:
name: avs-managed-disk
resource_group: myResourceGroup
platform_update_domain_count: 5
platform_fault_domain_count: 2
sku: Aligned
- name: Create a VM with managed disk
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: vm-managed-disk
admin_username: "{{ username }}"
availability_set: avs-managed-disk
managed_disk_type: Standard_LRS
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
version: latest
vm_size: Standard_D4
- name: Create a VM with existing storage account and NIC
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
vm_size: Standard_D4
storage_account: testaccount001
admin_username: "{{ username }}"
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your 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: myResourceGroup
name: testvm001
vm_size: Standard_D4
managed_disk_type: Standard_LRS
admin_username: "{{ username }}"
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your ssh public key here... >
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
version: latest
data_disks:
- lun: 0
managed_disk_id: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/disks/myDisk"
- 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: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
ssh_password_enabled: false
ssh_public_keys:
- path: /home/adminUser/.ssh/authorized_keys
key_data: < insert your ssh public key here... >
network_interfaces: testvm001
storage_container: osdisk
storage_blob: osdisk.vhd
boot_diagnostics:
enabled: yes
type: managed
image:
offer: 0001-com-ubuntu-server-focal
publisher: canonical
sku: 20_04-lts-gen2
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: Create a VM with a custom image
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image: customimage001
- name: Create a VM with a custom image from a particular resource group
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
name: customimage001
resource_group: myResourceGroup
- name: Create a VM with an image id
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
id: '{{image_id}}'
- name: Create a VM with spcified OS disk size
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: big-os-disk
admin_username: "{{ username }}"
admin_password: "{{ password }}"
os_disk_size_gb: 512
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Create a VM with OS and Plan, accepting the terms
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: f5-nva
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
publisher: f5-networks
offer: f5-big-ip-best
sku: f5-bigip-virtual-edition-200m-best-hourly
version: latest
plan:
name: f5-bigip-virtual-edition-200m-best-hourly
product: f5-big-ip-best
publisher: f5-networks
- name: Create a VM with Spot Instance
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm10
vm_size: Standard_D4
priority: Spot
eviction_policy: Deallocate
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image:
offer: CentOS
publisher: OpenLogic
sku: '7.1'
version: latest
- name: Power Off
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
started: no
- name: Deallocate
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
allocated: no
- name: Power On
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
- name: Restart
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
restarted: yes
- name: Create a VM with an Availability Zone
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm001
vm_size: Standard_DS1_v2
admin_username: "{{ username }}"
admin_password: "{{ password }}"
image: customimage001
zones: [1]
- name: Create a VM with security profile
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
vm_size: Standard_D4s_v3
managed_disk_type: Standard_LRS
admin_username: "{{ username }}"
admin_password: "{{ password }}"
security_profile:
uefi_settings:
secure_boot_enabled: True
v_tpm_enabled: True
encryption_at_host: True
security_type: TrustedLaunch
ssh_public_keys:
- path: /home/azureuser/.ssh/authorized_keys
key_data: "ssh-rsa *****"
image:
offer: 0001-com-ubuntu-server-jammy
publisher: Canonical
sku: 22_04-lts-gen2
version: latest
- name: Remove a VM and all resources that were autocreated
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: testvm002
remove_on_absent: all_autocreated
state: absent
'''
RETURN = '''
powerstate:
description:
- Indicates if the state is C(running), C(stopped), C(deallocated), C(generalized).
returned: always
type: str
sample: running
deleted_vhd_uris:
description:
- List of deleted Virtual Hard Disk URIs.
returned: 'on delete'
type: list
sample: ["https://testvm104519.blob.core.windows.net/vhds/testvm10.vhd"]
deleted_network_interfaces:
description:
- List of deleted NICs.
returned: 'on delete'
type: list
sample: ["testvm1001"]
deleted_public_ips:
description:
- List of deleted public IP address names.
returned: 'on delete'
type: list
sample: ["testvm1001"]
azure_vm:
description:
- Facts about the current state of the object. Note that facts are not part of the registered output but available directly.
returned: always
type: dict
sample: {
"properties": {
"availabilitySet": {
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Compute/availabilitySets/MYAVAILABILITYSET"
},
"proximityPlacementGroup": {
"id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Compute/proximityPlacementGroups/testid13"
},
"hardwareProfile": {
"vmSize": "Standard_D1"
},
"instanceView": {
"disks": [
{
"name": "testvm10.vhd",
"statuses": [
{
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"time": "2016-03-30T07:11:16.187272Z"
}
]
}
],
"statuses": [
{
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"time": "2016-03-30T20:33:38.946916Z"
},
{
"code": "PowerState/running",
"displayStatus": "VM running",
"level": "Info"
}
],
"vmAgent": {
"extensionHandlers": [],
"statuses": [
{
"code": "ProvisioningState/succeeded",
"displayStatus": "Ready",
"level": "Info",
"message": "GuestAgent is running and accepting new configurations.",
"time": "2016-03-30T20:31:16.000Z"
}
],
"vmAgentVersion": "WALinuxAgent-2.0.16"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkInterfaces/testvm10_NIC01",
"name": "testvm10_NIC01",
"properties": {
"dnsSettings": {
"appliedDnsServers": [],
"dnsServers": []
},
"enableIPForwarding": false,
"ipConfigurations": [
{
"etag": 'W/"041c8c2a-d5dd-4cd7-8465-9125cfbe2cf8"',
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkInterfaces/testvm10_NIC01/ipConfigurations/default",
"name": "default",
"properties": {
"privateIPAddress": "10.10.0.5",
"privateIPAllocationMethod": "Dynamic",
"provisioningState": "Succeeded",
"publicIPAddress": {
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/testvm10_PIP01",
"name": "testvm10_PIP01",
"properties": {
"idleTimeoutInMinutes": 4,
"ipAddress": "13.92.246.197",
"ipConfiguration": {
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkInterfaces/testvm10_NIC01/ipConfigurations/default"
},
"provisioningState": "Succeeded",
"publicIPAllocationMethod": "Static",
"resourceGuid": "3447d987-ca0d-4eca-818b-5dddc0625b42"
}
}
}
}
],
"macAddress": "00-0D-3A-12-AA-14",
"primary": true,
"provisioningState": "Succeeded",
"resourceGuid": "10979e12-ccf9-42ee-9f6d-ff2cc63b3844",
"virtualMachine": {
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Compute/virtualMachines/testvm10"
}
}
}
]
},
"osProfile": {
"adminUsername": "chouseknecht",
"computerName": "test10",
"linuxConfiguration": {
"disablePasswordAuthentication": false
},
"secrets": []
},
"provisioningState": "Succeeded",
"storageProfile": {
"dataDisks": [
{
"caching": "ReadWrite",
"createOption": "empty",
"diskSizeGB": 64,
"lun": 0,
"name": "datadisk1.vhd",
"vhd": {
"uri": "https://testvm10sa1.blob.core.windows.net/datadisk/datadisk1.vhd"
}
}
],
"imageReference": {
"offer": "CentOS",
"publisher": "OpenLogic",
"sku": "7.1",
"version": "7.1.20160308"
},
"osDisk": {
"caching": "ReadOnly",
"createOption": "fromImage",
"name": "testvm10.vhd",
"osType": "Linux",
"vhd": {
"uri": "https://testvm10sa1.blob.core.windows.net/vhds/testvm10.vhd"
}
}
}
},
"type": "Microsoft.Compute/virtualMachines"
}
''' # NOQA
import base64
import random
import re
import time
try:
from azure.core.exceptions import ResourceNotFoundError
from azure.core.polling import LROPoller
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
from msrestazure.tools import parse_resource_id
except ImportError:
# This is handled in azure_rm_common
pass
from ansible.module_utils.basic import to_native, to_bytes
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import (AzureRMModuleBase,
azure_id_to_dict,
normalize_location_name,
format_resource_id
)
AZURE_OBJECT_CLASS = 'VirtualMachine'
AZURE_ENUM_MODULES = ['azure.mgmt.compute.models']
def extract_names_from_blob_uri(blob_uri, storage_suffix):
# HACK: ditch this once python SDK supports get by URI
m = re.match(r'^https://(?P<accountname>[^.]+)\.blob\.{0}/'
r'(?P<containername>[^/]+)/(?P<blobname>.+)$'.format(storage_suffix), blob_uri)
if not m:
raise Exception("unable to parse blob uri '%s'" % blob_uri)
extracted_names = m.groupdict()
return extracted_names
proximity_placement_group_spec = dict(
id=dict(type='str'),
name=dict(type='str'),
resource_group=dict(type='str')
)
windows_configuration_spec = dict(
enable_automatic_updates=dict(type='bool', required=True),
provision_vm_agent=dict(type='bool', required=True),
)
linux_configuration_spec = dict(
disable_password_authentication=dict(type='bool')
)
user_assigned_identities_spec = dict(
id=dict(type='list', default=[]),
append=dict(type='bool', default=True)
)