Browse Source

external terraform modules

master
parent
commit
af395ce021
11 changed files with 62 additions and 228 deletions
  1. +1
    -1
      .gitignore
  2. +8
    -0
      .gitmodules
  3. +40
    -0
      deploy.tf
  4. +9
    -9
      deploy.yaml
  5. +2
    -0
      group_vars/vms/vm.yaml
  6. +0
    -71
      inventory.tf
  7. +1
    -0
      modules/vms
  8. +1
    -0
      modules/vms-hetzner
  9. +0
    -138
      provider_hetzner.tf
  10. +0
    -5
      providers.tf
  11. +0
    -4
      versions.tf

+ 1
- 1
.gitignore View File

@@ -1,7 +1,7 @@
.terraform .terraform
terraform-info.json terraform-info.json
provider_hetzner.auto.tfvars provider_hetzner.auto.tfvars
inventory.json
inventory.vms.json
terraform.tfstate terraform.tfstate
terraform.tfstate.backup terraform.tfstate.backup
inventory.auto.tfvars.json inventory.auto.tfvars.json

+ 8
- 0
.gitmodules View File

@@ -2,3 +2,11 @@
path = roles/lxd path = roles/lxd
url = ../ansible-role-lxd.git url = ../ansible-role-lxd.git
branch = master branch = master
[submodule "modules/vms"]
path = modules/vms
url = ../terraform-module-vms.git
branch = master
[submodule "modules/vms-hetzner"]
path = modules/vms-hetzner
url = ../terraform-module-vms-hetzner.git
branch = master

+ 40
- 0
deploy.tf View File

@@ -0,0 +1,40 @@
variable "vms" {
default = {}
}

variable "groups" {
default = {}
}

module "vms" {
source = "./modules/vms"
vms = var.vms
groups = var.groups
providers_hostvars = {
hetzner = module.vms_hetzner.hostvars
}
}

output "vms" {
value = {
vms_by_provider = module.vms.vms_by_provider
ansible_inventory = module.vms.ansible_inventory
}
}

variable "hetzner_token" {
default = {}
}

module "vms_hetzner" {
source = "./modules/vms-hetzner"
vms = module.vms.vms_by_provider.hetzner
token = var.hetzner_token
}

resource "local_file" "ansible_inventory" {
content = jsonencode(module.vms.ansible_inventory)
filename = "inventory.vms.json"
file_permission = "0644"
directory_permission = "0755"
}

+ 9
- 9
deploy.yaml View File

@@ -2,18 +2,18 @@


- hosts: localhost - hosts: localhost
vars: vars:
vm_hosts: "{
{% for hostname in groups.vms %}
{{hostname|to_json}}: {{
{
'provider': hostvars[hostname].provider,
'server_type': hostvars[hostname].server_type,
} | to_json }},
{% endfor %} }"
vms: "{
{% for hostname in groups.vms %}
{{hostname|to_json}}: {{
{
'provider': hostvars[hostname].provider,
'server_type': hostvars[hostname].server_type,
} | to_json }},
{% endfor %} }"
tasks: tasks:
- name: terraform inventory vars - name: terraform inventory vars
copy: copy:
content: content:
vm_hosts: "{{ vm_hosts }}"
vms: "{{vms}}"
groups: {} groups: {}
dest: "{{playbook_dir}}/inventory.auto.tfvars.json" dest: "{{playbook_dir}}/inventory.auto.tfvars.json"

+ 2
- 0
group_vars/vms/vm.yaml View File

@@ -0,0 +1,2 @@
provider: hetzner
server_type: cx11

+ 0
- 71
inventory.tf View File

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

+ 1
- 0
modules/vms

@@ -0,0 +1 @@
Subproject commit 9cc65061a59c3afe0bae8a0140f9b51586200614

+ 1
- 0
modules/vms-hetzner

@@ -0,0 +1 @@
Subproject commit 8db63cc6b26733a46539d84712ba1e414b2b0606

+ 0
- 138
provider_hetzner.tf View File

@@ -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 View File

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

+ 0
- 4
versions.tf View File

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

terraform {
required_version = ">= 0.12"
}

Loading…
Cancel
Save