Auto create Ansible inventories with terraform on OVHPublicCloud when creating instances

I have configured Terraform Ansible role that is creating successfully instances on OVHPublicCloud, but I don’t know how to extract the public IP address from newly created instance to Ansible inventory hosts using Terraform.

I was trying with those answers, but I always have an error:

A managed resource “instance” “tmpnode” has not been declared in the root module.

Here’s my .tf file:

resource "openstack_compute_keypair_v2" "keypair" {
  provider   =
  name       = "test_keypair2"
  public_key = file("~/.ssh/")

resource "openstack_compute_instance_v2" "test_terraform_instance" {
  name        = "tmpnode${count.index}"
  count       = var.node_count
  provider    =
  image_name  = "Debian 10"
  flavor_name = var.flavor_name
  key_pair    =
  network {
    name      = "Ext-Net"

resource "local_file" "hosts_cfg" {
  content = templatefile("${path.module}/templates/hosts",
      tmpnodes = instance.tmpnode.*.public_ip
  filename = "../inventory/hosts"

And the ${path.module}/templates/hosts file:

%{ for ip in tmpnodes ~}
%{ endfor ~}

I was trying also with values from terraform.tfstate like instances.attributes.access_ip_v4 using openstack_compute_instance_v2 resource without any luck (same error).

Any suggestions?