据我所知,您可以使用Terraform 外部数据源执行脚本以获取容器是否存在的信息,然后根据状态创建容器或不创建容器。
以下是使用 Azure CLI 命令的 bash 脚本示例代码:
#!/bin/bash
eval "$(jq -r '@sh "export container_name=\(.container_name) account_name=\(.account_name)"')"
flag=$(az storage container exists --name $container_name --account-name $account_name --query exists)
if [ $flag ]
then
echo "{\"exists\":\"True\"}"
else
echo "{\"exists\":\"False\"}"
fi
地形:
variable "container_name" {}
data "azurerm_storage_account" "test" {
name = "charlescloudshell"
resource_group_name = "v-chaxu-ChinaCXPTeam"
}
data "external" "exists" {
program = ["/bin/bash", "./container.sh"]
query = {
container_name = "${var.container_name}"
account_name = "${data.azurerm_storage_account.test.name}"
}
}
resource "azurerm_storage_container" "test" {
count = "${data.external.exists.result["exists"] == "False" ? 1 : 0}"
name = "${var.container_name}"
storage_account_name = "${data.azurerm_storage_account.test.name}"
container_access_type = "private"
}