I'm starting to use Terraform (0.12) with different types of servers, each associated to a different image. I'm trying to simply output all private IP (I don't know the number before runtime) but can't find a simple way to do it (I found a more complicated one).
I'm trying TF on scaleway provider and I have some servers declaration like this (simplified)
# INPUT
variable "srv1" {
type = number
default = 0
}
variable "srv2" {
type = number
default = 0
}
# IMAGES
data "scaleway_instance_image" "srv1" {
name = "srv1"
}
data "scaleway_instance_image" "srv2" {
name = "srv2"
}
# SERVERS
resource "scaleway_instance_server" "srv1" {
name = "srv1_${count.index}"
image = data.scaleway_instance_image.srv1.id
count = var.srv1
}
resource "scaleway_instance_server" "srv2" {
name = "srv2_${count.index}"
image = data.scaleway_instance_image.srv2.id
count = var.srv2
}
To output private IP from all servers, this is what I do
# OUTPUT
output "srv1_private_ips" {
value = ["${scaleway_instance_server.srv1.*.private_ip}"]
}
output "srv2_private_ips" {
value = ["${scaleway_instance_server.srv2.*.private_ip}"]
}
It works but I need to create an output
section for each server type. I would like to have the option
- to loop over a list of server types and output all IPs in one output. Didn't find how
- to just output all
scaleway_instance_server.*.*.private_ip
but the double wildcard seems not possible
Any help appreciated.
You have a bit of a misunderstanding of how "count" and the splat ("*") syntax work. Using count, you will create a single resource block for your servers, using count to define how many times that instance will be duplicated. Hence:
Will create two resources:
scaleway_instance_server.srv[0]
andscaleway_instance_server.srv[1]
. (Yes, they'll have the sameimage
- that's a hint that this probably isn't the best way to manage this)Outputting a list of the
private_ip
attribute from all of the servers becomes trivial:NOTE: I've never used the scaleway provider in the example you gave, so I'm just guessing that attribute exists