-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
162 lines (126 loc) · 4.9 KB
/
main.tf
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
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.51.0"
}
}
}
locals {
# prefix the resource names is the variable has been specified
name = try(length(var.resource_prefix), 0) > 0 ? "${var.resource_prefix}-${var.name}" : var.name
# Accept either admin_ssh_public_key or use a file
admin_ssh_public_key = length(var.admin_ssh_public_key) > 0 ? var.admin_ssh_public_key : file(var.admin_ssh_public_key_file)
# Set a boolean for the connect if the arc object has been set
azcmagent_connect = var.arc == null ? false : true
// And then force azcmagent_download to true
azcmagent_download = local.azcmagent_connect ? true : var.azcmagent
// If connecting then convert map of tags to string of comma delimited key=value pairs and merge back into the arc object
arc_tags_string = local.azcmagent_connect && try(length(var.arc.tags), 0) > 0 ? (
join(",", [for key, value in var.arc.tags :
# "${key}=${replace(value, " ", "_")}"])
# "${key}=\\\"${value}\\\""])
"${key}=${value}"])
) : (
null
)
arc = local.azcmagent_connect ? merge(var.arc, { tags = local.arc_tags_string }) : null
// Construct the input steps for the cloud init based on the booleans.
cloudinit_agent_imds = [{
"name" = "remove_azure_agent_block_imds"
"content" = templatefile("${path.module}/cloud_init/azure_agent_imds.tpl", { hostname = var.name })
}]
cloudinit_download = local.azcmagent_download ? [{
"name" = "azcmagent_download"
"content" = file("${path.module}/cloud_init/azcmagent_install.yaml")
}] : []
cloudinit_connect = local.azcmagent_connect ? [{
"name" = "azcmagent_connect"
"content" = templatefile("${path.module}/cloud_init/azcmagent_connect.tpl", local.arc)
}] : []
// cloudinit_azure_cli = [{
// "name" = "install_azure_cli"
// "content" = file("${path.module}/cloud_init/azure_cli.yaml")
// }]
//
cloud_init = concat(local.cloudinit_agent_imds, local.cloudinit_download, local.cloudinit_connect)
cloudinit_steps = {
for n in range(length(local.cloud_init)) :
format("step%02d", n + 1) => local.cloud_init[n]
}
}
data "cloudinit_config" "multipart" {
gzip = false
base64_encode = false
dynamic "part" {
for_each = local.cloudinit_steps
content {
filename = part.value.name
content_type = "text/cloud-config"
content = part.value.content
}
}
}
# Boolean controlled public IP
resource "azurerm_public_ip" "onprem" {
for_each = toset(var.public_ip ? ["pip"] : [])
name = "${local.name}-pip"
resource_group_name = var.resource_group_name
location = var.location
tags = var.tags
allocation_method = "Static"
domain_name_label = var.dns_label
}
resource "azurerm_network_interface" "onprem" {
name = "${local.name}-nic"
resource_group_name = var.resource_group_name
location = var.location
tags = var.tags
ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = var.public_ip ? azurerm_public_ip.onprem["pip"].id : null
}
}
resource "azurerm_network_interface_application_security_group_association" "onprem" {
network_interface_id = azurerm_network_interface.onprem.id
application_security_group_id = var.asg_id
}
resource "azurerm_linux_virtual_machine" "onprem" {
name = local.name
resource_group_name = var.resource_group_name
location = var.location
tags = var.tags
depends_on = [ azurerm_network_interface_application_security_group_association.onprem ]
computer_name = var.name
admin_username = var.admin_username
disable_password_authentication = true
size = var.size
network_interface_ids = [azurerm_network_interface.onprem.id]
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_disk {
name = "${local.name}-os"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
# Don't provision the Azure Agent - needs to be missing for Azure Arc agent installation
provision_vm_agent = false
allow_extension_operations = false
// custom_data = filebase64("${path.module}/example_cloud_init")
// custom_data = base64encode(templatefile("${path.module}/azure_arc_cloud_init.tpl", { hostname = var.name }))
custom_data = base64encode(data.cloudinit_config.multipart.rendered)
admin_ssh_key {
username = var.admin_username
public_key = local.admin_ssh_public_key
}
// Pointless, but impossible to not have an identity
identity {
type = "SystemAssigned"
}
}