zoukankan      html  css  js  c++  java
  • 使用terraform v0.12 生成gitlab repo 创建部署tf 文件

    以前写过一个使用模版引擎+ rest 接口的模式,生成tf 文件,v0.12 直接提供了方便的json 处理函数
    我们可以直接结合http 以及templatefile providers 方便的生成文件

    rest api 格式

    因为有一个数据格式的问题,在rest 接口中直接就暴露了parent 的tf 信息,后边在说明出有一个解决方法

    {
       "data": {
        "projects": [
            {
              "name": "firstrong",
              "path":"firstrong",
              "description":"firstrong",
              "group" : "firstrong"
            },
            {
               "name": "secondrong",
               "path":"secondrong",
               "description":"secondrong",
               "group": "secondrong"
            },
            {
                "name": "thirdrong",
                "path":"thirdrong",
                "description":"thirdrong",
                "group": "thirdrong"
            }
        ],
        "groups_parent":[
            {
              "name":"firstrong",
              "path":"firstrong",
              "description":"firstrong"
            },
            {
                "name":"secondrong",
                "path":"secondrong",
                 "description":"secondrong"
            },
            {
                "name":"thirdrong",
                "path":"thirdrong",
                "description":"thirdrong"
            }
        ],
        "subgroups":[
            {
              "name":"demoapp",
              "path":"demoapp",
              "parent":"${gitlab_group.firstrong.id}"
            },
            {
                "name":"demoapp2",
                "path":"demoapp2",
                "parent":"${gitlab_group.secondrong.id}"
            },
            {
                "name":"demoapp3",
                "path":"demoapp3",
                "parent":"${gitlab_group.thirdrong.id}"
            }
        ]
    
       } 
    }

    tf 配置

    • main.tf 文件
    data "http" "example" {
      url = "http://localhost:8080/app.json"
    }
    
    resource "local_file" "foo2" {
        content = "${templatefile("backends.tmpl", "${jsondecode(data.http.example.body)}")}"
        filename = "init2.sh"
    }
    • 模版文件
    provider "gitlab" {
        base_url = "http://gitserver/api/v4/"
        token = "xxxxxx"
    }
    
    %{ for item in data.projects ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        description = "${item.description}"
    }
    %{ endfor ~}
    
    %{ for item in data.groups_parent ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        description = "${item.description}"
    }
    %{ endfor ~}
    
    %{ for item in data.subgroups ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        parent_id = "${item.parent}"
    }
    %{ endfor ~}

    生成文件

    • init
     terraform init

    效果

    Initializing the backend...
    
    Initializing provider plugins...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.http: version = "~> 1.1"
    * provider.local: version = "~> 1.2"
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
    • pan
     terraform  plan

    效果

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    
    data.http.example: Refreshing state...
    local_file.foo2: Refreshing state... [id=98c83d090df2e6559c0225e00f2419dc4c554701]
    
    ------------------------------------------------------------------------
    
    No changes. Infrastructure is up-to-date.
    
    This means that Terraform did not detect any differences between your
    configuration and real physical resources that exist. As a result, no
    actions need to be performed.
    • apply
     terraform  apply

    效果

    data.http.example: Refreshing state...
    local_file.foo2: Refreshing state... [id=98c83d090df2e6559c0225e00f2419dc4c554701]
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # local_file.foo2 will be created
      + resource "local_file" "foo2" {
          + content = "
    provider "gitlab" {
     base_url = "http://gitserver/api/v4/"
     token = "xxxxxx"
    }
    
    resource "gitlab_group
    " "firstrong" {
     name = "firstrong"
     path = "firstrong"
     description = "firstrong"
    }
    resource "gitlab_group" "second
    rong" {
     name = "secondrong"
     path = "secondrong"
     description = "secondrong"
    }
    resource "gitlab_group" "thirdrong" {
    
     name = "thirdrong"
     path = "thirdrong"
     description = "thirdrong"
    }
    
    resource "gitlab_group" "firstrong" {
     nam
    e = "firstrong"
     path = "firstrong"
     description = "firstrong"
    }
    resource "gitlab_group" "secondrong" {
     name = "seco
    ndrong"
     path = "secondrong"
     description = "secondrong"
    }
    resource "gitlab_group" "thirdrong" {
     name = "thirdrong"
    n path = "thirdrong"
     description = "thirdrong"
    }
    
    resource "gitlab_group" "demoapp" {
     name = "demoapp"
     path = 
    "demoapp"
     parent_id = "${gitlab_group.firstrong.id}"
    }
    resource "gitlab_group" "demoapp2" {
     name = "demoapp2"
     path =
     "demoapp2"
     parent_id = "${gitlab_group.secondrong.id}"
    }
    resource "gitlab_group" "demoapp3" {
     name = "demoapp3"
     pa
    th = "demoapp3"
     parent_id = "${gitlab_group.thirdrong.id}"
    }
    "
          + filename = "init2.sh"
          + id = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    local_file.foo2: Creating...
    local_file.foo2: Creation complete after 0s [id=98c83d090df2e6559c0225e00f2419dc4c554701]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

    说明

    以上只是一个简单的v0.12 的试用,注意当前gitlab 插件不支持v0.12 ,后边估计会好吧,对于模版包含特殊字符的处理
    参考如下:

    provider "gitlab" {
        base_url = "http://gitserver/api/v4/"
        token = "xxxxxx"
    }
    
    %{ for item in data.projects ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        description = "${item.description}"
    }
    %{ endfor ~}
    
    %{ for item in data.groups_parent ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        description = "${item.description}"
    }
    %{ endfor ~}
    
    %{ for item in data.subgroups ~}
    resource "gitlab_group" "${item.name}" {
        name = "${item.name}"
        path = "${item.path}"
        parent_id = "$${gitlab_group.${item.parent}.id}"
    }
    %{ endfor ~}

    参考资料

    https://www.terraform.io/docs/configuration/functions/templatefile.html 
    https://www.terraform.io/docs/configuration/functions/jsondecode.html 
    https://www.terraform.io/docs/configuration/expressions.html#string-templates 
    https://github.com/rongfengliang/terraform-gtilab-demo.git

  • 相关阅读:
    级数问题
    放苹果
    _WIN32_WINNT not defined错误 解决办法
    日期大写
    金额大写转换
    选择屏幕字段不允许直接输入…
    OO面向对象ALV小测试
    判断是否有人在操作某张表,并获取…
    屏幕中设置焦点
    前导零
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/10911585.html
Copyright © 2011-2022 走看看