forked from Azure-Samples/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_create_windows.yml
88 lines (79 loc) · 3.6 KB
/
vm_create_windows.yml
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
# Description
# ===========
# This playbook creates an Azure Windows VM with public IP. It also cobnfigures the machine to be accessible via Ansible using WinRM.
# This playbook originally comes from @jborean93 (https://github.com/jborean93/ansible-win-demos)
- hosts: localhost
tasks:
- name: Prepare random postfix
set_fact:
rpfx: "{{ 100000 | random }}"
run_once: yes
- name: provision new azure host
hosts: localhost
connection: local
vars:
resource_group: "{{ resource_group_name }}"
vm_name: wintestvm{{ rpfx }}
vm_user: azureuser
vm_password: MyPassword123!!!
location: eastus
# Below is UTF-16 Base64 encoding for:
# Invoke-Expression -Command ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1')); Enable-WSManCredSSP -Role Server -Force
winrm_enable_script: SQBuAHYAbwBrAGUALQBFAHgAcAByAGUAcwBzAGkAbwBuACAALQBDAG8AbQBtAGEAbgBkACAAKAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABTAHkAcwB0AGUAbQAuAE4AZQB0AC4AVwBlAGIAQwBsAGkAZQBuAHQAKQAuAEQAbwB3AG4AbABvAGEAZABTAHQAcgBpAG4AZwAoACcAaAB0AHQAcABzADoALwAvAHIAYQB3AC4AZwBpAHQAaAB1AGIAdQBzAGUAcgBjAG8AbgB0AGUAbgB0AC4AYwBvAG0ALwBhAG4AcwBpAGIAbABlAC8AYQBuAHMAaQBiAGwAZQAvAGQAZQB2AGUAbAAvAGUAeABhAG0AcABsAGUAcwAvAHMAYwByAGkAcAB0AHMALwBDAG8AbgBmAGkAZwB1AHIAZQBSAGUAbQBvAHQAaQBuAGcARgBvAHIAQQBuAHMAaQBiAGwAZQAuAHAAcwAxACcAKQApADsAIABFAG4AYQBiAGwAZQAtAFcAUwBNAGEAbgBDAHIAZQBkAFMAUwBQACAALQBSAG8AbABlACAAUwBlAHIAdgBlAHIAIAAtAEYAbwByAGMAZQA=
tasks:
- name: create Azure resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: '{{ location }}'
state: present
- name: create Azure virtual network in resource group
azure_rm_virtualnetwork:
name: "{{ vm_name }}"
resource_group: "{{ resource_group }}"
address_prefixes_cidr:
- 10.1.0.0/16
state: present
- name: create Azure subnet in virtualnetwork
azure_rm_subnet:
name: '{{ vm_name }}'
state: present
virtual_network_name: "{{ vm_name }}"
resource_group: "{{ resource_group }}"
address_prefix_cidr: 10.1.0.0/24
- name: create Azure storage account
azure_rm_storageaccount:
name: '{{ vm_name }}'
resource_group: "{{ resource_group }}"
account_type: Standard_LRS
- name: provision new Azure virtual host
azure_rm_virtualmachine:
admin_username: '{{ vm_user }}'
admin_password: "{{ vm_password }}"
os_type: Windows
image:
offer: WindowsServer
publisher: MicrosoftWindowsServer
sku: 2016-Datacenter
version: latest
name: "{{ vm_name }}"
resource_group: "{{ resource_group }}"
state: present
vm_size: Standard_D1
storage_account_name: "{{ vm_name }}"
virtual_network_name: "{{ vm_name }}"
subnet_name: "{{ vm_name }}"
- name: create Azure vm extension to enable HTTPS WinRM listener
azure_rm_virtualmachine_extension:
name: winrm-extension
resource_group: "{{ resource_group }}"
virtual_machine_name: "{{ vm_name }}"
publisher: Microsoft.Compute
virtual_machine_extension_type: CustomScriptExtension
type_handler_version: 1.9
settings: '{"commandToExecute": "powershell.exe -ExecutionPolicy ByPass -EncodedCommand {{winrm_enable_script}}"}'
auto_upgrade_minor_version: true
- name: wait for the WinRM port to come online
wait_for:
port: 5986
host: '{{azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.publicIPAddress.properties.ipAddress}}'
timeout: 600