Explorar el Código

hetzner vms definition

master
padre
commit
8db63cc6b2
Se han modificado 8 ficheros con 119 adiciones y 0 borrados
  1. +4
    -0
      main.tf
  2. +11
    -0
      network.tf
  3. +15
    -0
      outputs.tf
  4. +3
    -0
      provider.tf
  5. +5
    -0
      ssh.tf
  6. +38
    -0
      variables.tf
  7. +15
    -0
      vms.tf
  8. +28
    -0
      volumes.tf

+ 4
- 0
main.tf Ver fichero

@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}

+ 11
- 0
network.tf Ver fichero

@@ -0,0 +1,11 @@
#resource "hcloud_network" "internal_network" {
# name = "internal_network"
# ip_range = "10.0.0.0/8"
#}

#resource "hcloud_network_subnet" "internal_network" {
# network_id = hcloud_network.internal_network.id
# type = "server"
# network_zone = "eu-central"
# ip_range = "10.9.0.0/16"
#}

+ 15
- 0
outputs.tf Ver fichero

@@ -0,0 +1,15 @@
output "hostvars" {
value = {
for hostname, host in var.vms: hostname =>
{
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
})
}
}
}
}

+ 3
- 0
provider.tf Ver fichero

@@ -0,0 +1,3 @@
provider "hcloud" {
token = var.token
}

+ 5
- 0
ssh.tf Ver fichero

@@ -0,0 +1,5 @@
resource "hcloud_ssh_key" "ssh_keys" {
name = each.value
public_key = file(each.value)
for_each = var.ssh_keys_filenames
}

+ 38
- 0
variables.tf Ver fichero

@@ -0,0 +1,38 @@
variable "vms" {
default = {
#"hostname.example.com" = {
# provider = "hetzner"
# groups = [ "testgroup" ]
# volumes = {
# data = { size = 32 }
# }
#}
}
}

variable "token" {
type = string
description = "Hetzner Cloud API-Token"
}

variable "default_location" {
type = string
default = "nbg1"
}

variable "server_image" {
type = string
default = "debian-10"
}

variable "default_server_type" {
type = string
default = "cx11"
}

variable "ssh_keys_filenames" {
type = set(string)
default = [
"~/.ssh/id_ed25519.pub",
]
}

+ 15
- 0
vms.tf Ver fichero

@@ -0,0 +1,15 @@
resource "hcloud_server" "vms" {
name = each.key
image = var.server_image
server_type = var.default_server_type
location = var.default_location
ssh_keys = [for value in hcloud_ssh_key.ssh_keys: value.id]

lifecycle {
ignore_changes = [
ssh_keys,
]
}

for_each = var.vms
}

+ 28
- 0
volumes.tf Ver fichero

@@ -0,0 +1,28 @@
locals {
volumes = { for item in flatten([
for hostname, host in var.vms: [
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.default_location
size = each.value.size

for_each = local.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.volumes
}

Cargando…
Cancelar
Guardar