terraform 递归验证
我正在尝试在自动化中运行 terraform validate(v1.16)。 有没有办法在给定路径中的所有目录和子目录中递归运行 terraform 验证。
如果我想在这种情况下运行 Terraform 验证,我是否需要初始化子目录? 例如:
|-d1-infra
| |-outputs.tf
| |-main.tf
| |-tfmodules
| | |-new_host1
| | | |-outputs.tf
| | | |-variables.tf
| | | |-ay.tf
| | | |-cn.tf
| | |-new_host2
| | | |-outputs.tf
| | | |-variables.tf
| | | |-y.tf
| | | |-n.tf
| |-pol.tf
| |-variables.tf
| |-data.tf
| |-backend.tf
| |-host1.tf
我如何在 shell 脚本中验证这一点 目前我正在做,
tf_dirs_to_validate=(d1-infra d2-infra d3-infra)
for d in "${tf_dirs_to_validate[@]}" ; do
if [ "`cd ./$$d && terraform init -input=false -backend=false > /dev/null && terraform validate -json`" ]; then
echo "===>Terraform format check passed successfully in <---- $$d -->"
else
echo "validation failed in <---- $$d -->" && exit 1
fi
done;
但这似乎只在顶级目录上验证,即 d1-infra
d2-infra
d3-infra
但没有' t 验证模块
I am trying to run terraform validate(v1.16) in automation.
Is there a way to run terraform validation recursively in all directories, and subdirectories in a given path.
and if I would want to run terraform validation
in such case do I need to initialize even the subdirectories ?
ex-:
|-d1-infra
| |-outputs.tf
| |-main.tf
| |-tfmodules
| | |-new_host1
| | | |-outputs.tf
| | | |-variables.tf
| | | |-ay.tf
| | | |-cn.tf
| | |-new_host2
| | | |-outputs.tf
| | | |-variables.tf
| | | |-y.tf
| | | |-n.tf
| |-pol.tf
| |-variables.tf
| |-data.tf
| |-backend.tf
| |-host1.tf
How can I validate this in a shell script
currently I am doing
tf_dirs_to_validate=(d1-infra d2-infra d3-infra)
for d in "${tf_dirs_to_validate[@]}" ; do
if [ "`cd ./$d && terraform init -input=false -backend=false > /dev/null && terraform validate -json`" ]; then
echo "===>Terraform format check passed successfully in <---- $d -->"
else
echo "validation failed in <---- $d -->" && exit 1
fi
done;
but this seems to validate only on the top level dir i.e, d1-infra
d2-infra
d3-infra
but doesn't validates the modules
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Jenifer,我想在这种情况下你可以寻求 terragrunt 的帮助。 Terragrunt 是 Terraform 的薄包装器,因此当您运行任何 terragrunt 命令(如计划、验证、应用等)时,Terragrunt 会执行后续的 terraform 命令。
在您的情况下,您可以使用以下命令
参考: https://terragrunt.gruntwork.io/docs/reference/cli-options/#validate-all-deprecated-use-run-all
同样可以合并到你的bash中 脚本。
@Jenifer, I guess you can take help of terragrunt in this case. Terragrunt is a thin wrapper for Terraform, so when you run any terragrunt command (like plan,validate, apply etc) Terragrunt executes subsequent terraform command.
In your case, you can use below command
Reference : https://terragrunt.gruntwork.io/docs/reference/cli-options/#validate-all-deprecated-use-run-all
The same can be incorporated in your bash script.