Terraform 在自动化中计划人类可读的输出 [英] Terraform plan human readable output in automation

查看:14
本文介绍了Terraform 在自动化中计划人类可读的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过 github/jenkins 设置了一个自动化,以通过 jenkins 发布 terraform plan 的输出作为对 github 中拉取请求的评论.整个编排工作得很好,除了 terraform plan 的输出不是人类可读的,并且在这种自动化中没有为您提供在终端中运行时的样子.我使用了几种方法,例如将 terraform show 用于计划文件,然后将其抓取到自定义文件中并将其作为评论发布在 GitHub PR 中.在每种情况下,输出都包含一些二进制字符.

I have set up an automation through github/jenkins to post the output of terraform plan for the repo through jenkins as a comment to the pull request in github. The entire orchestration works great except for the fact that the output of terraform plan is not that human readable and doesn't provide you in this kind of automation a way as it looks when you run it in a terminal. I used several ways like using terraform show for the plan file, then grabbing that to a custom file and posting that as a comment in GitHub PR. In every case the output contains some binary characters.

我什至使用了 terraform-plan-parserhttps://github.com/lifeomic/terraform-plan-parser但这不适用于 terraform 0.12,并且与以下问题有关:-https://github.com/lifeomic/terraform-plan-parser/issues/31

i even used the terraform-plan-parser https://github.com/lifeomic/terraform-plan-parser but that doesn't work for terraform 0.12 and relates to the below issue :- https://github.com/lifeomic/terraform-plan-parser/issues/31

在自动化中检索任何 terraform 计划的输出以便可以引用它的最佳方法是什么,但是在应用完成之前需要进行检查.在我看来,它只在终端上工作得很好.

What's the best way to retrieve the output of any terraform plan in automation so that it can be referenced however that needs to be to inspect before the apply is done. Looks to me it only works great in a terminal.

这里的任何帮助或建议将一如既往地感激不尽.

Any help or suggestions here will be greatly appreciated as always.

推荐答案

Terraform 默认使用 终端转义序列 使用简单的格式(例如颜色或粗体字)突出显示部分输出.

By default Terraform uses terminal escape sequences to highlight parts of the output with simple formatting such as colors or a bold typeface.

为了在 GitHub 的上下文中准确地重现该结果,需要将终端转义序列转换为 GitHub 能够呈现的形式.

In order to reproduce that result exactly in the context of GitHub would require translating the terminal escape sequences into a form that GitHub is able to render.

不幸的是,GitHub 评论是用 GitHub 风格的 Markdown 编写的,它不支持任何直接的方式来创建类似于我写这篇文章时 Terraform 计划输出的彩色文本.因此,我知道没有简单的方法可以从 GitHub 评论中的 Terraform 计划输出中重现文本格式.

Unfortunately GitHub comments are written in GitHub-flavored Markdown, which doesn't support any direct way to create colored text similar to Terraform's plan output at the time when I'm write this. Therefore I know of no easy way to reproduce the text formatting from the Terraform plan output in a GitHub comment.

如果您使用 -no-color 选项运行 terraform plan,那么它将跳过终端转义序列并生成可以包含在预格式化的纯文本输出Markdown 评论中的文本块.但是,该输出将因此不包括您通常在终端中看到的文本格式.

If you run terraform plan with the -no-color option then it will skip the terminal escape sequences and produce plain-text output that you could include in a preformatted text block in your Markdown comment. However, that output will therefore not include the text formatting you normally see in your terminal.

如果您愿意编写一些自定义格式代码来为您的 GitHub 评论以不同格式呈现 Terraform 计划,您可以通过将计划保存到磁盘然后使用 读取来获取计划的 JSON 表示地形展示:

If you are willing to write some custom formatting code to present the Terraform plan in a different format for your GitHub comments, you can obtain a JSON representation of the plan by saving the plan to disk and then reading it with terraform show:

terraform plan -out=tfplan
terraform show -json tfplan

这将生成 计划的 JSON 表示 您可以在自己设计的程序中解析并发出您想要的任何结果格式.然而,这将比仅仅从 Terraform 的正常输出解释终端转义序列要多得多,因为它是 Terraform 用于生成计划渲染的数据的 JSON 表示,而不是计划渲染本身.

This will produce a JSON representation of the plan that you could parse in a program of your own design and emit whatever result format you want. This will, however, be considerably more work than just interpreting the terminal escape sequences from Terraform's normal output, because it's a JSON representation of the data that Terraform uses to produce the plan rendering, not of the plan rendering itself.

这篇关于Terraform 在自动化中计划人类可读的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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