一句话来描述terraform话: 基于各种云的api实现管理基础设施的命令行工具
官方原话: Terraform is a tool for building, changing, and combining infrastructure safely and efficiently.
详细简介见:https://www.terraform.io/intro/index.html
下载地址: https://www.terraform.io/downloads.html
安装灰常简单: 下载对应系统的压缩包 解压 添加环境变量 然后就OK啦。
验证安装是否成功:
$terraform --version Terraform v0.5.3
首先创建一个目录
注意: 后面的操作都是在这个目录中完成的
$mkdir cs; cd cs
创建provider变量文件(不需要更改)
$cat variables.tf variable "cloudstack_api_url" {} variable "cloudstack_api_key" {} variable "cloudstack_secret_key" {}
在terraform.tfvars 填写cloudstack api地址及key
key生成见下图:
点击①会生成②
注意: 将其中url key 等替换为你的
$cat terraform.tfvars cloudstack_api_url = "http://172.16.165.10:8080/client/api" cloudstack_api_key = "kSeGs1wiujx-xlIZJ1mTb2FjuM0wzPXyG0bTGdLzXqoBGjp3ErLTdIjM8BPNU6xZRB2WXKZj0QymVtmP-Ze_tw" cloudstack_secret_key = "oiz3g5xHxg9zW1lgRM7TUpH-C2nhZAX35uBwYslwKI_cNkiJ_ML-6LRPhHchpCpIezdxwk4Qydi7qWQ_2436Lg"
配置provider(不需要更改)
$cat cloudstack.tf # Configure the CloudStack Provider provider "cloudstack" { api_url = "${var.cloudstack_api_url}" api_key = "${var.cloudstack_api_key}" secret_key = "${var.cloudstack_secret_key}" }
三个配置文件都配置完成后、验证配置:
$terraform plan Refreshing Terraform state prior to plan... No changes. Infrastructure is up-to-date. This means that Terraform could not detect any differences between your configuration and the real physical resources that exist. As a result, Terraform doesn't need to do anything.
无报错就说明配置ok啦
编辑cloudstack.tf
追加如下内容:
## add instance resource "cloudstack_instance" "web" { name = "server-1" service_offering= "Small Instance" network = "test-network" template = "CentOS6.6" ipaddress = "10.1.1.2" zone = "zone1" }
注意: 将其中实例的模板、网络、IP等信息替换为你当前环境的, ip地址一定要在你的network的cidr 中哦!!
执行terraform plan
$terraform plan There are warnings and/or errors related to your configuration. Please fix these before continuing. Warnings: * cloudstack_template.CentOS6.6 : CentOS6.6 : resource name can only contain letters, numbers, dashes, and underscores. This will be an error in Terraform 0.4 No errors found. Continuing with 1 warning(s). Refreshing Terraform state prior to plan... The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + cloudstack_instance.web display_name: "" => "<computed>" expunge: "" => "0" ipaddress: "" => "10.1.1.2" name: "" => "server-1" network: "" => "test-network" service_offering: "" => "Small Instance" template: "" => "CentOS6.6" zone: "" => "zone1"
检查下实例信息是否正确。
执行terraform apply提交进行创建
$terraform apply There are warnings and/or errors related to your configuration. Please fix these before continuing. Warnings: * cloudstack_template.CentOS6.6 : CentOS6.6 : resource name can only contain letters, numbers, dashes, and underscores. This will be an error in Terraform 0.4 No errors found. Continuing with 1 warning(s). cloudstack_instance.web: Creating... display_name: "" => "<computed>" expunge: "" => "0" ipaddress: "" => "10.1.1.2" name: "" => "server-1" network: "" => "test-network" service_offering: "" => "Small Instance" template: "" => "CentOS6.6" zone: "" => "zone1" cloudstack_instance.web: Creation complete Apply complete! Resources: 1 added, 0 changed, 0 destroyed. The state of your infrastructure has been saved to the path below. This state is required to modify and destroy your infrastructure, so keep it safe. To inspect the complete state use the `terraform show` command. State path: terraform.tfstate
登录cloudstack界面中验证:
好了现在已经创建了一个指定ip的实例啦
附上terraform支持cloudstack的功能列表:
https://www.terraform.io/docs/providers/cloudstack/index.html
更多terraform高级功能详见官网: https://www.terraform.io/