Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Fetch the machines IPs or hostnames with terraform show #17

Open
benfab opened this issue Nov 29, 2017 · 14 comments
Open

Fetch the machines IPs or hostnames with terraform show #17

benfab opened this issue Nov 29, 2017 · 14 comments

Comments

@benfab
Copy link

benfab commented Nov 29, 2017

Hello,

I am currently using terraform to provision my infrastructure and Ansible to configure the software pieces.

So, once terraform apply executed, I would like terraform to fetch the IPs or hostnames of the machines in order to populate my Ansible host file.

I am currently running this workflow with the vSphere and AWS terraform provider and would like to do the same with the vRA 7 terraform provider.

Thanks.

@andreaspeters
Copy link

Cool idea! I crated a pull request for that. :-)

@nferro
Copy link
Contributor

nferro commented Jan 5, 2018

Actually that pull request doesn't fetch the IP address, just the hostname. I've done a pull pull request to @andreaspeters branch adding the ip address logic.

@andreaspeters
Copy link

@nferro thats great. thanks for that. :-)

@Boran
Copy link

Boran commented Feb 2, 2018

Any chance of that PR being merged into the vmware master branch, having the IP would be essential for me too.

@knlambert
Copy link

+1

@g1ps
Copy link

g1ps commented May 17, 2018

Likewise. The VM is pretty useless if we can't automate it after creation and for that we need the name and/or the IP. This is bog standard stuff. Can we get this change merged, please?

Thanks.

@nferro
Copy link
Contributor

nferro commented Jul 5, 2018

I've created PR #66 to grab the IP address.

@g1ps
Copy link

g1ps commented Jul 5, 2018

Can somebody please explain how this is supposed to work? I can't find any way to retrieve the machine name, despite the discussion about it. I try this:

output "ip" {
  value = "${vra7_resource.ansible.resource_configuration.RHEL7.4.ip_address}"
}

...

resource "vra7_resource" "ansible" {
  catalog_name = "Red Hat Enterprise Linux 7.4"
  resource_configuration = {
    RHEL7.4.cpu         = "2"
    RHEL7.4.memory      = "4096"
    RHEL7.4.description = "Ansible master"
    RHEL7.4.ip_address  = ""
  }
...
  provisioner "remote-exec" {
    inline = [
      "yum install ansible",
    ]

    connection {
      type     = "ssh"
      host     = "${vra7_resource.ansible.resource_configuration.RHEL7.4.ip_address}"
      user     = "<user>"
      password = "<pw>"
      timeout  = "30s"
    }
  }
}

It usually doesn't work. I have noticed that, for some reason, Private Cloud 2 doesn't show an IP address for a machine that has just been created and when that's the case, Terraform doesn't return it through this component. Once the IP appears, refresh will return it. Any light you can shed on why vRA would do that and cripple this would be brilliant.

I did manage to return an IP on one occassion but, even then, it won't connect, presumably due to the SSH prompt to approve the key. How do I deal with that when I DO get an address?

Finally, I followed through this discussion and tried retrieving the name but can't make that work, either. I tried creating a field in resource_configuration and I tried putting name in the root of resource as shown in configuration here but neither works. Terraform rejects that last one as not exisiting and refuses to run.

I'd appreciate an explanation of how this works and what use it is if we can't access either the name or the IP immediately after creation, because I'm beaten again.

Thanks.

@raghavav
Copy link

raghavav commented Jul 6, 2018

I think it depends on how ip addresses are assigned. DHCP addresses cannot be predictably assigned in a time window. The API returns the ip address only after the address is assigned and data-collected. On the other hand, if the ip address comes from an ip pool, vRA controls the address and is able to provide the right value. Can you please try this with ip-pool-based address assignment.

@g1ps
Copy link

g1ps commented Jul 10, 2018

Hi,

Thanks, @raghavav . No, I don't control the system and it wouldn't be worth my time trying to make it happen. That isn't very useful but if I can get the name I might stil be OK. It looks lke vRA has fallen well short of expectations and that we're moving to AWS but, in the meantime, how do I get the name?

Many thanks.

@nferro
Copy link
Contributor

nferro commented Jul 10, 2018

Now that #66 was merged, the same way you get the IP also allows you to get the machine name.
See the example.

Instead of Machine.ip_address I get the name with Machine.name

@g1ps
Copy link

g1ps commented Jul 10, 2018

OK, thanks. I'll try again.

@g1ps
Copy link

g1ps commented Jul 16, 2018

Provisioning is working for me using the machine name, now, after updating and fiddling with the timeout and some other parameters. Thanks again.

For anyone following who's looking for a working solution, the remote-exec provisoner works using the machine name thus:

// Use vars and prompt for user and pw.
resource "vra7_resource" "ansible_master" {
  catalog_name = "Red Hat Enterprise Linux 7.4"

  resource_configuration = {
    RHEL7.4.description = "Ansible master"
    RHEL7.4.ip_address  = ""
    RHEL7.4.name  = ""
  }

...

  provisioner "remote-exec" {
    inline = [
      // sudo prompt work-around.
      "echo '<password>' | sudo -S wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm",
      "sudo rpm -ivh epel-release-latest-7.noarch.rpm",
      "sudo yum -y install ansible"
    ]

    connection {
      type     = "ssh"
      host     = "${vra7_resource.ansible_master.resource_configuration.RHEL7.4.name}"
      user     = "<user>"
      password = "<password>"
    }
  }

The IP address is retrieved if vRA has populated the field but it usually doesn't do that fast enough. I use outputs to display the name and IP on completion. If you do that you can retrieve both at any time with terraform refresh.

Cheers.

@mcascone
Copy link

None of this is working for me.

> terraform -v
Terraform v0.11.14
+ provider.vra7 v0.2.0
provider "vra7" {
  username = "${var.VRA_USER}"
  password = "${var.VRA_PASS}"
  tenant = "${var.vra_tenant}"
  host = "${var.vra_url}"
  # insecure = true
}

resource "vra7_deployment" "terraform-1" {
  count             = "${var.server_count}"
  description       = "deployment via terraform"
  reasons           = "because it's awesome"
  catalog_item_name = "${var.vra_cat_item_name}"
  wait_timeout      = "1800"
  lifecycle {
    create_before_destroy = true
  }

  resource_configuration {
    Machine.name = ""
  }

gives error:

vra7_deployment.terraform-1: Refreshing state... (ID: 53a10f47-7766-4dc1-8725-bf9aca07ade2)
vra7_deployment.terraform-1: Creating...
  businessgroup_id:                    "" => "<computed>"
  businessgroup_name:                  "" => "<computed>"
  catalog_item_id:                     "" => "<computed>"
  catalog_item_name:                   "" => "MTP_Windows_Server_2012_R2_v01"
  description:                         "" => "deployment via terraform"
  failed_message:                      "" => "<computed>"
  reasons:                             "" => "because it's awesome"
  request_status:                      "" => "<computed>"
  resource_configuration.%:            "" => "1"
  resource_configuration.Machine.name: "" => "<computed>"
  wait_timeout:                        "" => "1800"

Error: Error applying plan:

1 error occurred:
        * vra7_deployment.terraform-1: 1 error occurred:
        * vra7_deployment.terraform-1: The resource_configuration in the config file has invalid component name(s): Machine.name

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants