Score:0

Auto create Ansible inventories when creating instances from Ansible playbook with Terraform on OVHCloud using OpenStack

gb flag

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   = openstack.ovh
  name       = "test_keypair2"
  public_key = file("~/.ssh/id_rsa.pub")
}

resource "openstack_compute_instance_v2" "test_terraform_instance" {
  name        = "tmpnode${count.index}"
  count       = var.node_count
  provider    = openstack.ovh
  image_name  = "Debian 10"
  flavor_name = var.flavor_name
  key_pair    = openstack_compute_keypair_v2.test_keypair.name
  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:

[tmpnodes]
%{ for ip in tmpnodes ~}
${ip}
%{ 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).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.