From b6a013c8d58f359811bbd555684aaa961842c6ff Mon Sep 17 00:00:00 2001 From: Markus Katharina Brechtel Date: Sun, 10 May 2020 16:46:59 +0200 Subject: [PATCH] terraform hetzner volumes --- .gitignore | 1 + inventory_input.tf | 8 +++++++- provider_hetzner.tf | 43 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2098ce8..720e01e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.auto.tfvars *.tfstate *.tfstate.backup +inventory.json diff --git a/inventory_input.tf b/inventory_input.tf index 8daf759..f629ad3 100644 --- a/inventory_input.tf +++ b/inventory_input.tf @@ -1,6 +1,12 @@ variable "vm_hosts" { default = { - "controller.testing.thengo.net" = { provider = "hetzner", groups = [ "controllers" ] } + "controller.testing.thengo.net" = { + provider = "hetzner" + groups = [ "controllers" ] + volumes = { + data = { size = 32 } + } + } #"ns.testing.thengo.net" = { provider = "hetzner", groups = [ "nameservers" ] } } } diff --git a/provider_hetzner.tf b/provider_hetzner.tf index e5d6416..f0cac7a 100644 --- a/provider_hetzner.tf +++ b/provider_hetzner.tf @@ -83,12 +83,38 @@ resource "hcloud_server" "vms" { for_each = local.hetzner_vm_hosts } -#resource "hcloud_server_network" "vms" { -# server_id = hcloud_server.vms[each.key].id -# network_id = hcloud_network.internal_network.id -# -# for_each = local.hetzner_vm_hosts -#} + +# Volumes + +locals { + hetzner_volumes = { for item in flatten([ + for hostname, host in local.hetzner_vm_hosts: [ + for volumename, volume in lookup(host, "volumes", {}): { + hostname = hostname + volumename = volumename + volume = volume + host = host + } + ] + ]): "${item.hostname}--${item.volumename}" => merge(item.volume,{hostname=item.hostname}) } +} + +resource "hcloud_volume" "volumes" { + name = each.key + + location = var.hetzner_default_location + size = each.value.size + + for_each = local.hetzner_volumes +} + +resource "hcloud_volume_attachment" "volume_attachments" { + volume_id = hcloud_volume.volumes[each.key].id + server_id = hcloud_server.vms[each.value.hostname].id + #automount = true + for_each = local.hetzner_volumes +} + # Provider Variable @@ -100,6 +126,11 @@ locals { external_ip = hcloud_server.vms[hostname].ipv4_address ansible_host = hcloud_server.vms[hostname].ipv4_address #internal_ip = hcloud_server_network.vms[hostname].ip + volumes = { + for volumename, volume in lookup(host, "volumes", {}): volumename => merge(volume,{ + device = hcloud_volume.volumes["${hostname}--${volumename}"].linux_device + }) + } } if host.provider == "hetzner" }