当我从 plan -out=file 执行 terraform apply 时,依赖性不一致

Sha*_*asi 12 lockfile terraform hashicorp

我正在尝试使用远程后端在 GCP 上创建新资源\n执行 terraform init plan -out=tfplan 然后 terraform apply tfplan 后,出现以下错误:

\n
Error: Inconsistent dependency lock file\n\xe2\x94\x82 \n\xe2\x94\x82 The following dependency selections recorded in the lock file are\n\xe2\x94\x82 inconsistent with the configuration in the saved plan:\n\xe2\x94\x82 Terraform 0.13 and earlier allowed provider version constraints inside the\n\xe2\x94\x82 provider configuration block, but that is now deprecated and will be\n\xe2\x94\x82 removed in a future version of Terraform. To silence this warning, move the\n\xe2\x94\x82 provider version constraint into the required_providers block.\n\xe2\x94\x82 \n\xe2\x94\x82 (and 22 more similar warnings elsewhere)\n\xe2\x95\xb5\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/aws: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/external: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/google-beta: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/google: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/local: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/null: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/random: required by this configuration but no version is selected\n\xe2\x94\x82 \n\xe2\x94\x82 A saved plan can be applied only to the same configuration it was created\n\xe2\x94\x82 from. Create a new plan from the updated configuration.\n\xe2\x95\xb5\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Inconsistent dependency lock file\n\xe2\x94\x82 \n\xe2\x94\x82 The given plan file was created with a different set of external dependency\n\xe2\x94\x82 selections than the current configuration. A saved plan can be applied only\n\xe2\x94\x82 to the same configuration it was created from.\n\xe2\x94\x82 \n\xe2\x94\x82 Create a new plan from the updated configuration. \n
Run Code Online (Sandbox Code Playgroud)\n

另一方面,当我执行 terraform init plan 并应用 -auto-approve 其工作时没有任何问题

\n

Mar*_*ins 17

部分工作terraform init是将所有必需的提供程序安装到临时目录中.terraform/providers,以便其他 Terraform 命令可以运行它们。对于全新的提供程序,它还将更新依赖项锁定文件以记录它选择的版本,以便将来terraform init可以保证做出相同的决定。

\n

terraform apply tfplan如果您在与运行位置不同的目录中运行terraform plan -out=tfplan,则所需提供程序插件的本地缓存将不可用,因此应用将失败。

\n

除此之外,似乎当您在terraform init创建计划之前运行时,Terraform 必须安装一些以前未记录在依赖项锁定文件中的新提供程序,因此它更新了依赖项锁定文件。但是,当您terraform apply tfplan稍后运行时,对锁定文件的这些更改不可见,因此 Terraform 报告当前锁定与创建计划的锁定不一致。

\n

在自动化中运行Terraform指南有一个章节“计划并在不同的机器上应用”,其中讨论了当您尝试在创建计划的位置之外的其他地方应用时会出现的一些特殊问题。但是,我将尝试根据此错误消息总结与您的情况最相关的部分。

\n
\n

首先,应在您的版本控制系统中记录最新的依赖项锁定文件,以便您的自动化仅重新安装先前选择的提供程序,而不会进行全新的提供程序选择。这将确保您的所有运行都使用相同的提供程序版本,并且升级始终在您的控制下进行。

\n

您可以通过添加-lockfile=readonly选项来让自动化检测到这种情况terraform init,如果需要更改依赖项锁定文件才能执行其工作,这会使该命令失败:

\n
terraform init -lockfile=readonly\n
Run Code Online (Sandbox Code Playgroud)\n

如果您发现自动化失败,那么适当的修复方法是在开发环境内terraform init -lockfile=readonly运行,然后将更新的锁定文件签入版本控制系统。

\n

如果您无法在开发环境中初始化远程后端,您可以跳过该步骤,但仍然通过添加来安装所需的提供程序-backend=false,如下所示:

\n
terraform init -backend=false\n
Run Code Online (Sandbox Code Playgroud)\n
\n

在应用步骤之前再次重新安装相同的提供程序是这里问题的另一部分。

\n

我上面链接的指南建议通过在规划为工件后归档整个工作目录,然后在应用步骤中的相同路径重新提取它来实现此目的。这是最彻底的解决方案,特别是 Terraform Cloud 所做的,以确保规划期间在磁盘上创建的任何其他文件(例如使用提供商archive_file的数据源hashicorp/archive)将在应用阶段保留下来。

\n

但是,如果您知道您的配置本身在规划期间不会修改文件系统(如果可能,这是最佳实践),那么在terraform init -lockfile=readonly运行之前重新运行也是有效的terraform apply tfplan,这将因此重新安装之前选择的文件系统提供程序,以及terraform init通常执行的所有其他工作目录初始化工作。

\n
\n

最后一点,与其余部分无关,Terraform 似乎还打印了有关已弃用的语言功能的警告,并且在您的系统上,警告输出与错误输出交错,使得第一条消息令人困惑,因为它包含一个段落从里面的警告。

\n

我相信预期的错误消息文本(没有警告中错误的额外内容)如下:

\n
Error: Inconsistent dependency lock file\n\xe2\x94\x82 \n\xe2\x94\x82 The following dependency selections recorded in the lock file are\n\xe2\x94\x82 inconsistent with the configuration in the saved plan:\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/aws: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/external: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/google-beta: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/google: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/local: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/null: required by this configuration but no version is selected\n\xe2\x94\x82   - provider registry.terraform.io/hashicorp/random: required by this configuration but no version is selected\n\xe2\x94\x82 \n\xe2\x94\x82 A saved plan can be applied only to the same configuration it was created\n\xe2\x94\x82 from. Create a new plan from the updated configuration.\n\xe2\x95\xb5\n
Run Code Online (Sandbox Code Playgroud)\n


Man*_*Ali 5

可能有多种原因,但主要原因是您可能会更新模板,并且您可能会使用一些插件,在我的情况下,我更新了模板并添加了随机模块,该问题的解决方案是使用以下命令升级锁定文件

terraform init -upgrade
Run Code Online (Sandbox Code Playgroud)

它将安装新模块并自动使锁定文件保持一致