How to write the name and ip of the virtual machine in inventory.ini.tmpl using terraform, if it is created?

The virtual machine is created using terraform version 13.5.

The virtual machine have variable name_b (which are interesting to me)

Main code for create virtual machime

resource "vcd_vapp_vm" "b" {
  count         = var.create_b == "true" ? 1 : 0
  name          = "b"
}

If I do not want to create a virtual machine:

create_b = "false"

it is necessary that the name of the virtual machine are recorded in inventory.ini, if it is created.

resource "local_file" "AnsibleInventory" {
 content = templatefile("inventory.ini.tmpl",
 {
  name_b                = vcd_vapp_vm.b.name,
 }
 )
 filename = "inventory.ini"
}

How to avoid the mistake?

Error: Missing resource instance key

  on outputs.tf line 12, in resource "local_file" "AnsibleInventory":
  13:   name_b      = vcd_vapp_vm.b.name,

Because vcd_vapp_vm.b has "count" set, its attributes must be accessed
on specific instances.

For example, to correlate with indices of a referring resource, use:
    vcd_vapp_vm.b[count.index]

I try add [count.index] to resource

resource "local_file" "AnsibleInventory" {
 content = templatefile("inventory.ini.tmpl",
 {
  name_b                = vcd_vapp_vm.b[count.index].name,
  
 }
 )
 filename = "inventory.ini"
}

Error:

Error: Reference to "count" in non-counted context

  on outputs.tf line 12, in resource "local_file" "AnsibleInventory":
  13:   name_b                = vcd_vapp_vm.b[count.index].name,

The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.

I try add check special variable

resource "local_file" "AnsibleInventory" {
 content = templatefile("inventory.ini.tmpl",
 {
  name_b                = var.create_b ? vcd_vapp_vm.b.name : null,
  
 }
 )
 filename = "inventory.ini"
}

Error

  on outputs.tf line 12, in resource "local_file" "AnsibleInventory":

  13:   name_b                = var.create_b ? vcd_vapp_vm.b.name : null,

Because vcd_vapp_vm.b has "count" set, its attributes must be accessed
on specific instances.

For example, to correlate with indices of a referring resource, use:
    vcd_vapp_vm.b[count.index]

Try

resource "local_file" "AnsibleInventory" {
 content = templatefile("inventory.ini.tmpl",
 {
  name_b = try(vcd_vapp_vm.b.name, null)
 }
 )
 filename = "inventory.ini"
}

Error

Error: Missing resource instance key

  on outputs.tf line 12, in resource "local_file" "AnsibleInventory":
  13:   name_b = try(vcd_vapp_vm.b.name, null)

Because vcd_vapp_vm.b has "count" set, its attributes must be accessed
on specific instances.

For example, to correlate with indices of a referring resource, use:
    vcd_vapp_vm.b[count.index]

For the local_file resource you also want a count entry, so that it doesn’t get created if you aren’t making a VM.

Additionally within that resource you need to adjust the reference to the vm resource to take the count into account - so vcd_vapp_vm.b.name would become vcd_vapp_vm.b[0].name