【问题标题】:How to identify infra already matches terraform config如何识别已经与 terraform 配置匹配的基础设施
【发布时间】:2021-08-19 16:54:11
【问题描述】:
我正在创建用于在 Azure DevOps 中执行 terraform 脚本的管道,而不是运行预定义的 terraform 任务(我们的组织中尚未包含该任务),我计划通过 Azure CLI 运行脚本。我的问题是,如果“没有更改。您的基础设施与配置相匹配。”,我们是否可以在 terraform 计划中识别出来,这样我就不必运行 terraform apply。
我知道如果配置匹配,则 terraform apply 不会造成伤害。我打算跳过该命令,有没有办法通过 Azure CLI 检查计划输出并做出决定?
【问题讨论】:
标签:
azure-devops
terraform
azure-cli
terraform-provider-azure
【解决方案1】:
是的,你可以。你应该使用-detailed-exitcode。这里有一个 bash 示例:
terraform init
terraform plan -out=plan.out -detailed-exitcode
status=$?
echo "The terraform command exit status : ${status}"
#run apply only on changed detected
if [ $status -eq 2 ]; then
echo 'Applying terraform changes'
terraform apply -auto-approve plan.out
elif [ $status -eq 1 ]; then
exit 1
fi
这里有link to documentation
命令退出时返回详细的退出代码。提供时,此参数会更改退出代码及其含义,以提供有关结果计划包含内容的更详细信息:
- 0 = 成功,差异为空(无更改)
- 1 = 错误
- 2 = 以非空差异成功(存在更改)