forked from marattm/terraform-linode-module-dbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
70 lines (60 loc) · 1.94 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
locals {
ssh_keys = [
for value in toset(var.authorized_keys) : chomp(file(value))
]
}
locals {
ssh_keys_str = join("\n\n", local.ssh_keys)
}
resource "linode_sshkey" "authKeys" {
for_each = toset(local.ssh_keys)
label = "Initial deploy SSH key"
ssh_key = each.value
}
resource "linode_instance" "db" {
count = var.node_count
label = "${var.SITE}-db${var.ID + count.index}.${var.DOMAIN}"
image = var.image
region = var.region
type = var.instance_type
backups_enabled = var.backups_enabled
authorized_keys = local.ssh_keys
root_pass = random_string.password.result
group = var.group
tags = var.tags
private_ip = true
connection {
type = "ssh"
user = "root"
password = random_string.password.result
host = self.ip_address
}
provisioner "file" {
source = "sshd_public_key_only.conf"
destination = "/etc/ssh/sshd_config.d/sshd_public_key_only.conf"
}
provisioner "file" {
source = "access_setup.sh"
destination = "/tmp/access_setup.sh"
}
provisioner "file" {
source = "user.txt"
destination = "/tmp/user.txt"
}
provisioner "file" {
source = "useradd.sh"
destination = "/tmp/useradd.sh"
}
provisioner "remote-exec" {
inline = [
"sudo chmod +x /tmp/access_setup.sh",
"sudo sh /tmp/access_setup.sh -u ${var.admin_user} -k '${local.ssh_keys_str}'",
"sudo bash -c \"echo '${var.admin_user}:${random_string.password.result}' | sudo chpasswd\"",
"service sshd restart",
"sudo hostnamectl set-hostname '${var.SITE}-db${var.ID + count.index}.${var.DOMAIN}'",
"if [ ${var.create_users} = true ]; then sudo chmod +x /tmp/useradd.sh; fi",
"if [ ${var.create_users} = true ]; then sudo bash /tmp/useradd.sh; fi",
"if [ ${var.create_users} = true ]; then service sshd restart; fi"
]
}
}