Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

52 rindas
1.4KB

  1. variable "ansible_inventory_filename" {
  2. type = string
  3. default = "inventory.json"
  4. }
  5. locals {
  6. vm_hosts_with_groups = {
  7. for hostname, host in var.vm_hosts: hostname => host if contains(keys(host),"groups")
  8. }
  9. vm_hosts_without_groups = {
  10. for hostname, host in var.vm_hosts: hostname => host if !contains(keys(host),"groups")
  11. }
  12. vm_groups = distinct(flatten(values(local.hetzner_vm_hosts_with_groups)[*].groups))
  13. ansible_inventory = {
  14. all = {
  15. hosts = {
  16. for hostname,host in var.vm_hosts: hostname => {}
  17. }
  18. children = merge(
  19. {
  20. for group in local.vm_groups: group => {
  21. hosts = {
  22. for hostname, host in local.vm_hosts_with_groups:
  23. hostname => {}
  24. if contains(host.groups,group)
  25. }
  26. }
  27. },{
  28. for providername, provider in local.providers: "provider_${providername}" => {
  29. hosts = {
  30. for hostname, host in local.vm_hosts_with_groups:
  31. hostname => local.providers[host.provider].hostvars[hostname]
  32. if host.provider == providername
  33. }
  34. }
  35. }
  36. )
  37. }
  38. }
  39. }
  40. output "ansible_inventory" {
  41. value = local.ansible_inventory
  42. }
  43. resource "local_file" "foo" {
  44. content = jsonencode(local.ansible_inventory)
  45. filename = var.ansible_inventory_filename
  46. file_permission = "0644"
  47. directory_permission = "0755"
  48. }