terraform init 在自动化中创建一个新的工作空间
terraform init creating a new workspace in automation
我在 Internet 上查看过,但没有找到任何接近答案的内容。
我有以下 main.tf :
terraform {
cloud {
organization = "my-organization"
workspaces {
tags = ["app:myapplication"]
}
}
}
我正在使用 terraform cloud,我想在自动化中使用工作区。
为此,我需要先执行 terraform init :
/my/path # terraform init
Initializing Terraform Cloud...
No workspaces found.
There are no workspaces with the configured tags
(app:myapplication) in your Terraform Cloud
organization. To finish initializing, Terraform needs at least one
workspace available.
Terraform can create a properly tagged workspace for you now. Please
enter a name to create a new Terraform Cloud workspace.
Enter a value:
我想做这样的事情:
terraform init -workspace=my-workspace
这样如果不存在就创建。但我没有找到任何东西。创建第一个工作区的唯一方法是手动。
如何使用 ci/cd 实现自动化?
[编辑]
terraform 工作区命令在 init
之前不可用
/src/terraform # terraform workspace list
Error: Terraform Cloud initialization required: please run "terraform
init"
Reason: Initial configuration of Terraform Cloud.
Changes to the Terraform Cloud configuration block require
reinitialization, to discover any changes to the available workspaces.
To re-initialize, run: terraform init
Terraform has not yet made changes to your existing configuration or
state.
您需要使用 TF Cloud/TFE API。您正在使用 TF Cloud,但可以修改端点以将您的安装定位为使用 TFE。
您首先需要list the TF Cloud Workspaces:
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
https://app.terraform.io/api/v2/organizations/my-organization/workspaces
其中 my-organization
是您的 TF Cloud 组织。这将 return 工作空间 JSON 格式。然后,您需要解析 JSON 并迭代现有 TF Cloud 工作区的 maps/hashes/dictionaries。对于每次迭代,在 data
内然后是 name
键将是工作区名称的嵌套值。您将收集工作空间的名称,并根据您希望存在的工作空间的名称进行检查。如果工作空间列表中不存在所需的工作空间,则您 create the TF Cloud workspace:
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/organizations/my-organization/workspaces
再次替换为您的组织和您的特定负载。然后,您可以 terraform init
成功地使用后端指定 Tf Cloud 工作区。
请注意,如果您按照问题中指定的方式自动执行此操作,则构建代理需要连接到 TF Cloud。
我不会将此标记为答案,但我最终做到了,这对我来说是个坏把戏:
export TF_WORKSPACE=myWorkspace
if terraform init -input=false; then echo "already exist"; else (sleep2; echo $TF_WORKSPACE) | terraform init; fi
terraform apply -auto-approve -var myvar=boo
我在 Internet 上查看过,但没有找到任何接近答案的内容。 我有以下 main.tf :
terraform {
cloud {
organization = "my-organization"
workspaces {
tags = ["app:myapplication"]
}
}
}
我正在使用 terraform cloud,我想在自动化中使用工作区。 为此,我需要先执行 terraform init :
/my/path # terraform init
Initializing Terraform Cloud...
No workspaces found.
There are no workspaces with the configured tags (app:myapplication) in your Terraform Cloud organization. To finish initializing, Terraform needs at least one workspace available.
Terraform can create a properly tagged workspace for you now. Please enter a name to create a new Terraform Cloud workspace.
Enter a value:
我想做这样的事情:
terraform init -workspace=my-workspace
这样如果不存在就创建。但我没有找到任何东西。创建第一个工作区的唯一方法是手动。
如何使用 ci/cd 实现自动化?
[编辑] terraform 工作区命令在 init
之前不可用/src/terraform # terraform workspace list
Error: Terraform Cloud initialization required: please run "terraform init"
Reason: Initial configuration of Terraform Cloud.
Changes to the Terraform Cloud configuration block require reinitialization, to discover any changes to the available workspaces.
To re-initialize, run: terraform init
Terraform has not yet made changes to your existing configuration or state.
您需要使用 TF Cloud/TFE API。您正在使用 TF Cloud,但可以修改端点以将您的安装定位为使用 TFE。
您首先需要list the TF Cloud Workspaces:
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
https://app.terraform.io/api/v2/organizations/my-organization/workspaces
其中 my-organization
是您的 TF Cloud 组织。这将 return 工作空间 JSON 格式。然后,您需要解析 JSON 并迭代现有 TF Cloud 工作区的 maps/hashes/dictionaries。对于每次迭代,在 data
内然后是 name
键将是工作区名称的嵌套值。您将收集工作空间的名称,并根据您希望存在的工作空间的名称进行检查。如果工作空间列表中不存在所需的工作空间,则您 create the TF Cloud workspace:
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/organizations/my-organization/workspaces
再次替换为您的组织和您的特定负载。然后,您可以 terraform init
成功地使用后端指定 Tf Cloud 工作区。
请注意,如果您按照问题中指定的方式自动执行此操作,则构建代理需要连接到 TF Cloud。
我不会将此标记为答案,但我最终做到了,这对我来说是个坏把戏:
export TF_WORKSPACE=myWorkspace
if terraform init -input=false; then echo "already exist"; else (sleep2; echo $TF_WORKSPACE) | terraform init; fi
terraform apply -auto-approve -var myvar=boo