What specific syntax must be changed below in order for the terraform cli to return only one
json
object describing state instead of 4json
objects?
The command we are currently running is:
terraform show --json
The results we get from the above command include 4 json
objects as follows:
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": [ ... ]
}
What specifically do we have to change in order to receive one single
json
object in response which summarizes the state within that one singlejson
object?
CodePudding user response:
This is not a standard terraform show --json
output. I suspect that you set TF_LOG to DEBUG
or INFO
. Thus you have to change that env variable to normal value. For example, if you run it in bash
on linux you can change it in one line as follows:
TF_LOG=ERROR terraform show --json
Alternatively, in an os-agnostic language like Python, you would use two different lines to first set TF_LOG=ERROR
on one line using os.environ
and then run a shell command on a second line to run terraform show --json
using something like subprocess.popen
. Remembering of course to re-set TF_LOG
afterwards to whatever the user has as its default.