1

Im setting up and openstack instance using terraform. Im writing to a file the ip returned but for some reason its alwayse empty (i have looked at the instance in openstack consol and everythign is correct with ip, securitygroups etc etc)

resource "openstack_compute_instance_v2" "my-deployment-web" {
  count = "1"
  name = "my-name-WEB"
  flavor_name = "m1.medium"
  image_name = "RHEL7Secretname"
  security_groups = [
    "our_security_group"]
  key_pair = "our-keypair"   


  network {
    name = "public"
  }

  metadata {
    expire = "2",
    owner = ""
  }

  connection {
    type = "ssh"
    user = "vagrant"
    private_key = "config/vagrant_private.key"
    agent = "false"
    timeout = "15m"
  }

  ##Create Ansible host in staging inventory
  provisioner "local-exec" {
    command =  "echo -e '\n[web]\n${openstack_compute_instance_v2.my-deployment-web.network.0.floating_ip}' > ../ansible/inventories/staging/hosts"
    interpreter = ["sh", "-c"]
  }    
}

The host file generated only gets [web] but no ip. Anyone know why?

[web]
Marthin
  • 6,413
  • 15
  • 58
  • 95
  • 1
    Take a look at using a template instead of raw shell comands in a `local-exec`. That would be much easier for you: https://www.terraform.io/docs/providers/template/index.html – Matthew Schuchard Apr 03 '19 at 13:10
  • I have that as well but the same problem that the variable doesn't resolve to the ip – Marthin Apr 03 '19 at 13:11
  • 1
    Oh well then i guess that simplifies things. If I look at the documentation https://www.terraform.io/docs/providers/openstack/r/compute_instance_v2.html#attributes-referencears the floating ip is not an attribute, which would explain it being empty. There are fixed ip attributes though. – Matthew Schuchard Apr 03 '19 at 13:14
  • you are correct. I picked it up from an example but its not valid as you said. For me it worked with '.access_ip_v4' instead. – Marthin Apr 03 '19 at 13:40

1 Answers1

0

Modifying the variable from

${openstack_compute_instance_v2.my-deployment-web.network.0.floating_ip}

to

${openstack_compute_instance_v2.my-deployment-web.network.0.access_ip_v4}

solved the problem. Thank you @Matt Schuchard

Marthin
  • 6,413
  • 15
  • 58
  • 95