cloudstack之 添加指定IP实例

前言:

一句话来描述terraform话: 基于各类云的api实现管理基础设施的命令行工具html

官方原话: Terraform is a tool for building, changing, and combining infrastructure safely and efficiently.web

详细简介见:https://www.terraform.io/intro/index.htmlapi

1、安装terraform:

下载地址: https://www.terraform.io/downloads.html bash

   安装灰常简单: 下载对应系统的压缩包 解压 添加环境变量 而后就OK啦。网络

   验证安装是否成功:app

$terraform --version
Terraform v0.5.3

‍2、配置provider‍

  首先建立一个目录 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啦

3、建立指定ip实例

   编辑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/ 

相关文章
相关标签/搜索