创建一个简单的terraform module
terraform module可以实现代码的复用,同时方便分享,下面创建一个简单的基于localfile && template provider 的module
module 项目
一个简单基于模板生成curl 配置module
- 项目结构
├── README.md
├── init.tpl
├── main.tf
├── outputs.tf
└── variables.tf
- 代码说明
main.tf:
使用template provider 生成文件
data "template_file" "init" {
template = "${file("init.tpl")}"
vars = {
name = "${var.username}"
age = 444
consul_host = "${var.consul_host}"
platform ="mobile"
}
}
variables.tf:
变量定义
variable "username" {
type = "string"
description = "this is user default name"
default = "dalong"
}
variable "consul_host" {
type = "string"
description = "consul host "
default = "http://localhost:8500"
}
outputs.tf:
输出定义
output "exec_shell" {
value = "${data.template_file.init.rendered}"
}
tpl 模板格式:
#!/bin/bash
curl -X POST \
${consul_host} \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"name":"${name}",
"age": "${age}",
"platform":"${platform}",
"id":"${uuid()}"
}'
引用module
为了简单使用本地source,和module 在一个目录中
- 项目结构
├── README.md
├── init.sh
├── init.tpl
├── main.tf
├── modules
│ └── users
│ ├── README.md
│ ├── init.tpl
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
- 代码说明
main.tf :
引用module, 以及使用module 的输出变量
module "users" {
source = "./modules/users"
username = "dddddemo"
consul_host ="http://127.0.0.1:8500"
}
resource "local_file" "foo" {
content = "${module.users.exec_shell}"
filename = "${path.module}/init.sh"
}
init.tpl: 模板内容
#!/bin/bash
curl -X POST \
${consul_host} \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"name":"${name}",
"age": "${age}",
"platform":"${platform}",
"id":"${uuid()}"
}'
使用
- get module
terraform get
- init 下载plugin
terraform init
效果
Initializing modules...
- module.users
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.local: version = "~> 1.2"
* provider.template: version = "~> 2.1"
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.
- 查看plan
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.template_file.init: Refreshing state...
local_file.foo: Refreshing state... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
-/+ local_file.foo (new resource required)
id: "785957ddadd6fe919ea5644ff2096066536e186b" => <computed> (forces new resource)
content: "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"d288df44-c939-1ca3-0bb6-b8998b9305ca\"\n}'" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"204da291-b163-9f37-b719-e304c351ccbd\"\n}'" (forces new resource)
filename: "/Users/dalong/mylearning/t-mode/init.sh" => "/Users/dalong/mylearning/t-mode/init.sh"
Plan: 1 to add, 0 to change, 1 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
- apply
terraform apply
效果
data.template_file.init: Refreshing state...
local_file.foo: Refreshing state... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
-/+ local_file.foo (new resource required)
id: "785957ddadd6fe919ea5644ff2096066536e186b" => <computed> (forces new resource)
content: "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"d288df44-c939-1ca3-0bb6-b8998b9305ca\"\n}'" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"a2f79788-011b-0bd8-ad21-51ce9604a42a\"\n}'" (forces new resource)
filename: "/Users/dalong/mylearning/t-mode/init.sh" => "/Users/dalong/mylearning/t-mode/init.sh"
Plan: 1 to add, 0 to change, 1 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.foo: Destroying... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)
local_file.foo: Destruction complete after 0s
local_file.foo: Creating...
content: "" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"a2f79788-011b-0bd8-ad21-51ce9604a42a\"\n}'"
filename: "" => "/Users/dalong/mylearning/t-mode/init.sh"
local_file.foo: Creation complete after 0s (ID: 18917f440b6efa1ab11ae1c4ad1bde51bba37b85)
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- 生成的curl 命令
#!/bin/bash
curl -X POST \
http://127.0.0.1:8500 \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"name":"dddddemo",
"age": "444",
"platform":"mobile",
"id":"a2f79788-011b-0bd8-ad21-51ce9604a42a"
}'
说明
创建的module 很简单,但是包好了基本的创建以及使用流程,同时最好的实践是通过源代码管理进行module 的管理(git)
参考资料
https://github.com/rongfengliang/terraform-module-demo
https://www.terraform.io/docs/modules/index.html
https://www.terraform.io/docs/configuration/modules.html
创建一个简单的terraform module的更多相关文章
- 用Eclipse 创建一个 简单的 Maven JavaWeb 项目
使用Maven 创建一个简单的 javaWeb 项目: 本篇属于 创建 JavaWeb 项目的第三篇: 建议阅读本篇之前 阅读 用 Eclipse 创建一个简单的web项目 ;本篇是这这篇文章的基础 ...
- 用 Eclipse 创建一个简单的web项目
Eclipse neon 汉化版 ; 1;右击新建 --> 选择 动态Web项目 2: 填写 项目名 项目位置 ; 选择 Dynamic web module version 和 tomca ...
- 一个先进的App框架:使用Ionic创建一个简单的APP
原文 http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...
- 通过创建一个简单的骰子游戏来探究 Python
在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示如何使用 Python 模块 Pygame 来创建一个图形化游戏.它将需要几篇文章才 ...
- 如何创建一个简单的Visual Studio Code扩展
注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一款轻量级的代码编辑器,免费,开源,支持多种 ...
- 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型
第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...
- 如何创建一个简单的C++同步锁框架(译)
翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...
- Windows 8.1 应用再出发 (WinJS) - 创建一个简单项目
前面几篇我们介绍了如何利用 C# + XAML 完成Windows Store App 功能的实现,接下来的几篇我们来看看如何利用 Html + WinJS 来完成这些功能. 本篇我们使用WinJS ...
- ADF_General JSF系列1_创建一个简单的JSF Application
2015-02-17 Creatd By BaoXinjian
随机推荐
- SignalR NuGet程序包
最近公司有一个边看直播边聊天的需求,直播好搞,直接用腾讯的小直播,组装推流和播放地址,把推流地址拿出去就OK,只要一推流,就可以使用播放地址观看直播,看完后通过webclient去异步下载直播的视频到 ...
- Java获取系统时间的四种方法
1.Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ...
- JSONP解决跨域问题,什么是JSONP(转)
原文链接:https://www.cnblogs.com/xinxingyu/p/6075881.html 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的 ...
- Linux:配置samba服务
配置samba服务 一.简略教程 1.挂载系统 mount /dev/cdrom /mnt/cdrom2.创建用户:useradd linlin3.创建用户密码:passwd linlin4.在用户 ...
- 关于lunece的搜索的分页和多字段搜索关键词
关于全文检索lunece的分页,我们需要用到的是以下方法 IndexSearch类下的searchAfter方法. IndexSearch isearch=new IndexSearch(a); is ...
- 爬虫系列3:scrapy技术进阶(xpath、rules、shell等)
本文主要介绍与scrapy应用紧密相关的关键技术,不求很深入,但求能够提取要点.内容包括: 1.xpath选择器:选择页面中想要的内容 2.rules规则:定义爬虫要爬取的域 3.scrapy she ...
- 2018年3月最新的Ubuntu 16.04.4漏洞提权代码
2018年3月最新的Ubuntu 16.04.4漏洞提权代码,本代码取自Vitaly Nikolenko的推子 亲测阿里云提权可用. /* * Ubuntu 16.04.4 kernel priv e ...
- centos7 安装mysql--python模块出现EnvironmentError: mysql_config not found和error: command 'gcc' failed with exit status 1
要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块. 下载地址:https://pypi.python.org/pypi/MySQ ...
- The repository 'http://cdn.debian.net/debian stretch Release' is not signed.
/********************************************************************************* * The repository ...
- 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较
由于红外补光灯的爆闪,所以一般DMS会用global shutter的sensor,而不是rolling shutter的. 参考 1. Global Shutter(全局快门)与Rolling Sh ...