Selaa lähdekoodia

vms module

master
vanhempi
commit
00d88ef3bb
6 muutettua tiedostoa jossa 49 lisäystä ja 166 poistoa
  1. +0
    -10
      inventory.tf
  2. +19
    -0
      locals.tf
  3. +11
    -5
      outputs.tf
  4. +0
    -138
      provider_hetzner.tf
  5. +0
    -5
      providers.tf
  6. +19
    -8
      variables.tf

+ 0
- 10
inventory.tf Näytä tiedosto

@@ -1,10 +0,0 @@

locals {
vm_hosts_with_groups = {
for hostname, host in var.vm_hosts: hostname => host if contains(keys(host),"groups")
}
vm_hosts_without_groups = {
for hostname, host in var.vm_hosts: hostname => host if !contains(keys(host),"groups")
}
vm_groups = distinct(flatten(values(local.hetzner_vm_hosts_with_groups)[*].groups))
}

+ 19
- 0
locals.tf Näytä tiedosto

@@ -0,0 +1,19 @@
locals {

vms_with_groups = {
for hostname, host in var.vms: hostname => host if contains(keys(host),"groups")
}
vms_without_groups = {
for hostname, host in var.vms: hostname => host if !contains(keys(host),"groups")
}
vm_groups = distinct(flatten(values(local.vms_with_groups)[*].groups))

vms_with_provider = {
for hostname, host in var.vms: hostname => host if contains(keys(host),"provider")
}
vms_without_provider = {
for hostname, host in var.vms: hostname => host if !contains(keys(host),"provider")
}
vm_providers = distinct(values(local.vms_with_provider)[*].provider)

}

+ 11
- 5
outputs.tf Näytä tiedosto

@@ -1,23 +1,29 @@
output "vms_by_provider" {
value = {

}
}

output "ansible_inventory" {
value = {
all = {
hosts = {
for hostname,host in var.vm_hosts: hostname => {}
for hostname,host in var.vms: hostname => {}
}
children = merge(
{
for group in local.vm_groups: group => {
hosts = {
for hostname, host in local.vm_hosts_with_groups:
for hostname, host in local.vms_with_groups:
hostname => {}
if contains(host.groups,group)
}
}
},{
for providername, provider in local.providers: "provider_${providername}" => {
for providername in local.vm_providers: "provider_${providername}" => {
hosts = {
for hostname, host in var.vm_hosts:
hostname => local.providers[host.provider].hostvars[hostname]
for hostname, host in local.vms_with_provider:
hostname => var.providers_hostvars[host.provider].hostvars[hostname]
if host.provider == providername
}
}


+ 0
- 138
provider_hetzner.tf Näytä tiedosto

@@ -1,138 +0,0 @@
# Provider Setup

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

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

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

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

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

provider "hcloud" {
token = var.hetzner_token
}


# Network

#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"
#}


# VMs

resource "hcloud_ssh_key" "ssh_keys" {
name = each.value
public_key = file(each.value)
for_each = var.hetzner_ssh_keys_filenames
}

locals {
hetzner_vm_hosts = {
for hostname, host in var.vm_hosts: hostname => host
if host.provider == "hetzner"
}
hetzner_vm_hosts_with_groups = {
for hostname, host in local.hetzner_vm_hosts: hostname => host if contains(keys(host),"groups")
}
hetzner_vm_hosts_without_groups = {
for hostname, host in local.hetzner_vm_hosts: hostname => host if !contains(keys(host),"groups")
}
}

resource "hcloud_server" "vms" {
name = each.key
image = var.hetzner_server_image
server_type = var.hetzner_default_server_type
location = var.hetzner_default_location
ssh_keys = [for value in hcloud_ssh_key.ssh_keys: value.id]

lifecycle {
ignore_changes = [
ssh_keys,
]
}

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

locals {
provider_hetzner = {
hostvars = {
for hostname, host in var.vm_hosts: 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
})
}
}
if host.provider == "hetzner"
}
}
}

+ 0
- 5
providers.tf Näytä tiedosto

@@ -1,5 +0,0 @@
locals {
providers = {
hetzner = local.provider_hetzner
}
}

+ 19
- 8
variables.tf Näytä tiedosto

@@ -1,12 +1,19 @@
variable "vm_hosts" {
variable "vms" {
default = {
#"hostname.example.com" = {
# provider = "hetzner"
# groups = [ "testgroup" ]
# volumes = {
# data = { size = 32 }
# }
#}
"host1.example.com" = {
#provider = "hetzner"
groups = [ "testgroup" ]
volumes = {
data = { size = 32 }
}
}
"host2.example.com" = {
#provider = "hetzner"
groups = [ "testgroup" ]
volumes = {
data = { size = 32 }
}
}
}
}

@@ -17,3 +24,7 @@ variable "groups" {
#}
}
}

variable "providers_hostvars" {
default = {}
}

Loading…
Peruuta
Tallenna