Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash脚本编辑.tf变量文件_Bash_Terraform_Terraform Provider Aws_Terraform Provider Azure - Fatal编程技术网

使用bash脚本编辑.tf变量文件

使用bash脚本编辑.tf变量文件,bash,terraform,terraform-provider-aws,terraform-provider-azure,Bash,Terraform,Terraform Provider Aws,Terraform Provider Azure,我在.tf文件中有一个很大的变量文件,我想使用bash脚本更改值 像我的文件有很多这样的块,我想一步一步地改变每个块。改变terraform文件变量值的最佳实践是什么 variable "azure_spoke_gateways" { default = { spoke1 = { name = "name" size = "size" vpc

我在.tf文件中有一个很大的变量文件,我想使用bash脚本更改值

像我的文件有很多这样的块,我想一步一步地改变每个块。改变terraform文件变量值的最佳实践是什么

variable "azure_spoke_gateways" {
  default = {
    spoke1 = {
      name         = "name"
      size         = "size"
      vpc          = ""
    },
    spoke2 = {
      name         = "dummy"
      size         = "size2"
    }
  }
}

如果您将变量存储在中,我建议您使用JSON感知的东西,例如,而不是使用
sed
/
awk
等天真地修改JSON。这样您应该能够可靠地维护JSON格式


如果您需要维护原始格式(我理解为HCL),可以使用HCL解析器编写脚本,例如

如果您拥有该文件并且可以从头开始生成它,而不是编辑现有的文件,则可以使用以下方法

cat << EOF > main.tf.sh
variable "azure_spoke_gateways" {
  default = {
    spoke1 = {
      name    = "AZ-${region}-Spoke1-GW"
      size       = "Standard_B1ms"
      .. 
    }
  }
}
EOF
chmod +x main.tf.sh

export region=EU
. ./main.tf.sh > main.tf
cat main.tf.sh
变量“azure\u网关”{
默认值={
辐条1={
name=“AZ-${region}-Spoke1 GW”
size=“标准\u B1ms”
.. 
}
}
}
EOF
chmod+x main.tf.sh
出口地区=欧盟
. ./main.tf.sh>main.tf
虽然它仅限于某种场景,但非常简单明了。

使用GNU awk:

在继续之前设置变量

spke="spoke1" # The spoke we are concerned with
var="size" # The variable within the spoke section we are concerned with
val="size1" # The value we want to change to.
clp="azure" # Either azure or aws
使用-v将这些变量传递到GNU awk

awk -v spke="$spke" -v var="$var" -v val="$val" -v clp="$clp"'
      /variable/ { 
                                      cloudp=gensub(/(^variable[[:space:]]")(.*)(".*$)/,"\\2",$0) # Pull out the cloud provider
                 }
      /spoke[[:digit:]][[:space:]]=/ { 
                                        spoke=$1 # Track the spoke section
                                     } 
          spoke==spke && $1==var && cloudp ~ clp { # If spoke is equal to the passed spoke and the first space separated field is equal to the variable and clp is equal to the passed cloud provider (clp) we want to change (var)
                                        $0=gensub(/(^.*=[[:space:]]")(.*)(".*$)/,"\\1"val"\\3",$0) # Substitute the value for the value passed (val)
                                     }1' file
一班轮

awk -v spke="$spke" -v var="$var" -v val="$val" -v clp="$clp" '/variable/ { cloudp=gensub(/(^variable[[:space:]]")(.*)(".*$)/,"\\2",$0) } /spoke[[:digit:]][[:space:]]=/ { spoke=$1 } spoke==spke && $1==var && cloudp ~ clp { $0=gensub(/(^.*=[[:space:]]")(.*)(".*$)/,"\\1"val"\\3",$0) }1' file
如果您有最新版本的GNU awk,只需添加-i标志即可将更改提交到文件中,因此:

awk -i -v spke="$spke" -v var="$var" -v val="$val" -v clp="$clp" '/variable/ { cloudp=gensub(/(^variable[[:space:]]")(.*)(".*$)/,"\\2",$0) } /spoke[[:digit:]][[:space:]]=/ { spoke=$1 } spoke==spke && $1==var && cloudp ~ clp { $0=gensub(/(^.*=[[:space:]]")(.*)(".*$)/,"\\1"val"\\3",$0) }1' file
否则:

awk -v spke="$spke" -v var="$var" -v val="$val" -v clp="$clp" '/variable/ { cloudp=gensub(/(^variable[[:space:]]")(.*)(".*$)/,"\\2",$0) } /spoke[[:digit:]][[:space:]]=/ { spoke=$1 } spoke==spke && $1==var && cloudp ~ clp { $0=gensub(/(^.*=[[:space:]]")(.*)(".*$)/,"\\1"val"\\3",$0) }1' file > file.tmp && mv -f file.tmp file 

遗憾的是,这不是json,我已经6天在做研究了:(它的格式是
.ts
。如果我想更改
.tf
变量文件值,你知道吗?如果原始格式是HCL,我想你会找到各种不同脚本语言的解析器。如果没有,你可能可以在简单的情况下使用sed/awk。请给出
pyhcl
的一个小例子?我没有使用它,我承认谢谢你的回答,但我只能使用
bash脚本来编辑它。如果我运行awk命令2次,则只会应用最后一次更改。就像我先运行名称更改,然后再运行大小。并且只运行名称更改工作我只使用一行。您必须确保使用awk-I或w提交更改我使用
否则
选项,因为-I对我来说不是有效的命令。这很好。两种方法都可以。