gpt4 book ai didi

地形图和对象变量作为输入

转载 作者:行者123 更新时间:2023-12-04 09:37:59 25 4
gpt4 key购买 nike

map /对象变量内的任何字符串属性的命令行输入都存在问题。
以下配置适用于以下命令 .但是当我在对象变量中使用任何字符串属性时。它失败

terraform plan -var='storageProfile2={"storage_mb":102400,"backup_retention_days":15,"geo_redundant_backup_enabled":false}'
//main.tf
resource "azurerm_postgresql_server" "dmcdevops_postgress" {
name = "pstgressdb101"
location = azurerm_resource_group.dmc_rg_creation.location
resource_group_name = azurerm_resource_group.dmc_rg_creation.name
sku_name = "GP_Gen5_4"
backup_retention_days = var.storageProfile2.backup_retention_days
storage_mb = var.storageProfile2.storage_mb
geo_redundant_backup_enabled = var.storageProfile2.geo_redundant_backup_enabled
administrator_login = "sdfgsgfsg"
administrator_login_password = "H@Sh1CoR3!"
version = "11"
ssl_enforcement_enabled = true

}
//variables.tf
variable "storageProfile2" {
default = {
storage_mb = 102400
backup_retention_days = 15
geo_redundant_backup_enabled = false
}

type = object(
{
storage_mb = number
backup_retention_days = number
geo_redundant_backup_enabled = bool
}
)
}
下面的配置不起作用 .我刚刚添加了 管理员登录作为对象变量的字符串属性。
terraform plan 和 apply 使用默认值。
terraform plan -var='storageProfile2={"storage_mb":102400,"backup_retention_days":15,"geo_redundant_backup_enabled":false,"administrator_login":"pgadmin1223"}'
//main.tf 
resource "azurerm_postgresql_server" "dmcdevops_postgress" {
name = "pstgressdb101"
location = azurerm_resource_group.dmc_rg_creation.location
resource_group_name = azurerm_resource_group.dmc_rg_creation.name
sku_name = "GP_Gen5_4"
backup_retention_days = var.storageProfile2.backup_retention_days
storage_mb = var.storageProfile2.storage_mb
geo_redundant_backup_enabled = var.storageProfile2.geo_redundant_backup_enabled
administrator_login = var.storageProfile2.administrator_login
administrator_login_password = "H@Sh1CoR3!"
version = "11"
ssl_enforcement_enabled = true

}

//varibale.tf
variable "storageProfile2" {
default = {
storage_mb = 102400
backup_retention_days = 15
geo_redundant_backup_enabled = false
administrator_login = "pgadmin"
}

type = object(
{
storage_mb = number
backup_retention_days = number
geo_redundant_backup_enabled = bool
administrator_login = string
}
)
}
错误信息
enter image description here

最佳答案

由于第二个配置使用变量的默认值,配置不是问题,terraform apply -var一定是问题。正确处理是一件非常棘手的事情,并且与 shell 解析规则之间存在许多有问题的交互,这些交互可能会让您陷入困境。
我发现使用 .tfvars files更可靠,我不再尝试让 -var 为我的 Terraform 工作工作。
terraform.tfvars:

storageProfile2 = {
storage_mb = 102400
backup_retention_days = 15
geo_redundant_backup_enabled = false
administrator_login = "pgadmin1223"
}
在同一个目录中创建上面的 terraform.tfvars 然后运行 ​​ terraform planterraform apply没有 -var。它应该可以解决您的问题。
原答案
azurerm 提供程序中有几项重大更改旨在向后兼容,但很可能会导致此处出现问题。
geo_redundant_backup 是自 v2.7.0 或 v2.10.0 起已弃用的属性,具体取决于您使用的数据库资源。您应该使用 geo_redundant_backup_enabled 而是将其指定为 bool 值( bool 类型)。我怀疑向后兼容性并不完全可靠。
storage_profile 块也被弃用,它们的所有属性现在都是相应的顶级。
azurerm 提供程序文档中的示例使用 storage_profile是不正确的,这是:
storage_profile {
storage_mb = var.storageProfile2.storageMb
backup_retention_days = var.storageProfile2.backupRetentionDays
geo_redundant_backup = var.storageProfile2.geoRedundantBackup
}
应该重写为(资源上的直接属性,而不是块内):
storage_mb                    = var.storageProfile2.storageMb
backup_retention_days = var.storageProfile2.backupRetentionDays
geo_redundant_backup_enabled = var.storageProfile2.geoRedundantBackup
还有您的 storageProfile2 应更新变量声明以设置 的类型geoRedundantBackup bool :
variable storageProfile2 {
default = {
storageMb = 102400
backupRetentionDays = 15
geoRedundantBackup = false
}
type = object({ storageMb=number, backupRetentionDays=number, geoRedundantBackup=bool })
}
azurerm provider v2.7.0于 2020 年 4 月 23 日发布,包括以下更改:
  • azurerm_postgres_server - all properties in the storage_profile block have been moved to the top level (#6459)
  • azurerm_postgres_server - the following properties were renamed and changed to a boolean type: ssl_enforcement to ssl_enforcement_enabled, geo_redundant_backup to backup_geo_redundant_enabled, and auto_grow to auto_grow_enabled (#6459)

azurerm provider v2.10.0于 2020 年 5 月 12 日发布附加 storage_profile 被压扁:
  • azurerm_mariadb_server - all properties in the storage_profile block have been moved to the top level (#6865)
  • azurerm_mysql_server - all properties in the storage_profile block have been moved to the top level (#6833)
  • azurerm_mariadb_server - the following properties were renamed and changed to a boolean type: ssl_enforcement to ssl_enforcement_enabled, geo_redundant_backup to geo_redundant_backup_enabled, and auto_growazurerm_mysql_server - support for the create_mode property allowing the creation of replicas, point in time restores, and geo restors (#6833)
  • azurerm_mysql_server - the following properties were renamed and changed to a boolean type: ssl_enforcement to ssl_enforcement_enabled, geo_redundant_backup to geo_redundant_backup_enabled, and auto_grow to auto_grow_enabled (#6833)

旁白:代码风格
Terraform 中的常规代码样式:
  • 使用 snake_case而不是 camelCase (未正式化,但每个提供商都遵循此示例)
  • 引用顶级名称,如资源和变量名称
  • 在组中对齐等号(它们之间没有多个换行符)
  • variable "storage_profile_2" {
    default = {
    storage_mb = 102400
    backup_retention_days = 15
    geo_redundant_backup_enabled = false
    }
    type = object(
    {
    storage_mb = number
    backup_retention_days = number
    geo_redundant_backup_enabled = bool
    }
    )
    }
    并分配属性如下
    storage_mb                   = var.storage_profile_2.storage_mb
    backup_retention_days = var.storage_profile_2.backup_retention_days
    geo_redundant_backup_enabled = var.storage_profile_2.geo_redundant_backup_enabled
    Terraform 代码在全局范围内越一致,如果我们需要处理其他人的代码,我们所有从业者就越容易。

    关于地形图和对象变量作为输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62465697/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com