一句話來描述terraform話: 基於各類雲的api實現管理基礎設施的命令行工具html
官方原話: Terraform is a tool for building, changing, and combining infrastructure safely and efficiently.web
詳細簡介見:https://www.terraform.io/intro/index.htmlapi
下載地址: https://www.terraform.io/downloads.html bash
安裝灰常簡單: 下載對應系統的壓縮包 解壓 添加環境變量 而後就OK啦。網絡
驗證安裝是否成功:app
$terraform --version Terraform v0.5.3
首先建立一個目錄 ide
注意: 後面的操做都是在這個目錄中完成的工具
$mkdir cs; cd cs
建立provider變量文件(不須要更改)ui
$cat >variables.tf<< EOF variable "cloudstack_api_url" {} variable "cloudstack_api_key" {} variable "cloudstack_secret_key" {} EOF
在terraform.tfvars 填寫cloudstack api地址及key this
key生成見下圖:
點擊①會生成②
注意: 將其中url key 等替換爲你的
$cat >terraform.tfvars <<EOF 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" EOF
配置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/