This is probably really easy but I’m hitting a brick wall on how to do this, I know I’ll end up kicking myself if someone can offer a pointer.
As a little background, I’m working with Azure, and trying to get some resource attributes (specifically IP address & hostname) passed into a command for a VM extension resource (to add the IP/Name attributes from all VMs to the hosts file on each VM).
So, NIC resource outputs a ‘private_IP_Address’ and VM resource outputs a ‘name’ attribute. Easy way to get these for one resource is to create a local that references the attributes…
locals {
host = “{azurerm_network_interface.network_interface.private_ip_address} {azurerm_virtual_machine.virtual_machine.name}”
}
… and then add “${local.host}” to the “commandToExecute” in the ‘Settings Block’ of the VM Extension. This works and the IP address and name are added to the file.
However, the VM and NIC resources both have a Count value, so the deployment might require 3 VMs & NICs (or 10, or 2 etc). Clearly the example I gave above doesn’t scale with the number of VMs deployed.
I’ve tried various things with elements, count.index and ‘splats’ etc but they either just don’t seem to work (tuple value when string required) or I have to hard code the index value in the local being created (which again, doesn’t scale).
Any ideas greatly appreciated.
Thanks