Skip to content

Instantly share code, notes, and snippets.

@alban
Last active October 31, 2021 12:25
Show Gist options
  • Save alban/af2d0b53cb1c2ae172464cb783f658fe to your computer and use it in GitHub Desktop.
Save alban/af2d0b53cb1c2ae172464cb783f658fe to your computer and use it in GitHub Desktop.
Terraform snippets for Flatcar on Hetzner Cloud
variable "worker_names" {
type = "list"
}
# worker_names = [
# "w1",
# "w2",
# "w3",
# ]
variable "hcloud_ssh_keys_names" {
type = "list"
}
# hcloud_ssh_keys_names = [
# "[email protected]",
# "[email protected]",
# ]
variable "ssh_authorized_keys" {
type = "list"
}
# ssh_authorized_keys = [
# "ssh-rsa AAA... [email protected]",
# "ssh-rsa ABC... [email protected]",
# ]
variable "worker_clc_snippets" {
type = "list"
description = "Worker Container Linux Config snippets"
default = []
}
variable "worker_networkds" {
type = "list"
description = "Worker Container Linux config networkd section"
default = []
}
data "template_file" "worker_config" {
count = "${length(var.worker_names)}"
template = "${file("${path.module}/cl/worker.yaml.tmpl")}"
vars = {
domain_name = "${element(var.worker_domains, count.index)}"
ssh_authorized_keys = "${jsonencode(var.ssh_authorized_keys)}"
# Terraform evaluates both sides regardless and element cannot be used on 0 length lists
networkd_content = "${length(var.worker_networkds) == 0 ? "" : element(concat(var.worker_networkds, list("")), count.index)}"
}
}
data "ct_config" "worker_ign" {
count = "${length(var.worker_names)}"
content = "${element(data.template_file.worker_config.*.rendered, count.index)}"
pretty_print = false
snippets = ["${var.worker_clc_snippets}"]
}
resource "hcloud_server" "workers" {
count = "${length(var.worker_names)}"
name = "${format("worker-%s", element(var.worker_names, count.index))}"
image = "debian-9"
server_type = "cx11"
ssh_keys = "${var.hcloud_ssh_keys_names}"
datacenter = "fsn1-dc8"
rescue = "linux64"
connection {
host = "${self.ipv4_address}"
timeout = "1m"
}
provisioner "file" {
content = "${element(data.ct_config.worker_ign.*.rendered, count.index)}"
destination = "/root/ignition.json"
}
provisioner "remote-exec" {
script = "${path.module}/install-flatcar.bash"
}
provisioner "remote-exec" {
connection {
host = "${self.ipv4_address}"
timeout = "2m"
user = "core"
}
inline = "sudo hostnamectl set-hostname ${element(var.worker_names, count.index)}"
}
}
#!/bin/bash
set -ex
curl -fsSLO https://raw.githubusercontent.com/flatcar-linux/init/flatcar-master/bin/flatcar-install
chmod +x flatcar-install
./flatcar-install -d /dev/sda -i /root/ignition.json
reboot
---
systemd:
units:
storage:
files:
- path: /etc/hostname
filesystem: root
mode: 0644
contents:
inline:
${domain_name}
networkd:
${networkd_content}
passwd:
users:
- name: core
ssh_authorized_keys: ${ssh_authorized_keys}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment