如何在 terraform 中回滚到以前的状态 [英] How to Rollback to Previous State in terraform

查看:48
本文介绍了如何在 terraform 中回滚到以前的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 terraform 任务并试图了解状态文件的工作原理.我创建了具有

I am working on terraform tasks and trying to understand how state files work. I have created main.tf file which has

vpc,firewall,subnet,compute_instance

必须在 GCP 中创建.因此,我已将其应用于 GCP 环境并创建了一个文件名 terraform.tfstate 文件,并且我将此文件备份到名为 1st-run 的文件夹中.

which has to be create in GCP. So i have applied this to GCP environment and a file name terraform.tfstate file got created and i did backup of this file into folder called 1st-run.

现在我已经用

2vpc,2firewalls,2subnets,compute_instance

因为我需要为我的 vm.Did 添加另一个 nic.是否已应用 terraform 并已创建环境并已创建 terraform.tfstate 文件.我将此文件备份到名为 2nd-run 的文件夹中.

as i need to add another nic for my vm.Did terraform apply and environment got created and terraform.tfstate file got created. I did backup of this file into folder called 2nd-run.

我想回滚执行第一次运行的环境.我有第一次运行文件夹中的状态文件.

I want to rollback the environment where i have executed for 1st-run. I have that state file which is in 1st-run folder.

使用statefile而不是触摸代码来回滚的命令是什么,以便我的GCP环境自动拥有

What is the command to rollback by using statefile instead of touching the code so that automatically my GCP environment will have

vpc,firewall,subnet,compute_instance

这是我第一次执行.

推荐答案

没有办法回滚到今天在 Terraform 中的状态文件中描述的先前状态.Terraform 总是以从先前状态(最新状态快照)移动到配置所代表的目标状态为目标来计划更改.Terraform 还将配置用于状态中未跟踪的信息,例如提供程序配置.

There is no way to roll back to a previous state as described in a state file in Terraform today. Terraform always plans changes with the goal of moving from the prior state (the latest state snapshot) to the goal state represented by the configuration. Terraform also uses the configuration for information that is not tracked in the state, such as the provider configurations.

在 Terraform 中表示回滚"的常用方法是将您的配置置于版本控制中并在每次更改之前提交,然后如果需要,您可以使用版本控制系统的功能恢复到旧配置.

The usual way to represent "rolling back" in Terraform is to put your configuration in version control and commit before each change, and then you can use your version control system's features to revert to an older configuration if needed.

但并非所有更改都可以仅通过还原 VCS 更改来回滚.例如,如果您在一次提交中为该提供程序添加了一个新的 provider 块和资源,然后应用了结果,为了回滚,您需要更改配置以仍然包含 provider 块,但 not 包含任何 resource 块,因此您需要在还原期间调整配置.然后 Terraform 将使用剩余的 provider 块来配置提供程序以运行销毁操作,之后您最终也可以删除 provider 块.

Not all changes can be rolled back purely by reverting a VCS change though. For example, if you added a new provider block and resources for that provider all in one commit and then applied the result, in order to roll back you'd need to change the configuration to still include the provider block but not include any of the resource blocks, so you'd need to adjust the configuration during the revert. Terraform will then use the remaining provider block to configure the provider to run the destroy actions, after which you can finally remove the provider block too.

这篇关于如何在 terraform 中回滚到以前的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆