-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-test.tf
95 lines (83 loc) · 2.15 KB
/
jenkins-test.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
## Dependencies
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
version = "~>1.35"
}
}
}
# Variables
variable "hcloud_token" {
type = string
description = "Token to the Hetzner Cloud API"
}
variable "username" {
type = string
description = "User to be provisioned and used for ansible"
default = "jenkins"
}
variable "server_size" {
type = string
description = "Server Sizing"
default = "cx41"
}
variable "jenkins_path" {
type = string
description = "Workdir for agent"
default = "/opt/jenkins"
}
variable "node_name" {
type = string
description = "name of the node"
default = "jenkins-agent"
}
provider "hcloud" {
token = var.hcloud_token
}
data "hcloud_ssh_keys" "jenkins_master_keys" {
with_selector = "target=jenkins"
}
data "hcloud_image" "jenkins" {
with_selector = "jenkins=agent"
}
resource "hcloud_server" "jenkins-slave" {
name = var.node_name
image = data.hcloud_image.jenkins.id
server_type = var.server_size
location = "nbg1"
// ssh_keys = data.hcloud_ssh_keys.jenkins_master_keys.ssh_keys.*.name
provisioner "remote-exec" {
inline = [
"sleep 30",
"adduser --disabled-password --gecos \"\" ${var.username}",
"adduser ${var.username} sudo",
"mkdir /home/${var.username}/.ssh",
"touch /home/${var.username}/.ssh/authorized_keys",
"chown -R ${var.username}:${var.username} /home/${var.username}/.ssh ",
"chmod 700 /home/${var.username}/.ssh ",
"chmod 600 /home/${var.username}/.ssh/authorized_keys ",
"mkdir -p ${var.jenkins_path}",
"chmod 755 ${var.jenkins_path}",
"chown ${var.username}:${var.username} ${var.jenkins_path}",
"adduser ${var.username} docker"
]
connection {
host = self.ipv4_address
type = "ssh"
user = "root"
}
}
provisioner "file" {
content = "${data.hcloud_ssh_keys.jenkins_master_keys.ssh_keys.0.public_key}"
destination = "/home/${var.username}/.ssh/authorized_keys"
connection {
host = self.ipv4_address
type = "ssh"
user = "root"
}
}
}
output "instance_ip_addr" {
value = hcloud_server.jenkins-slave.ipv4_address
}