Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Can't access vars in resource_configuration #104

Open
g1ps opened this issue Jan 20, 2021 · 3 comments
Open

Can't access vars in resource_configuration #104

g1ps opened this issue Jan 20, 2021 · 3 comments

Comments

@g1ps
Copy link

g1ps commented Jan 20, 2021

vRA 7.x version
Version: 7.5.0 (Build: 10053500)

Terraform version
Terraform v0.14.4

terraform-provider-vra7 plugin version

  • provider registry.terraform.io/vmware/vra7 v3.0.0

Describe the bug

Variables inside resource_configuration blocks are inaccessible.

Hello,

After huge gobs of pain I had deployment working under Terraform 11. I have just been through the various upgrade steps to move to V14 but I am now unable to access vars in resource_configuration. I've spent a day and a half searching and trying things I've found here and elsewhere without any real progress. Terraform complains Error: Invalid template interpolation value: Cannot include the given value in a string template: string required. after the VM is deployed successfully and stops.

I've seen that this problem has been experienced by others. I've tried things with and without count but I really want it to remain to permit mutilple deployments. My script looks in part like this:

...
output "ansible-master-IP-address" {
  value = vra7_deployment.ansible_master[*].resource_configuration[*].instances[*].properties.ip_address
}

output "ansible-master-name" {
  value = vra7_deployment.ansible_master[*].resource_configuration[*].instances[*].properties.name
}

provider "vra7" {
  username = var.vra_user
  password = var.vra_password
  tenant   = "XXXXXXX"
  host     = "https://secret.com/"
  insecure = false
}

resource "vra7_deployment" "ansible_master" {
  businessgroup_name = "dev"
  catalog_item_name  = "Red Hat Enterprise Linux 8"
  count              = 1
  description        = "Ansible master."
  reasons            = "Automation"
  wait_timeout       = 30
  lease_days         = 390

  resource_configuration {
    component_name = "RHEL8"
    configuration = {
      cpu         = "1"
      memory      = "4096"
      description = "Ansible master"
      ip_address  = ""
      name        = ""
    }
  }

  deployment_configuration = {
    _number_of_instances = 1
  }

  # Copy the files.
  provisioner "file" {
    source      = "ansible/"
    destination = "./"

    connection {
      type     = "ssh"
      host     = "${self.resource_configuration[*].instances[*].properties.name}.domain.com"
      user     = var.vra_user
      password = var.vra_password
    }
  }

...

I've tried splats, as seen in the output vars. I've tried self as seen in the file provisioner connection block. I've tried with and without properties and instances and many other things and I still can't get this to run. vra7_deployment.ansible_master[*] in connection also fails. Can someone help, please, as this really does seem like a bug rather than a 'feature' and no amount of trying will make it work here? It shouldn't be this hard especially given that it worked previously.

Thanks.

To Reproduce
As described above. Even paths which work from terraform console do not work in production.

Expected behavior
Variables inside resource_configuration should be accessible.

@g1ps
Copy link
Author

g1ps commented May 25, 2021

I'm trying again with terraform 15. I can get outputs to work with

output "server-name" {
   value = vra7_deployment.my_server[*].resource_configuration[*].instances[*].properties.name
 }

although it seems to alternate between returning the VM's actual name and the name field which is visible in Properties/name in the machine's properties tab, which contains some other text.

I still can't get self to work on providers. I've tried dozens of variations on resource_configuration[*], instances[0] or [*], etc, configuration, the vm name, array syntax, element, etc. Everything I can find online, including here. Nothing works. How do I use self, or anything else, in a provider to retrieve name and ip address? I have looked at the structure in the state file, the vars in the UI and anything else I can find. Nothing makes this work.

@mh013370
Copy link

I spent quite a bit of time on this same problem and eventually came to a solution. I'm using provider version 3.0.2, terraform 1.0.3, and vra7 v7.5+.

I have a resource which provisions several VMs. In my case I'm only configuring a single resource_configuration, so i assume that in the below. I have a null_resource alongside the vra7_deployment with a remote-exec and the following connection configuration:

resource "null_resource" "remote-exec-example" {
   count = var.instance_count # this is the number of VMs i provision

   connection {
      type = "ssh"
      user = var.user
      password = var.password
      host = one(vra7_deployment.my-deployment.resource_configuration)["instances"][count.index]["name"]
   }
}

Both resource_configuration and resource_configuration.instances are lists of objects, so they need to be accessed accordingly.

This works as expected, so this is how i've successfully accessed instance information. I couldn't get the examples provided in this repo to work, but perhaps that's user error. /shrug

@jonhowe
Copy link

jonhowe commented Sep 10, 2021

@michael81877 THANK YOU for sharing this. I was banging my head against a wall for a long time trying to get this working and this got me on the right track. I'm using provider version 3.0.2, Terraform 1.0.4 and vRA 7.6.

I needed to make one change though since I'm potentially deploying >1 deployment. Both of the resources are below.

resource "vra7_deployment" "ubuntu" {
  count              = var.instances
  catalog_item_name  = var.catalog_item_name
  businessgroup_name = var.businessgroup_name
  wait_timeout       = 20
  lease_days         = var.lease_days

  deployment_configuration = {
    "customprop.prop1" = var.customprop1
    "customprop.prop2" = var.customprop2
  }

  resource_configuration {
    component_name = var.component_name
    configuration = {
      cpu                                          = 2
      memory                                       = 2048
      "VirtualMachine.Admin.CustomizeGuestOSDelay" = "00:01:00"
      "VirtualMachine.Customize.WaitComplete"      = "false"
      "VirtualMachine.Admin.UseGuestAgent"         = "false"
      "Vrm.DataCenter.Location"                    = var.datacenter_location
      "VirtualMachine.Network0.Name"               = var.portgroup_name
    }
  }
}

resource "null_resource" "remote-exec" {
  count = var.instances

  connection {
      type = "ssh"
      user     = var.ssh_user
      password = var.ssh_password
      host = one(vra7_deployment.ubuntu[count.index].resource_configuration)["instances"][count.index]["ip_address"]
  }
  provisioner "remote-exec" {
    inline = [
      "apt-get update && apt-get upgrade -y",
      "apt-get install ansible -y"
    ]
  }
}

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

3 participants