以前写过一个使用模版引擎+ 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 = "\nprovider \"gitlab\" {\n base_url = \"http://gitserver/api/v4/\"\n token = \"xxxxxx\"\n}\n\nresource \"gitlab_group
\" \"firstrong\" {\n name = \"firstrong\"\n path = \"firstrong\"\n description = \"firstrong\"\n}\nresource \"gitlab_group\" \"second
rong\" {\n name = \"secondrong\"\n path = \"secondrong\"\n description = \"secondrong\"\n}\nresource \"gitlab_group\" \"thirdrong\" {
\n name = \"thirdrong\"\n path = \"thirdrong\"\n description = \"thirdrong\"\n}\n\nresource \"gitlab_group\" \"firstrong\" {\n nam
e = \"firstrong\"\n path = \"firstrong\"\n description = \"firstrong\"\n}\nresource \"gitlab_group\" \"secondrong\" {\n name = \"seco
ndrong\"\n path = \"secondrong\"\n description = \"secondrong\"\n}\nresource \"gitlab_group\" \"thirdrong\" {\n name = \"thirdrong\"\
n path = \"thirdrong\"\n description = \"thirdrong\"\n}\n\nresource \"gitlab_group\" \"demoapp\" {\n name = \"demoapp\"\n path = \
"demoapp\"\n parent_id = \"${gitlab_group.firstrong.id}\"\n}\nresource \"gitlab_group\" \"demoapp2\" {\n name = \"demoapp2\"\n path =
\"demoapp2\"\n parent_id = \"${gitlab_group.secondrong.id}\"\n}\nresource \"gitlab_group\" \"demoapp3\" {\n name = \"demoapp3\"\n pa
th = \"demoapp3\"\n parent_id = \"${gitlab_group.thirdrong.id}\"\n}\n"
+ 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

 
 
 
 

使用terraform v0.12 生成gitlab repo 创建部署tf 文件的更多相关文章

  1. sqler 集成 terraform v0.12 生成资源部署文件

    terraform v0.12 发布了,有好多新功能的添加,包括语法的增强,新函数的引入,更好的开发提示 只是当前对于一些老版本的provider 暂时还不兼容,但是大部分官方的provider 都是 ...

  2. terraform v0.12.0 发布了

    v0.12.0 相比以前的有好多新的特性,包括语法,以及函数增强,昨天还在折腾的一个json解码的问题,直接使用 v0.12.0 就可以解决了,同时也包含了for 操作处理同时官方文档对于v0.12. ...

  3. Node.js V0.12 新特性之性能优化

    v0.12悠长的开发周期(已经过去九个月了,并且还在继续,是有史以来最长的一次)让核心团队和贡献者们有充分的机会对性能做一些优化. 本文会介绍其中最值得注意的几个. http://www.infoq. ...

  4. 【译】 Node.js v0.12的新特性 -- Cluster模式采用Round-Robin负载均衡

    原文:https://strongloop.com/strongblog/whats-new-in-node-js-v0-12-cluster-round-robin-load-balancing 本 ...

  5. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  6. Node.js V0.12新特性之性能优化

    v0.12悠长的开发周期(已经过去九个月了,并且还在继续,是有史以来最长的一次)让核心团队和贡献者们有充分的机会对性能做一些优化.本文会介绍其中最值得注意的几个. 支持塞住模式的可写流 现在可写流可以 ...

  7. Node v0.12.5 稳定版发布

    Node v0.12.5 稳定版发布了,该版本改进记录主要包括: openssl: upgrade to 1.0.1o (Addressing multiple CVEs) npm: upgrade ...

  8. 从GitLab上创建分支本地拉取项目和提交项目详解

    很多公司前端项目都是部署在GitLab上的,今天我要分享的就是如何从GitLab上创建新的分支并在本地拉取和提交项目 一.在GitLab上面创建自己新的分支 首先你得注册一个账号,登陆之后进入项目Pr ...

  9. GraphScope v0.12.0 版本发布

    GraphScope 每月进行常规版本的迭代与发布,GraphScope v0.12.0 全新版本在四月如期而至.v0.12.0 为交互式图查询 GAIA 引入全新的 IR 层以及新增 Giraph ...

随机推荐

  1. linux配置环境jdk

    条件:将jdk安装好,如果没有安装请看这里:linux(Centos7系统)中安装JDK.Tomcat.Mysql 步骤如下: linux中,环境变量是在 /etc/profile 中修改文件 vi ...

  2. ES6数组方法总结

    关于数组中forEach() .map().filter().reduce().some().every()的总结 1. forEach() let array = [1,2,3,4]; array. ...

  3. ES6--Promise讲解

    相信凡是写过javascript的童鞋也一定都写过回调方法(callback),简单说回调方法就是将一个方法func2作为参数传入另一个方法func1中,当func1执行到某一步或者满足某种条件的时候 ...

  4. 2.熟悉LINUX的基本操作

    cd命令:切换目录 (1)切换到目录 /usr/local cd /usr/local (2)去到目前的上层目录 cd .. (3)回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4)查看目 ...

  5. MongoDB 基本概念

    MongoDB和关系型数据库的对应关系 关系数据库 MongoDB 数据库   database 数据库   database 表格  table 集合  collection 行  row 文档  ...

  6. Linq 等式运算符:SequenceEqual(转载)

    https://www.bbsmax.com/A/nAJvbKywJr/ 引用类型比较的是引用,需要自己实现IEqualityComparer 比较器. IList<string> str ...

  7. Spring IOC 总结

    IOC 简介 IOC是(Inversion of Control,控制反转)的简写.Spring提供IOC容器,将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的的过度程序耦合.它由DI( ...

  8. Java多线程 常见问题整理

    线程 什么是线程 线程是指程序在执行过程中,能够执行程序代码的一个执行单元. 线程和进程的区别 线程:一段程序执行过程中的一个执行单元,各个线程之间共享程序的内存空间以及一些进程级的资源,各线程拥有自 ...

  9. Spring Boot 默认支持的并发量

    Spring Boot应用支持的最大并发量是多少? Spring Boot 能支持的最大并发量主要看其对Tomcat的设置,可以在配置文件中对其进行更改.当在配置文件中敲出max后提示值就是它的默认值 ...

  10. Springboot项目中Pom.xml报错

    摘要:使用idea,两次在maven上浪费时间,第一次不知道怎么就解决了,第二次记录一下解决办法 参考博客地址: https://blog.csdn.net/u013129944/article/de ...