我正在尝试使用 Terraform 从 YAML 文件中提取某些标签,但我只是不知道如何操作。 Yaml 文件如下所示:
---
name: subscriptionName
emailContact: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e0fcfef6f6fef2faffd3f7fcfef2fafdbdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a>
tags:
- key: "tagKey1"
value: "tagValue1"
- key: "tagKey2"
value: "tagValue2"
- key: "tagKey3"
value: "tagValue3"
- key: "tagKey4"
value: "tagValue4"
我感兴趣的是获取 2 个(假设 key1 和 key3)键值对作为标签和标签资源。我知道“本地人”在这里发挥着作用,但我对 terraform 有点陌生,无法标记任何资源。 资源是 Azure(如果重要的话)。
我尝试标记的资源是:
resource "azurerm_network_security_group" "nsg" {
name = "randomname"
location = "westeurope"
resource_group_name = "random_rg"
tags { }
}
最佳答案
如果您确实想要两个随机标签,您可以使用 random_shuffle :
locals {
loaded_yaml = yamldecode(file("./your_file.yaml"))
}
resource "random_shuffle" "indices" {
input = range(0, length(local.loaded_yaml.tags))
result_count = 2
seed = timestamp()
}
output "random_tags" {
value = [for idx in random_shuffle.indices.result:
local.loaded_yaml.tags[idx]]
}
更新 例如:
tags = {
(local.loaded_yaml.tags[0].key) = local.loaded_yaml.tags[0].value
(local.loaded_yaml.tags[3].key) = local.loaded_yaml.tags[3].value
}
关于azure - 如何使用 yaml 文件中的标签 - terraform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72287875/