terraform 显示返回 4 个 json 对象

Posted

技术标签:

【中文标题】terraform 显示返回 4 个 json 对象【英文标题】:terraform show returning 4 json objects 【发布时间】:2022-01-03 09:29:37 【问题描述】:

为了让 terraform cli 只返回一个描述状态的 json 对象而不是 4 个 json 对象,必须更改以下哪些特定语法?

我们当前运行的命令是:

terraform show --json  

我们从上面的命令得到的结果包括4个json对象如下:

2021-11-24T16:32:19.181-0800 [INFO]  Terraform version: 1.0.8
2021-11-24T16:32:19.181-0800 [INFO]  Go runtime version: go1.16.4
2021-11-24T16:32:19.181-0800 [INFO]  CLI args: []string"terraform", "show", "--json"
2021-11-24T16:32:19.193-0800 [INFO]  CLI command args: []string"show", "--json"
GET /containerName/my.tfstate HTTP/1.1
Host: myStorageAccountName.blob.core.windows.net
User-Agent: Terraform/1.0.8
X-Ms-Date: Thu, 25 Nov 2021 00:32:19 GMT
X-Ms-Version: 2018-11-09
Accept-Encoding: gzip
2021-11-24T16:32:20.752-0800 [DEBUG] Azure Backend Response for https://myStorageAccountName.blob.core.windows.net/containerName/my.tfstate:
HTTP/1.1 200 OK
Content-Length: 22852
Accept-Ranges: bytes
Content-Md5: someHashCode==
Content-Type: application/json
Date: Thu, 25 Nov 2021 00:32:19 GMT
Etag: "someHexTag"
Last-Modified: Tue, 23 Nov 2021 23:11:57 GMT
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
X-Ms-Blob-Type: BlockBlob
X-Ms-Creation-Time: Tue, 23 Nov 2021 23:02:50 GMT
X-Ms-Lease-State: available
X-Ms-Lease-Status: unlocked
X-Ms-Request-Id: another-request-id
X-Ms-Server-Encrypted: true
X-Ms-Version: 2018-11-09


  "version": 4,
  "terraform_version": "1.0.8",
  "serial": 1,
  "lineage": "...",
  "outputs":  ... ,
  "resources": [ ... ]

2021-11-24T16:32:20.754-0800 [TRACE] backend/local: requesting state lock for workspace "default"
2021-11-24T16:32:20.754-0800 [TRACE] backend/local: reading remote state for workspace "default"
2021-11-24T16:32:20.754-0800 [DEBUG] Azure Backend Request:
GET /containerName/my.tfstate HTTP/1.1
Host: myStorageAccountName.blob.core.windows.net
User-Agent: Terraform/1.0.8
X-Ms-Date: Thu, 25 Nov 2021 00:32:20 GMT
X-Ms-Version: 2018-11-09
Accept-Encoding: gzip
2021-11-24T16:32:20.910-0800 [DEBUG] Azure Backend Response for https://myStorageAccountName.blob.core.windows.net/containerName/my.tfstate:
HTTP/1.1 200 OK
Content-Length: 22852
Accept-Ranges: bytes
Content-Md5: someHashCode==
Content-Type: application/json
Date: Thu, 25 Nov 2021 00:32:19 GMT
Etag: "someHexTag"
Last-Modified: Tue, 23 Nov 2021 23:11:57 GMT
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
X-Ms-Blob-Type: BlockBlob
X-Ms-Creation-Time: Tue, 23 Nov 2021 23:02:50 GMT
X-Ms-Lease-State: available
X-Ms-Lease-Status: unlocked
X-Ms-Request-Id: some-request-id
X-Ms-Server-Encrypted: true
X-Ms-Version: 2018-11-09


  "version": 4,
  "terraform_version": "1.0.8",
  "serial": 1,
  "lineage": "...",
  "outputs":  ... ,
  "resources": [ ... ]

2021-11-24T16:32:20.925-0800 [INFO]  provider: configuring client automatic mTLS
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Applications"..: timestamp=2021-11-24T16:32:21.017-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Applications"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "App Role Assignments"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "App Role Assignments"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "ConditionalAccess"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "ConditionalAccess"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Directory Roles"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Directory Roles"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Domains"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Domains"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Groups"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Groups"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Invitations"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Invitations"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Service Principals"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.018-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Service Principals"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.019-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Resources for "Users"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.019-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: 2021/11/24 16:32:21 [DEBUG] Registering Data Sources for "Users"..: timestamp=2021-11-24T16:32:21.018-0800
2021-11-24T16:32:21.021-0800 [INFO]  provider.terraform-provider-azuread_v2.6.0_x5.exe: configuring server automatic mTLS: timestamp=2021-11-24T16:32:21.020-0800
2021-11-24T16:32:21.162-0800 [INFO]  provider: configuring client automatic mTLS
2021-11-24T16:32:21.332-0800 [INFO]  provider.terraform-provider-azurerm_v2.79.1_x5.exe: configuring server automatic mTLS: timestamp=2021-11-24T16:32:21.332-0800
2021-11-24T16:32:21.579-0800 [INFO]  provider: configuring client automatic mTLS
2021-11-24T16:32:21.657-0800 [INFO]  provider.terraform-provider-http_v2.1.0_x5.exe: configuring server automatic mTLS: timestamp=2021-11-24T16:32:21.656-0800

GET /containerName/my.tfstate HTTP/1.1
Host: myStorageAccountName.blob.core.windows.net
User-Agent: Terraform/1.0.8
X-Ms-Date: Thu, 25 Nov 2021 00:32:21 GMT
X-Ms-Version: 2018-11-09
Accept-Encoding: gzip
2021-11-24T16:32:22.288-0800 [DEBUG] Azure Backend Response for https://myStorageAccountName.blob.core.windows.net/containerName/my.tfstate:
HTTP/1.1 200 OK
Content-Length: 22852
Accept-Ranges: bytes
Content-Md5: someHashCode==
Content-Type: application/json
Date: Thu, 25 Nov 2021 00:32:21 GMT
Etag: "someHexTag"
Last-Modified: Tue, 23 Nov 2021 23:11:57 GMT
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
X-Ms-Blob-Type: BlockBlob
X-Ms-Creation-Time: Tue, 23 Nov 2021 23:02:50 GMT
X-Ms-Lease-State: available
X-Ms-Lease-Status: unlocked
X-Ms-Request-Id: a-third-request-id
X-Ms-Server-Encrypted: true
X-Ms-Version: 2018-11-09


  "version": 4,
  "terraform_version": "1.0.8",
  "serial": 1,
  "lineage": "...",
  "outputs":  ... ,
  "resources": [ ... ]


2021-11-24T16:32:22.290-0800 [DEBUG] Azure Backend Request:
GET /containerName/my.tfstate HTTP/1.1
Host: myStorageAccountName.blob.core.windows.net
User-Agent: Terraform/1.0.8
X-Ms-Date: Thu, 25 Nov 2021 00:32:22 GMT
X-Ms-Version: 2018-11-09
Accept-Encoding: gzip
2021-11-24T16:32:22.462-0800 [DEBUG] Azure Backend Response for https://myStorageAccountName.blob.core.windows.net/containerName/my.tfstate:
HTTP/1.1 200 OK
Content-Length: 22852
Accept-Ranges: bytes
Content-Md5: someHashCode==
Content-Type: application/json
Date: Thu, 25 Nov 2021 00:32:21 GMT
Etag: "someHexTag"
Last-Modified: Tue, 23 Nov 2021 23:11:57 GMT
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
X-Ms-Blob-Type: BlockBlob
X-Ms-Creation-Time: Tue, 23 Nov 2021 23:02:50 GMT
X-Ms-Lease-State: available
X-Ms-Lease-Status: unlocked
X-Ms-Request-Id: a-4th-request-id
X-Ms-Server-Encrypted: true
X-Ms-Version: 2018-11-09


  "version": 4,
  "terraform_version": "1.0.8",
  "serial": 1,
  "lineage": "...",
  "outputs":  ... ,
  "resources": [ ... ]
  

为了接收一个单一的 json 对象作为响应,我们必须具体更改哪些内容,该对象总结了该单一 json 对象中的状态?

【问题讨论】:

【参考方案1】:

这不是标准的terraform show --json 输出。我怀疑您将TF_LOG 设置为DEBUGINFO。因此,您必须将该环境变量更改为正常值。例如,如果你在linux上运行bash,你可以在一行中进行如下更改:

TF_LOG=ERROR terraform show --json

或者,在像 Python 这样的与操作系统无关的语言中,您可以使用两条不同的行,首先使用 os.environ 在一行上设置 TF_LOG=ERROR,然后在第二行上运行 shell 命令以使用某些东西运行 terraform show --json喜欢subprocess.popen。当然要记住在之后将TF_LOG 重新设置为用户的默认值。

【讨论】:

你为什么把TF_LOG=ERRORterraform show --json放在同一行?这不是两个不同的命令吗? @CodeMed 只是将TF_LOG 的值设置为ERROR。关键是您在某处将其设置为DEBUG。它可能是您的环境变量。你必须找到你在哪里设置它,并将其更改为标准值。否则,您会从命令中获得这些详细的输出。 是的,我明白了。因此,为了使用您的建议,我在我们的脚本中 terraform show --json 命令上方的行中设置了TF_LOG=ERROR,然后我们在terraform show --json 行之后再次设置了TF_LOG=DEBUG。 ...我只是说将来您的答案的另一位读者可能不知道将其放在两个单独的行中。 ...如您的答案所示,将其放在一行会导致错误,可通过将其拆分为两行来解决。 @CodeMed 这是一行,而不是两行。我在 linux 上使用bash 来运行它。我可以更新答案以澄清这一点。感谢您指出。

以上是关于terraform 显示返回 4 个 json 对象的主要内容,如果未能解决你的问题,请参考以下文章

单独 JSON 文件中的 Terraform Bigquery 表架构

json HashiCorp Terraform AWS和DOCKER正在运行

想在 uitablelview 中只返回 1 个单元格,但由于 json 响应,显示了多个单元格

将 terraform HCL 变量 type=map(any) 转换为 JSON

php json - 以返回的数组顺序显示问题(json 排序)

Terraform aws_iam_role_policy 中的 JSON 语法错误