首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Terraform:如何自动解决从旧状态文件中删除的提供程序问题?

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

Stack Overflow用户
提问于 2021-12-21 20:28:26
回答 2查看 4.4K关注 0票数 2

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

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

代码语言:javascript
运行
复制
$ 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.

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

代码语言:javascript
运行
复制
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

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

我是否可以使用terraform命令列出这些删除的模块,而不需要额外的测试,这样我就可以循环运行terraform state rm来从状态文件中删除它们了吗?

或者是否有某种terraform命令可以以通用方式(如terraform state rm -all-not-present )自动完成此操作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-22 12:40:38

有几种可能性。如果没有该模块的源代码,那么很难这样说,提供这样的代码可能会有帮助。

几点建议

清洗缓存

删除.terraform目录(通常在运行init、plan和apply的目录中)。模块的旧版本可以缓存,但仍然包含空引用。

状态刷新

使用Terraform Refresh,您应该能够扫描下位并使状态对齐。

可能是危险的,而不是Hashicorp推荐的。

人工清除

您建议的状态rm命令在这里可能会有所帮助,而且相当安全。您有一个--dry-run选项,您可以指向特定使用terraform state rm的资源,就像您建议手动删除处于状态的模块中的资源一样。同样,您希望检查模块引用是否指向该模块的旧版本或缓存旧版本,否则它们将被重新创建。

不,没有rm缺失,但是如果您知道将丢失的是所有的空数据源,您可以使用terraform state ls列出所有这些资源,然后在一个循环中遍历每个资源。

票数 1
EN

Stack Overflow用户

发布于 2021-12-30 18:36:37

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

代码语言:javascript
运行
复制
$ 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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70441257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档