-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdc.tf
More file actions
134 lines (116 loc) · 5.1 KB
/
Copy pathdc.tf
File metadata and controls
134 lines (116 loc) · 5.1 KB
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
resource "azurerm_network_interface" "nics" {
for_each = var.dcs
name = "nic-${each.key}"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "ip-conf-nic-${each.key}"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Static"
private_ip_address = each.value.private_ip_address
}
}
resource "azurerm_windows_virtual_machine" "dcs" {
for_each = var.dcs
name = each.key
computer_name = each.key
custom_data = base64encode(file("${path.module}/scripts/Configure-DSC.ps1"))
location = var.location
resource_group_name = var.resource_group_name
network_interface_ids = [azurerm_network_interface.nics[each.key].id]
size = each.value.vm_size
admin_username = each.value.admin_username
admin_password = each.value.admin_password
availability_set_id = azurerm_availability_set.availabilityset.id
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}
resource "azurerm_virtual_machine_extension" "createMgmtADForest" {
for_each = { for k, v in var.dcs : k => v if v.type == "primary" }
name = "createMgmtADForest${each.key}"
virtual_machine_id = azurerm_windows_virtual_machine.dcs[each.key].id
publisher = "Microsoft.Powershell"
type = "DSC"
type_handler_version = "2.77"
depends_on = [azurerm_windows_virtual_machine.dcs]
settings = <<SETTINGS
{
"WmfVersion": "latest",
"configuration": {
"url": "https://raw.githubusercontent.com/canada-ca-terraform-modules/terraform-azurerm-caf-adds/v1.1.0/DSC/CreateADRootDC1.ps1.zip",
"script": "CreateADRootDC1.ps1",
"function": "CreateADRootDC1"
},
"configurationArguments": {
"DomainName": "${var.ad_domain_name}",
"DnsForwarder": "168.63.129.16"
}
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"configurationArguments": {
"adminCreds": {
"UserName": "${azurerm_windows_virtual_machine.dcs[each.key].admin_username}",
"Password": "${azurerm_windows_virtual_machine.dcs[each.key].admin_password}"
}
}
}
PROTECTED_SETTINGS
}
resource "azurerm_virtual_machine_extension" "addMgmtADSecondaryDC" {
for_each = { for k, v in var.dcs : k => v if v.type == "secondary" }
name = "addMgmtADSecondaryDC${each.key}"
virtual_machine_id = azurerm_windows_virtual_machine.dcs[each.key].id
publisher = "Microsoft.Powershell"
type = "DSC"
type_handler_version = "2.77"
depends_on = [azurerm_windows_virtual_machine.dcs]
# depends_on = [azurerm_virtual_machine_extension.createMgmtADForest]
settings = <<SETTINGS
{
"WmfVersion": "latest",
"configuration": {
"url": "https://raw.githubusercontent.com/canada-ca-terraform-modules/terraform-azurerm-caf-adds/v1.1.0/DSC/ConfigureADNextDC.ps1.zip",
"script": "ConfigureADNextDC.ps1",
"function": "ConfigureADNextDC"
},
"configurationArguments": {
"domainName": "${var.ad_domain_name}",
"DNSServer": "${[for k, v in var.dcs : v.private_ip_address if v.type == "primary"][0]}",
"DnsForwarder": "${[for k, v in var.dcs : v.private_ip_address if v.type == "primary"][0]}"
}
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"configurationArguments": {
"adminCreds": {
"UserName": "${azurerm_windows_virtual_machine.dcs[each.key].admin_username}",
"Password": "${azurerm_windows_virtual_machine.dcs[each.key].admin_password}"
}
}
}
PROTECTED_SETTINGS
}
resource "azurerm_virtual_machine_extension" "CustomScriptExtension" {
name = "CustomScriptExtension"
for_each = var.dcs
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
virtual_machine_id = azurerm_windows_virtual_machine.dcs[each.key].id
settings = <<SETTINGS
{
"commandToExecute": "powershell -command Set-ExecutionPolicy RemoteSigned -force; powershell -command copy-item \"c:\\AzureData\\CustomData.bin\" \"c:\\AzureData\\CustomData.ps1\";\"c:\\AzureData\\CustomData.ps1\""
}
SETTINGS
}