我正在将模板从terraform 0.12.31升级到0.13.7,我们需要确保我们有一个自动系统来处理在旧版本下创建的部署。
我正在解决的一个问题是,我删除了移动过程中所有null
提供程序的使用。当我尝试在0.12上创建的状态文件上应用或规划时,当使用terraform版本0.13时,我会收到以下错误:
$ 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
:
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
)自动完成此操作?
发布于 2021-12-22 12:40:38
有几种可能性。如果没有该模块的源代码,那么很难这样说,提供这样的代码可能会有帮助。
几点建议
清洗缓存
删除.terraform目录(通常在运行init、plan和apply的目录中)。模块的旧版本可以缓存,但仍然包含空引用。
状态刷新
使用Terraform Refresh,您应该能够扫描下位并使状态对齐。
可能是危险的,而不是Hashicorp推荐的。
人工清除
您建议的状态rm命令在这里可能会有所帮助,而且相当安全。您有一个--dry-run
选项,您可以指向特定使用terraform state rm
的资源,就像您建议手动删除处于状态的模块中的资源一样。同样,您希望检查模块引用是否指向该模块的旧版本或缓存旧版本,否则它们将被重新创建。
不,没有rm缺失,但是如果您知道将丢失的是所有的空数据源,您可以使用terraform state ls
列出所有这些资源,然后在一个循环中遍历每个资源。
发布于 2021-12-30 18:36:37
这给了我一个可以使用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
https://stackoverflow.com/questions/70441257
复制相似问题