Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

72 строки
1.7KB

  1. variable "vm_hosts" {
  2. default = {
  3. #"hostname.example.com" = {
  4. # provider = "hetzner"
  5. # groups = [ "testgroup" ]
  6. # volumes = {
  7. # data = { size = 32 }
  8. # }
  9. #}
  10. }
  11. }
  12. variable "groups" {
  13. default = {
  14. #testgroup = {
  15. # testvar1 = "bla"
  16. #}
  17. }
  18. }
  19. variable "ansible_inventory_filename" {
  20. type = string
  21. default = "inventory.json"
  22. }
  23. locals {
  24. vm_hosts_with_groups = {
  25. for hostname, host in var.vm_hosts: hostname => host if contains(keys(host),"groups")
  26. }
  27. vm_hosts_without_groups = {
  28. for hostname, host in var.vm_hosts: hostname => host if !contains(keys(host),"groups")
  29. }
  30. vm_groups = distinct(flatten(values(local.hetzner_vm_hosts_with_groups)[*].groups))
  31. ansible_inventory = {
  32. all = {
  33. hosts = {
  34. for hostname,host in var.vm_hosts: hostname => {}
  35. }
  36. children = merge(
  37. {
  38. for group in local.vm_groups: group => {
  39. hosts = {
  40. for hostname, host in local.vm_hosts_with_groups:
  41. hostname => {}
  42. if contains(host.groups,group)
  43. }
  44. }
  45. },{
  46. for providername, provider in local.providers: "provider_${providername}" => {
  47. hosts = {
  48. for hostname, host in var.vm_hosts:
  49. hostname => local.providers[host.provider].hostvars[hostname]
  50. if host.provider == providername
  51. }
  52. }
  53. }
  54. )
  55. }
  56. }
  57. }
  58. output "ansible_inventory" {
  59. value = local.ansible_inventory
  60. }
  61. resource "local_file" "ansible_inventory" {
  62. content = jsonencode(local.ansible_inventory)
  63. filename = var.ansible_inventory_filename
  64. file_permission = "0644"
  65. directory_permission = "0755"
  66. }