TerraForm开发工具,用于提取变量并生成variables.tf文件。
scratchrelaxtv的Python项目详细描述
地形模块开发工具。
- 从
main.tf
中提取变量并生成variables.tf
文件 - 根据彼此查找
variables.tf
和main.tf
中缺少的变量 - 从
variables.tf
文件生成模块使用存根 - 使用
main.tf
中的变量生成.env文件
- 删除多余的scratchrelaxtv文件
安装
pip install scratchrelaxtv
提示
安装后,您可以通过键入relaxtv
或scratchrelaxtv
来运行scratchrelaxtv。
了解更多关于terraform工作流的信息,并使用scratchrelaxtvhere。
工作流
下面是使用scratchrelaxtv的两个示例工作流。
原始模块开发:
- 用任何需要的变量编写
main.tf
- 运行scratchrelaxtv生成
variables.tf
- 在
variables.tf
中填写描述、默认值等
- 运行
terraform fmt
以美化所有内容
清除模块:
- 在带有
main.tf
和variables.tf
的文件夹中运行scratchrelaxtv以查找丢失的变量 - 使用
-cf
选项,自动将缺少的变量添加到variables.tf
- 在
variables.tf
中为新添加的变量填写描述、默认值等 - 运行
terraform fmt
以美化所有内容
示例
示例:生成variables.tf
默认情况下,scratchrelaxtv查找main.tf
,并生成一个variables.tf
文件。变量在variables.tf
中的顺序与在main.tf
中的顺序相同。有对变量排序的选项。可以改写现有的^ {< CD2> }文件^ {CD24}}。否则,scratchrelaxtv将在每次运行时生成新的variables.tf
文件:variables.1.tf
,variables.2.tf
等等。
假设这个main.tf
:
resource "aws_s3_bucket" "this" { count = "${var.create_bucket ? 1 : 0}" bucket = "${var.bucket}" region = "${var.region}" }
运行scratchrelaxtv:
$ relaxtv 2019-04-26 08:02:54,011 - INFO - generating variables file2019-04-26 08:02:54,011 - INFO - input file: main.tf2019-04-26 08:02:54,011 - INFO - output file: variables.tf2019-04-26 08:02:54,011 - INFO - not forcing overwrite of output file2019-04-26 08:02:54,011 - INFO - not ordering output file
生成的variables.tf
:
variable "create_bucket" { description = "" type = "string" default = "" } variable "bucket" { description = "" type = "string" default = "" } variable "region" { description = "" type = "string" default = "" }
示例:查找并修复丢失的变量
假设您已经有一个main.tf
和一个variables.tf
。在本例中,variables.tf
缺少region
变量。
main.tf
:
resource "aws_s3_bucket" "this" { bucket = "${var.bucket}" region = "${var.region}" }
variables.tf
:
variable "bucket" { description = "The bucket where the stuff will be stored" type = "string" default = "" }
运行scratchrelaxtv自动添加任何缺少的变量:
$ relaxtv -cf 2019-04-26 08:21:27,289 - INFO - checking for missing variables2019-04-26 08:21:27,289 - INFO - input file: main.tf2019-04-26 08:21:27,289 - INFO - output file: variables.tf2019-04-26 08:21:27,289 - INFO - forcing overwrite of output file2019-04-26 08:21:27,289 - INFO - not ordering output file2019-04-26 08:21:27,290 - WARNING - input file main.tf is missing variables:region
现在,variables.tf
看起来像这样:
variable "bucket" { description = "The bucket where the stuff will be stored" type = "string" default = "" } variable "region" { description = "" type = "string" default = "" }
示例:生成使用模块的存根
默认情况下,生成存根时,scratchrelaxtv查找variables.tf
。
假设这个variables.tf
:
variable "id" { description = "The ID of the resource" type = "string" default = "" } variable "bucket" { description = "The bucket where the stuff will be stored" type = "string" default = "" } variable "region" { description = "The AWS region where the bucket lives" type = "string" default = "" }
使用模块存根选项运行scratchrelaxtv
$ relaxtv -m 2019-04-26 08:09:27,147 - INFO - generating module usage stub2019-04-26 08:09:27,147 - INFO - input file: variables.tf2019-04-26 08:09:27,147 - INFO - output file: modstub.tf2019-04-26 08:09:27,147 - INFO - not forcing overwrite of output file2019-04-26 08:09:27,147 - INFO - not ordering output file
生成的modstub.tf
:
module "tests2" { source = "../tests2" providers = { aws = "aws" } id = "${local.id}" bucket = "${local.bucket}" region = "${local.region}" }
示例:生成.env
(dotenv)文件
默认情况下,生成.env
文件时,scratchrelaxtv查找variables.tf
。
假设这个variables.tf
:
resource "aws_s3_bucket" "this" { bucket = "${var.bucket}" region = "${var.region}" }
使用generate.env
运行scratchrelaxtv并对升序选项进行排序:
$ relaxtv -ea 2019-06-21 20:01:35,362 - INFO - generating .env file2019-06-21 20:01:35,362 - INFO - input file: main.tf2019-06-21 20:01:35,362 - INFO - output file: .env2019-06-21 20:01:35,362 - INFO - not forcing overwrite of output file2019-06-21 20:01:35,362 - INFO - ordering output file ascending
生成的.env
:
unset"${!TF_VAR_@}"TF_VAR_bucket=replace TF_VAR_region=replace
示例:删除文件
$ relaxtv -r
scratchrelaxtv还可以通过删除自己额外生成的文件来整理目录。可能它只会删除您不再需要的文件,但是请小心。此图表显示了哪些内容将被删除或不删除的示例。
note:scratchrelaxtv删除当前目录和子目录中的文件。
Filename | Deleted? |
---|---|
variables.tf | no |
modstub.tf | yes |
modstub.1.tf | yes |
variables.1.tf | yes |
xyz.abc | no |
variables.a.tf | no |
variables.43.tf | yes |
modstub | no |
modstub..tf | no |
帮助
scratchrelaxtv包括帮助:
$ relaxtv --help usage: scratchrelaxtv [-h] [-i INPUT] [-o OUTPUT] [-f] [-m] [-n MODNAME] [-r] [-c] [-e] [-a | -d]optional arguments: -h, --help show this help message and exit -i INPUT, --input INPUT file to extract vars from -o OUTPUT, --output OUTPUT file to write extracted vars to -f, --force overwrite existing out file -m, --modstub generate module usage stub -n MODNAME, --modname MODNAME name to use in module stub -r, --remove remove all modstub.tf, variables.#.tf files -c, --check check that all vars are listed -e, --env generate .env with Terraform vars -a, --asc sort output variables in ascending order -d, --desc sort output variables in descending order
更改日志
0.5.2-2019.07.19
- 添加与HCl2/Terraform 0.12.x变量(不含^{
})的兼容性 - 删除功能以自动将列表默认值设置为空列表(对HCl2样式的变量不起作用)
0.5.0-2019.07.02
- 添加功能以生成地形变量(
.tfvars
)文件
0.4.0至2019.06.21
- 添加功能以生成点环境(
.env
)文件
0.3.0-2019.04.25
0.2.1-2019.04.25
- 修复删除文件时引发的无关紧要的错误
0.2.0-2019.04.25
- 添加删除额外工作文件的功能
0.1.3-2019.04.17
- 添加模块存根功能
0.1.2-2019.04.17
- 添加简单文档
0.1.1-2019.04.16
- 初始工作版本
0.1.0-2019.04.15
- 初次发布!