-
Notifications
You must be signed in to change notification settings - Fork 0
/
step-04-add-runners.tf.off
130 lines (121 loc) · 4.41 KB
/
step-04-add-runners.tf.off
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
# Create a new instance of the latest Ubuntu 14.04 on an
# t2.micro node with an AWS Tag naming it "HelloWorld"
resource "aws_instance" "runner" {
count = 2
ami = "ami-e3fdd999"
instance_type = "t2.small"
connection {
bastion_host = "${aws_instance.linux-bastion.public_ip}"
host = "${self.private_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "remote-exec" {
inline = [
"cp /etc/hosts ./",
"echo \"${aws_instance.chef-server.private_ip} CHANGEME.chef.io\" >> ./hosts",
"sudo cp ./hosts /etc/hosts"
]
}
provisioner "remote-exec" {
inline = [
"cp /etc/hosts ./",
"echo \"${aws_instance.CHANGEME.private_ip} CHANGEME-CHANGEME.com\" >> ./hosts",
"sudo cp ./hosts /etc/hosts"
]
}
# associate_public_ip_address = false
subnet_id = "${aws_subnet.chef_02_private.id}"
key_name = "${aws_key_pair.CHANGEME.id}"
vpc_security_group_ids = ["${aws_security_group.chef_internal.id}"]
tags = "${merge(var.default_tags, map(
"Name", "${var.prefix}-runner-${count.index}",
))}"
}
resource "null_resource" "generate_runner_rsa" {
depends_on = ["aws_instance.CHANGEME","null_resource.chef_server-automate-config"]
connection {
host = "${aws_instance.CHANGEME.public_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "remote-exec" {
inline = [
"[[ -e \".ssh/runner_rsa\" ]] && mv \".ssh/runner_rsa\" \".ssh/runner_rsa.bak.`date +%d%m%Y%H%M%S`\"",
"[[ -e \".ssh/runner_rsa.pub\" ]] && mv \".ssh/runner_rsa.pub\" \".ssh/runner_rsa.pub.bak.`date +%d%m%Y%H%M%S`\"",
"ssh-keygen -t rsa -N \"\" -f .ssh/runner_rsa",
"chmod 600 .ssh/runner_rsa"
]
}
provisioner "local-exec" {
command = "scp -r -o stricthostkeychecking=no -i ${var.ssh_key["file"]} ${var.ssh_username}@${aws_instance.CHANGEME.public_ip}:.ssh/runner_rsa* .chef/"
}
}
resource "null_resource" "copy_runner_rsa_pub_0" {
depends_on = ["null_resource.generate_runner_rsa","null_resource.chef_server-automate-config"]
connection {
bastion_host = "${aws_instance.linux-bastion.public_ip}"
host = "${aws_instance.runner.0.private_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "file" {
source = ".chef/runner_rsa.pub"
destination = "./runner_rsa.pub"
}
provisioner "remote-exec" {
inline = [
"cat ./runner_rsa.pub >> .ssh/authorized_keys"
]
}
}
resource "null_resource" "add_runner_0" {
depends_on = ["null_resource.copy_runner_rsa_pub_0","null_resource.chef_server-automate-config"]
connection {
host = "${aws_instance.CHANGEME.public_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "remote-exec" {
inline = [
"cp /etc/hosts ./",
"echo \"${aws_instance.runner.0.private_ip} runner-0.chef.io\" >> ./hosts",
"sudo cp ./hosts /etc/hosts",
"sudo automate-ctl install-runner runner-0.chef.io centos -i ~/.ssh/runner_rsa -y"
]
}
}
resource "null_resource" "copy_runner_rsa_pub_1" {
depends_on = ["null_resource.generate_runner_rsa","null_resource.chef_server-automate-config"]
connection {
bastion_host = "${aws_instance.linux-bastion.public_ip}"
host = "${aws_instance.runner.1.private_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "file" {
source = ".chef/runner_rsa.pub"
destination = "./runner_rsa.pub"
}
provisioner "remote-exec" {
inline = [
"cat ./runner_rsa.pub >> .ssh/authorized_keys"
]
}
}
resource "null_resource" "add_runner_1" {
depends_on = ["null_resource.copy_runner_rsa_pub_1","null_resource.chef_server-automate-config","null_resource.add_runner_0"]
connection {
host = "${aws_instance.CHANGEME.public_ip}"
user = "${var.ssh_username}"
private_key = "${file("${var.ssh_key["file"]}")}"
}
provisioner "remote-exec" {
inline = [
"cp /etc/hosts ./",
"echo \"${aws_instance.runner.1.private_ip} runner-1.chef.io\" >> ./hosts",
"sudo cp ./hosts /etc/hosts",
"sudo automate-ctl install-runner runner-1.chef.io centos -i ~/.ssh/runner_rsa -y"
]
}
}