Rendering file from list of maps variable with templatefile

I have tis list of maps variable:

variable "virtual_machines" {
  default = {
    "master1" = {
       name         = "z-ca-bdc-master1"
       worker_node  = false
       ipv4_address = "192.168.113.79"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },
    "master2" =  {
       name         = "z-ca-bdc-master2"
       worker_node  = false
       ipv4_address = "192.168.113.80"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },
    .
    .

This is my template:

[all]
${k8s_node_host}

[kube-master]
${k8s_master_host}

[etcd]

[kube-node]
${k8s_node_host}

[calico-rr]

[k8s-cluster:children]
kube-master
kube-node
calico-rr

This is my configuration code which I am aiming to create a local file resource from:

data "template_file" "k8s" {
  for_each = var.virtual_machines
  template = file("./templates/kubespray_inventory.tpl")

  vars = {
    k8s_master_host = each.value.worker_node ? join("\n", [each.value.name]) : ""
    k8s_node_host   = join("\n", [each.value.name])
  }
}

resource "local_file" "k8s_file" {
  for_each = var.virtual_machines
  content = data.template_file.k8s[each.key].rendered
  filename = "./k8s-host"
}

When I run terraform apply, the code attempts to create a local file resource for each map in my list, I just want the one, therefore for_each loops are not the way to go. To start off with the code in my data block is incorrect, I’ve spent trawling all the usual places for examples of where people have done this, stackoverflow etc without success, can someone please advise me as to ow to fix this, I would like a single file resource with the source data parsed from each map in my list.

Did you look into the templatefile() function?