生成多个名称/使用结果输出的方法是什么?

huangapple go评论71阅读模式
英文:

How to generate multiple names / use results output?

问题

当使用 azurecaf 生成多个名称,如下面的代码时,如何使用 [results output][1]?

    resource "azurecaf_name" "names" {
      name            = var.appname
      resource_type   = "azurerm_resource_group"
      resource_types  = ["azurerm_mssql_database"]
      prefixes        = [var.environment]
      suffixes        = [var.resource_group_location_short]
      random_length   = 5
      clean_input     = false
    }

> **results** - 基于 resource_types 列表生成的 Azure 资源的名称

如何使用它?另外,我是否可以以某种方式调试/打印出 `results` 的样子?(我不知道它是一个数组、一个键值结构等)

  [1]: https://github.com/aztfmod/terraform-provider-azurecaf#attributes-reference
英文:

When using azurecaf to generate multiple names like in the following code, how do I use the results output?

resource "azurecaf_name" "names" {
  name            = var.appname
  resource_type   = "azurerm_resource_group"
  resource_types  = ["azurerm_mssql_database"]
  prefixes        = [var.environment]
  suffixes        = [var.resource_group_location_short]
  random_length   = 5
  clean_input     = false
}

> results - The generated name for the Azure resources based in the resource_types list

How to use this? Also, can I somehow debug / print out what results looks like? (I don't know if it is an array, a key-value structure etc)

答案1

得分: 2

您可以以两种常见的方式查看结果。适用于资源的所有属性。

1 将所需属性导出为 terraform output

  • 当您将任何属性默认设置为代码中的输出时,terraform 将使用 terraform apply 显示值。

在您的用例中。

output "caf_name_result" {
  value = azurecaf_name.names.result
}
output "caf_name_results" {
  value = azurecaf_name.names.results
}

使用上述输出定义应用配置,您将在终端上获得以下输出。

Outputs:

caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
caf_name_results = tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})
  • 在第一次成功的 terraform apply 之后,您可以随时使用 terraform output 命令查看它们。
$ terraform output       
caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
caf_name_results = tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})

您还可以非常具体地检查特定的输出值。

$ terraform output caf_name_results
tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})

[2] 通过 Terraform State 命令查看已应用的资源

只有在应用资源后,以及在仅当 terraform 执行是在运行此命令的同一机器上完成的情况下,才可以使用此方法(简单地说,执行 terraform 的身份满足所有身份验证、授权和网络连接条件)。

不建议这样做,只是在需要快速查看已应用资源时提供的另一种选项。

$ terraform state list 
azurecaf_name.names
$ terraform state show azurecaf_name.names
# azurecaf_name.names:
resource "azurecaf_name" "names" {
    clean_input    = false
    id             = "YXp1cmVybV9yZXNvdXJjZV9ncm91cAlkZXYtcmctc3RhY2tvdmVyZmxvdy15b2RncC13ZXUKYXp1cmVybV9tc3NxbF9kYXRhYmFzZQlkZXYtc3FsZDZmcmVlLXNwZGVtb25pYy15b2RncC13ZXU="
    name           = "stackoverflow"
    passthrough    = false
    prefixes       = [
        "dev",
    ]
    random_length  = 5
    random_seed    = 1676730686950185
    random_string  = "yodgp"
    resource_type  = "azurerm_resource_group"
    resource_types = [
        "azurerm_mssql_database",
    ]
    result         = "dev-rg-stackoverflow-yodgp-weu"
    results        = {
        "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
        "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
    }
    separator      = "-"
    suffixes       = [
        "weu",
    ]
    use_slug       = true
}
英文:

You can view the results in two common ways. It is applicable to all attributes of the resource.

1 Exporting the attribute required as a terraform output.

  • When you add any attribute as an output in your code by default terraform will show you the values with terraform apply.

In your used case.

output "caf_name_result" {
  value = azurecaf_name.names.result
}
output "caf_name_results" {
  value = azurecaf_name.names.results
}

Apply the config with the above outputs definitions you will have the below output on your terminal.

Changes to Outputs:
  + caf_name_result  = (known after apply)
  + caf_name_results = (known after apply)
azurecaf_name.names: Creating...
azurecaf_name.names: Creation complete after 0s [id=YXp1cmVybV9yZXNvdXJjZV9ncm91cAlkZXYtcmctc3RhY2tvdmVyZmxvdy15b2RncC13ZXUKYXp1cmVybV9tc3NxbF9kYXRhYmFzZQlkZXYtc3FsZGItc3RhY2tvdmVyZmxvdy15b2RncC13ZXU=]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
caf_name_results = tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})
  • After the first successful terraform apply you can view them anytime when you want by using the terraform output command.
$ terraform output       
caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
caf_name_results = tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})

You can be very specific also to check only particular output value.

$ terraform output caf_name_results
tomap({
  "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
  "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
})

[2] View your applied resources via Terraform State Commands

This is only available after the resources are applied and only in cases when terraform execution was done from the same machine where this command is running. (in simple the identity doing terraform execution satisfies all the authentication, authorization and network connectivity conditions. )

> It is not recommended, just to share another option available when requiring a quick look on the resources applied.

$ terraform state list 
azurecaf_name.names
$ terraform state show azurecaf_name.names
# azurecaf_name.names:
resource "azurecaf_name" "names" {
    clean_input    = false
    id             = "YXp1cmVybV9yZXNvdXJjZV9ncm91cAlkZXYtcmctc3RhY2tvdmVyZmxvdy15b2RncC13ZXUKYXp1cmVybV9tc3NxbF9kYXRhYmFzZQlkZXYtc3FsZGItc3RhY2tvdmVyZmxvdy15b2RncC13ZXU="
    name           = "stackoverflow"
    passthrough    = false
    prefixes       = [
        "dev",
    ]
    random_length  = 5
    random_seed    = 1676730686950185
    random_string  = "yodgp"
    resource_type  = "azurerm_resource_group"
    resource_types = [
        "azurerm_mssql_database",
    ]
    result         = "dev-rg-stackoverflow-yodgp-weu"
    results        = {
        "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
        "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
    }
    separator      = "-"
    suffixes       = [
        "weu",
    ]
    use_slug       = true
}

huangapple
  • 本文由 发表于 2023年2月18日 20:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定