Terraform:如何自动解决从旧状态文件中删除的提供程序问题?

Ale*_*hen 4 terraform terraform0.12+

我正在努力将模板从 terraform 0.12.31 升级到 0.13.7,我们需要确保我们有一个自动系统来处理在旧版本下创建的部署。

null我正在解决的一个问题是我在迁移中删除了所有提供商的使用。当我尝试使用 terraform 0.13 版本应用或计划在 0.12 上创建的状态文件时,我收到以下错误:

$ terraform plan --var-file MY_VAR_FILE.json 
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

Error: Provider configuration not present

To work with
module.gcp_volt_site.module.ce_config.data.null_data_source.hosts_localhost
its original provider configuration at
provider["registry.terraform.io/-/null"] is required, but it has been removed.
This occurs when a provider configuration is removed while objects created by
that provider still exist in the state. Re-add the provider configuration to
destroy
module.gcp_volt_site.module.ce_config.data.null_data_source.hosts_localhost,
after which you can remove the provider configuration again.


Error: Provider configuration not present

To work with
module.gcp_volt_site.module.ce_config.data.null_data_source.cloud_init_master
its original provider configuration at
provider["registry.terraform.io/-/null"] is required, but it has been removed.
This occurs when a provider configuration is removed while objects created by
that provider still exist in the state. Re-add the provider configuration to
destroy
module.gcp_volt_site.module.ce_config.data.null_data_source.cloud_init_master,
after which you can remove the provider configuration again.


Error: Provider configuration not present

To work with
module.gcp_volt_site.module.ce_config.data.null_data_source.vpm_config its
original provider configuration at provider["registry.terraform.io/-/null"] is
required, but it has been removed. This occurs when a provider configuration
is removed while objects created by that provider still exist in the state.
Re-add the provider configuration to destroy
module.gcp_volt_site.module.ce_config.data.null_data_source.vpm_config, after
which you can remove the provider configuration again.
Run Code Online (Sandbox Code Playgroud)

terraform state rm我的手动解决方案是在列出的所有模块上运行:

terraform state rm module.gcp_volt_site.module.ce_config.data.null_data_source.vpm_config
terraform state rm module.gcp_volt_site.module.ce_config.data.null_data_source.hosts_localhost
terraform state rm module.gcp_volt_site.module.ce_config.data.null_data_source.cloud_init_master
Run Code Online (Sandbox Code Playgroud)

我想知道如何自动执行此操作以使脚本能够进行这些更改。

是否有某种 terraform 命令可以用来列出这些已删除的模块,而无需进行额外的测试,以便我可以循环运行以terraform state rm从状态文件中删除它们?

或者是否有某种 terraform 命令可以以通用方式自动执行此操作,例如terraform state rm -all-not-present

Ale*_*hen 5

这给了我一个可以迭代的列表terraform state rm $MODULE_NAME

$ terraform state list | grep 'null_data_source' 
module.gcp_volt_site.module.ce_config.data.null_data_source.cloud_init_master
module.gcp_volt_site.module.ce_config.data.null_data_source.hosts_localhost
module.gcp_volt_site.module.ce_config.data.null_data_source.vpm_config
Run Code Online (Sandbox Code Playgroud)