Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

provider_hetzner.tf 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Provider Setup
  2. variable "hetzner_token" {
  3. type = string
  4. description = "Hetzner Cloud API-Token"
  5. }
  6. variable "hetzner_default_location" {
  7. type = string
  8. default = "nbg1"
  9. }
  10. variable "hetzner_server_image" {
  11. type = string
  12. default = "debian-10"
  13. }
  14. variable "hetzner_default_server_type" {
  15. type = string
  16. default = "cx11"
  17. }
  18. variable "hetzner_ssh_keys_filenames" {
  19. type = set(string)
  20. default = [
  21. "~/.ssh/id_ed25519.pub",
  22. ]
  23. }
  24. provider "hcloud" {
  25. token = var.hetzner_token
  26. }
  27. # Network
  28. #resource "hcloud_network" "internal_network" {
  29. # name = "internal_network"
  30. # ip_range = "10.0.0.0/8"
  31. #}
  32. #resource "hcloud_network_subnet" "internal_network" {
  33. # network_id = hcloud_network.internal_network.id
  34. # type = "server"
  35. # network_zone = "eu-central"
  36. # ip_range = "10.9.0.0/16"
  37. #}
  38. # VMs
  39. resource "hcloud_ssh_key" "ssh_keys" {
  40. name = each.value
  41. public_key = file(each.value)
  42. for_each = var.hetzner_ssh_keys_filenames
  43. }
  44. locals {
  45. hetzner_vm_hosts = {
  46. for hostname, host in var.vm_hosts: hostname => host
  47. if host.provider == "hetzner"
  48. }
  49. hetzner_vm_hosts_with_groups = {
  50. for hostname, host in local.hetzner_vm_hosts: hostname => host if contains(keys(host),"groups")
  51. }
  52. hetzner_vm_hosts_without_groups = {
  53. for hostname, host in local.hetzner_vm_hosts: hostname => host if !contains(keys(host),"groups")
  54. }
  55. }
  56. resource "hcloud_server" "vms" {
  57. name = each.key
  58. image = var.hetzner_server_image
  59. server_type = var.hetzner_default_server_type
  60. location = var.hetzner_default_location
  61. ssh_keys = [for value in hcloud_ssh_key.ssh_keys: value.id]
  62. lifecycle {
  63. ignore_changes = [
  64. ssh_keys,
  65. ]
  66. }
  67. for_each = local.hetzner_vm_hosts
  68. }
  69. # Volumes
  70. locals {
  71. hetzner_volumes = { for item in flatten([
  72. for hostname, host in local.hetzner_vm_hosts: [
  73. for volumename, volume in lookup(host, "volumes", {}): {
  74. hostname = hostname
  75. volumename = volumename
  76. volume = volume
  77. host = host
  78. }
  79. ]
  80. ]): "${item.hostname}--${item.volumename}" => merge(item.volume,{hostname=item.hostname}) }
  81. }
  82. resource "hcloud_volume" "volumes" {
  83. name = each.key
  84. location = var.hetzner_default_location
  85. size = each.value.size
  86. for_each = local.hetzner_volumes
  87. }
  88. resource "hcloud_volume_attachment" "volume_attachments" {
  89. volume_id = hcloud_volume.volumes[each.key].id
  90. server_id = hcloud_server.vms[each.value.hostname].id
  91. #automount = true
  92. for_each = local.hetzner_volumes
  93. }
  94. # Provider Variable
  95. locals {
  96. provider_hetzner = {
  97. hostvars = {
  98. for hostname, host in var.vm_hosts: hostname =>
  99. {
  100. external_ip = hcloud_server.vms[hostname].ipv4_address
  101. ansible_host = hcloud_server.vms[hostname].ipv4_address
  102. #internal_ip = hcloud_server_network.vms[hostname].ip
  103. volumes = {
  104. for volumename, volume in lookup(host, "volumes", {}): volumename => merge(volume,{
  105. device = hcloud_volume.volumes["${hostname}--${volumename}"].linux_device
  106. })
  107. }
  108. }
  109. if host.provider == "hetzner"
  110. }
  111. }
  112. }