[英]How can I retrieve all the VPC custom images that have a specific tag with terraform?
I have a list of custom images in my account.我的帐户中有一个自定义图像列表。
In my terraform template, I'd like to retrieve an image with a specific tag.在我的 terraform 模板中,我想检索带有特定标签的图像。 I don't want to hardcode or pass the image ID to the terraform template, I want to reference a tag like version:1.0
instead.我不想硬编码或将图像 ID 传递给 terraform 模板,我想引用像version:1.0
这样的标签。 I could not find how to do it with data ibm_is_image
.我找不到如何使用data ibm_is_image
来做到这一点。
Using a combination of ibm_is_images
to retrieve all images and ibm_resource_tag
to retrieve the tags associated to one image, you can filter to find the image you are looking for.使用ibm_is_images
的组合来检索所有图像,并ibm_resource_tag
来检索与一个图像关联的标签,您可以过滤以找到您正在寻找的图像。
Here is an example.这是一个例子。 It assumes that you have several images in your custom image library and that these images have been tagged version:1.0
and version:2.0
它假定您的自定义图像库中有多个图像,并且这些图像已被标记为version:1.0
和version:2.0
variable "ibmcloud_api_key" {}
variable "region" { default = "us-south" }
terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
}
}
required_version = ">= 1.1.8"
}
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}
data "ibm_is_images" "images" {
visibility = "private"
}
data "ibm_resource_tag" "image_tag" {
for_each = { for image in data.ibm_is_images.images.images : image.id => image.crn }
resource_id = each.value
}
locals {
// create an array with all images and their tags
all_images = [ for image in data.ibm_is_images.images.images : {
id: image.id,
name: image.name
crn: image.crn
tags: data.ibm_resource_tag.image_tag[image.id].tags
}
]
}
output "all_images" {
value = local.all_images
}
output "all_v1_images" {
value = [ for image in local.all_images: image if contains(image.tags, "version:1.0") ]
}
output "all_v2_images" {
value = [ for image in local.all_images: image if contains(image.tags, "version:2.0") ]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.