从shell/bash脚本生成平台变量文件

hjqgdpho  于 2022-12-13  发布在  Shell
关注(0)|答案(2)|浏览(194)

我的需求是基于环境生成terraform变量文件,我尝试使用bash/shell脚本生成它们,但难以将输出转换为terraform HCL语言(我不能使用JSON,因为我将在terraform模块中进一步操作它们)
当前API输出(需要转换为HCL):

'cluster_name1' 'REGION1' 'Volume_Size1' 'Instance_Size1'
'cluster_name2' 'REGION2' 'Volume_Size2' 'Instance_Size2'
'cluster_name3' 'REGION3' 'Volume_Size3' 'Instance_Size3'
{...}

以CSV格式输出:

"cluster_name1","REGION1","Volume_Size1","Instance_Size1"
"cluster_name2","REGION2","Volume_Size2","Instance_Size2"
"cluster_name3","REGION3","Volume_Size3","Instance_Size3"
{...}

所需格式:

variable "cluster_configuration" {
    default = [
    {   
        "cluster_name" : "cluster_name1"
        "cluster_region" : "REGION1"
        "instance_db_size" : "Volume_Size1"
        "instance_size" : "Instance_Size1"
    },
    {
        "cluster_name" : "cluster_name2"
        "cluster_region" : "REGION2"
        "instance_db_size" : "Volume_Size2"
        "instance_size" : "Instance_Size2"
    },
    {
        "cluster_name" : "cluster_name3"
        "cluster_region" : "REGION3"
        "instance_db_size" : "Volume_Size3"
        "instance_size" : "Instance_Size3"
    },
    {....}
    ]
}

我的Terraform代码,仅供参考:

locals {
  dbconfig = [
    for db in var.cluster_configuration : [{
        instance_name = db.cluster_name
        db_size = db.instance_db_size
        instance_size = db.instance_size
        cluster_region = db.cluster_region
      }
    ]
  ]
}

我已经尝试了AWK和SED,但到目前为止没有运气。

2w3rbyxf

2w3rbyxf1#

Terraform可以处理JSON数据,如果你定义的对象正确的话。你可以使用jq把给定的CSV数据格式化成合适的JSON格式,让Terraform使用。如果我没记错的话,过滤器应该是这样的

# This *very* much assumes that the quotes aren't actually
# quoting fields that contain a real comma. jq is not suitable
# for robustly parsing CSV data.
[
  inputs |
     split(",") |
     map(ltrimstr("\"")) |
     map(rtrimstr("\"")) |
     {
        cluster_name: .[0],
        cluster_region: .[1],
        instance_db_size: .[2],
        instance_size: .[3]
     }
] | {variable: {cluster_configuration: {default: .}}}

(可能还有改进的空间。)假设您将其保存到类似api.jq的文件中,并且API输出的格式为output.csv,那么

$ jq -nRf api.jq output.csv
{
  "variable": {
    "cluster_configuration": {
      "default": [
        {
          "cluster_name": "cluster_name1",
          "cluster_region": "REGION1",
          "instance_db_size": "Volume_Size1",
          "instance_size": "Instance_Size1"
        },
        {
          "cluster_name": "cluster_name2",
          "cluster_region": "REGION2",
          "instance_db_size": "Volume_Size2",
          "instance_size": "Instance_Size2"
        },
        {
          "cluster_name": "cluster_name3",
          "cluster_region": "REGION3",
          "instance_db_size": "Volume_Size3",
          "instance_size": "Instance_Size3"
        }
      ]
    }
  }
}

但是,使用适当的CSV解析器选择语言来生成JSON可能会更简单。

xkftehaa

xkftehaa2#

使用jq的解决方案。
内容符合要求(但忽略指定格式)

INPUT="
'cluster_name1' 'REGION1' 'Volume_Size1' 'Instance_Size1'
'cluster_name2' 'REGION2' 'Volume_Size2' 'Instance_Size2'
'cluster_name3' 'REGION3' 'Volume_Size3' 'Instance_Size3'
"

jq -srR '
  split("\n") |        # split lines
  map(split(" ") |     # split fields
      select(any) |    # remove emty lines
      map(.[1:-1]) |   # remove enclosing quotes
      {
        cluster_name: .[0],
        cluster_region: .[1],
        instance_db_size: .[2],
        instance_size: .[3]
      }) |
  "variable \"cluster_configuration\" {",
  "    default = ",
  .,
  "}"
' <<< "$INPUT"

产出

variable "cluster_configuration" {
    default = 
[
  {
    "cluster_name": "cluster_name1",
    "cluster_region": "REGION1",
    "instance_db_size": "Volume_Size1",
    "instance_size": "Instance_Size1"
  },
  {
    "cluster_name": "cluster_name2",
    "cluster_region": "REGION2",
    "instance_db_size": "Volume_Size2",
    "instance_size": "Instance_Size2"
  },
  {
    "cluster_name": "cluster_name3",
    "cluster_region": "REGION3",
    "instance_db_size": "Volume_Size3",
    "instance_size": "Instance_Size3"
  }
]
}

相关问题