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" }