Markus Katharina Brechtel 4 роки тому
джерело
коміт
266ae6ae3c
4 змінених файлів з 218 додано та 0 видалено
  1. +71
    -0
      inventory.tf
  2. +138
    -0
      provider_hetzner.tf
  3. +5
    -0
      providers.tf
  4. +4
    -0
      versions.tf

+ 71
- 0
inventory.tf Переглянути файл

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

variable "groups" {
default = {
#testgroup = {
# testvar1 = "bla"
#}
}
}

variable "ansible_inventory_filename" {
type = string
default = "inventory.json"
}

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))
ansible_inventory = {
all = {
hosts = {
for hostname,host in var.vm_hosts: hostname => {}
}
children = merge(
{
for group in local.vm_groups: group => {
hosts = {
for hostname, host in local.vm_hosts_with_groups:
hostname => {}
if contains(host.groups,group)
}
}
},{
for providername, provider in local.providers: "provider_${providername}" => {
hosts = {
for hostname, host in var.vm_hosts:
hostname => local.providers[host.provider].hostvars[hostname]
if host.provider == providername
}
}
}
)
}
}
}

output "ansible_inventory" {
value = local.ansible_inventory
}

resource "local_file" "ansible_inventory" {
content = jsonencode(local.ansible_inventory)
filename = var.ansible_inventory_filename
file_permission = "0644"
directory_permission = "0755"
}

+ 138
- 0
provider_hetzner.tf Переглянути файл

@@ -0,0 +1,138 @@
# 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"
}
}
}

+ 5
- 0
providers.tf Переглянути файл

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

+ 4
- 0
versions.tf Переглянути файл

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

terraform {
required_version = ">= 0.12"
}

Завантаження…
Відмінити
Зберегти