我正在嘗試使用 Terraform 從 YAML 檔案中提取某些標簽,但我只是不知道如何。Yaml 檔案如下所示:
---
name: subscriptionName
emailContact: [email protected]
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 { }
}
uj5u.com熱心網友回復:
如果你真的想要兩個隨機標簽,你可以使用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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/478704.html
標籤:天蓝色 地形 yaml 天蓝色 terraform-provider-azure
