terraformでOpenstackのインスタンス作成を試してみた

ずっと試せていなかったterraformを試してみました。 Openstackの環境はdevstackなりで事前に作成してください。

terraformのインストール

さて、terraformのインストールですがbinary packageが提供されているので、PATHが通っている場所に配置するか自分でPATHを通して使ってください。

terraformの実行

terraformを実行してみます。

[komei@host01 ~]$ terraform
usage: terraform [--version] [--help] <command> [<args>]

Available commands are:
    apply       Builds or changes infrastructure
    destroy     Destroy Terraform-managed infrastructure
    get         Download and install modules for the configuration
    graph       Create a visual graph of Terraform resources
    init        Initializes Terraform configuration from a module
    output      Read an output from a state file
    plan        Generate and show an execution plan
    push        Upload this Terraform module to Atlas to run
    refresh     Update local state file against real resources
    remote      Configure remote state storage
    show        Inspect Terraform state or plan
    taint       Manually mark a resource for recreation
    validate    Validates the Terraform files
    version     Prints the Terraform version

[komei@host01 ~]$ terraform -version
Terraform v0.6.12

terraformの設定

一番シンプルな設定方法はmain.tfファイルに設定内容を記載していきます。 terraformにはwasやgapなど様々なresourceが用意されています。Openstackにも以下のように様々な操作対象のresourceが用意されています

  • block storage
  • compute
  • networking
  • firewall
  • object storage

※ 実際はこの中でさらに細分化されています。

テストインスタンスを作成するサンプル設定です。terraformはterraformの記法で設定を書きます。 ドキュメントを読めばわかりますが、networkとsecurity groupの記載は辞書・リストのように記載しますので注意してください。

resource "openstack_compute_instance_v2" "terraform-test-server" {
    name = test-server
    image_id = c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599
    flavor_id = 10
    network = {
       name = "v20"
    }
    availability_zone = "nova"
    key_pair = "komei_key"
    security_groups = ["accept_tcp22_in", "default"]

変数を扱うことも可能です。

variable "terraform-test-server_name" {}
variable "terraform-test-server_image_id" {}
variable "terraform-test-server_flavor_id" {}
variable "terraform-test-server_key_pair" {}
variable "terraform-test-server_availability_zone" {}
variable "terraform-test-server_network_name" {}

resource "openstack_compute_instance_v2" "terraform-test-server" {
    name = "${var.terraform-test-server_name}"
    image_id = "${var.terraform-test-server_image_id}"
    flavor_id = "${var.terraform-test-server_flavor_id}"
    network = {
       name = "${var.terraform-test-server_network_name}"
    }
    availability_zone = "${var.terraform-test-server_availability_zone}"
    key_pair = "${var.terraform-test-server_key_pair}"
    security_groups = ["accept_tcp22_in", "default"]

変数は以下のように値を代入するのですが、terraform.tfvarsという別ファイルに記載し読み込むことも可能です。

terraform-test-server_name = "test-server"
terraform-test-server_image_id = "c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599"
terraform-test-server_flavor_id = "10"
terraform-test-server_key_pair = "komei"
terraform-test-server_availability_zone = "nova"
terraform-test-server_network_name = "v20"

Openstackに必要な環境変数を含めて記載したのが以下です。remote-execを使って起動後にnginxをインストールして起動しています。 tfvarsは適宜設定してください。

[komei@host01 cia-network-terraform]$ more main.tf
variable "user_name" {}
variable "tenant_name" {}
variable "password" {}
variable "auth_url" {}

variable "terraform-test-server_name" {}
variable "terraform-test-server_image_id" {}
variable "terraform-test-server_flavor_id" {}
variable "terraform-test-server_key_pair" {}
variable "terraform-test-server_availability_zone" {}
variable "terraform-test-server_network_name" {}

variable "terraform-test-server_ssh_key_file" {}

# Configure the OpenStack Provider
provider "openstack" {
    user_name  = "${var.user_name}"
    tenant_name = "${var.tenant_name}"
    password  = "${var.password}"
    auth_url  = "${var.auth_url}"
}

resource "openstack_compute_instance_v2" "terraform-test-server" {
    name = "${var.terraform-test-server_name}"
    image_id = "${var.terraform-test-server_image_id}"
    flavor_id = "${var.terraform-test-server_flavor_id}"
    network = {
       name = "${var.terraform-test-server_network_name}"
    }
    availability_zone = "${var.terraform-test-server_availability_zone}"
    key_pair = "${var.terraform-test-server_key_pair}"
    security_groups = ["accept_tcp22_in", "default"]

    provisioner "remote-exec" {
        connection {
            type = "ssh"
            user = "centos"
            key_file = "${var.terraform-test-server_ssh_key_file}"
        }
        inline = [
            "sudo yum -y install nginx",
            "sudo service nginx start",
            "sudo chkconfig nginx on"
        ]
    }
}

terraformにはdry runもあります。

[komei@host01 ~]$ terraform plan
Refreshing Terraform state prior to plan...

openstack_compute_instance_v2.terraform-test-server: Refreshing state... (ID: e0e3c21b-7c48-8906-8bae-72bd22219b1a)

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.

-/+ openstack_compute_instance_v2.terraform-test-server
    access_ip_v4:               "" => "<computed>"
    access_ip_v6:               "" => "<computed>"
    availability_zone:          "" => "nova"
    flavor_id:                  "" => "10"
    flavor_name:                "" => "<computed>"
    image_id:                   "" => "c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599"
    image_name:                 "" => "<computed>"
    key_pair:                   "" => "komei_key"
    name:                       "" => "test-server"
    network.#:                  "" => "1"
    network.0.access_network:   "" => "0"
    network.0.fixed_ip_v4:      "" => "<computed>"
    network.0.fixed_ip_v6:      "" => "<computed>"
    network.0.floating_ip:      "" => "<computed>"
    network.0.mac:              "" => "<computed>"
    network.0.name:             "" => "v20"
    network.0.port:             "" => "<computed>"
    network.0.uuid:             "" => "<computed>"
    security_groups.#:          "" => "5"
    security_groups.1326803771: "" => "accept_tcp22_in"
    security_groups.3814588631: "" => "default"
    volume.#:                   "" => "<computed>"


Plan: 1 to add, 0 to change, 0 to destroy.

terraformを実行してみます。terraform apply を使用します。

[komei@host01 ~]$ terraform apply
openstack_compute_instance_v2.terraform-test-server: Refreshing state... (ID: e0e3c21b-7c48-8906-8bae-72bd22219b1a)
openstack_compute_instance_v2.terraform-test-server: Destroying...
openstack_compute_instance_v2.terraform-test-server: Creating...
  access_ip_v4:               "" => "<computed>"
  access_ip_v6:               "" => "<computed>"
  availability_zone:          "" => "nova"
  flavor_id:                  "" => "10"
  flavor_name:                "" => "<computed>"
  image_id:                   "" => "c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599"
  image_name:                 "" => "<computed>"
  key_pair:                   "" => "komei_key"
  name:                       "" => "test-server"
  network.#:                  "" => "1"
  network.0.access_network:   "" => "0"
  network.0.fixed_ip_v4:      "" => "<computed>"
  network.0.fixed_ip_v6:      "" => "<computed>"
  network.0.floating_ip:      "" => "<computed>"
  network.0.mac:              "" => "<computed>"
  network.0.name:             "" => "v20"
  network.0.port:             "" => "<computed>"
  network.0.uuid:             "" => "<computed>"
  security_groups.#:          "" => "5"
  security_groups.1326803771: "" => "accept_tcp_22"
  security_groups.3814588631: "" => "default"
  volume.#:                   "" => "<computed>"

openstack_compute_instance_v2.terraform-test-server: Provisioning with 'remote-exec'...
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connecting to remote host via SSH...
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Host: 192.168.10.11
openstack_compute_instance_v2.terraform-test-server (remote-exec):   User: centos
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Password: false
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Private key: true
openstack_compute_instance_v2.terraform-test-server (remote-exec):   SSH Agent: false
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connecting to remote host via SSH...
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Host: 192.168.10.11
openstack_compute_instance_v2.terraform-test-server (remote-exec):   User: centos
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Password: false
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Private key: true
openstack_compute_instance_v2.terraform-test-server (remote-exec):   SSH Agent: false
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connecting to remote host via SSH...
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Host: 192.168.10.11
openstack_compute_instance_v2.terraform-test-server (remote-exec):   User: centos
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Password: false
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Private key: true
openstack_compute_instance_v2.terraform-test-server (remote-exec):   SSH Agent: false
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connecting to remote host via SSH...
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Host: 192.168.10.11
openstack_compute_instance_v2.terraform-test-server (remote-exec):   User: centos
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Password: false
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Private key: true
openstack_compute_instance_v2.terraform-test-server (remote-exec):   SSH Agent: false
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connecting to remote host via SSH...
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Host: 192.168.10.11
openstack_compute_instance_v2.terraform-test-server (remote-exec):   User: centos
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Password: false
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Private key: true
openstack_compute_instance_v2.terraform-test-server (remote-exec):   SSH Agent: false
openstack_compute_instance_v2.terraform-test-server (remote-exec): Connected!
openstack_compute_instance_v2.terraform-test-server (remote-exec): Loaded plugins: fastestmirror
openstack_compute_instance_v2.terraform-test-server (remote-exec): base             | 3.6 kB     00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): centos-openstack | 2.9 kB     00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): epel             | 4.3 kB     00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): extras           | 3.4 kB     00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): updates          | 3.4 kB     00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (1/3): epel/x86_64 | 498 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (2/3): extras/7/x8 | 101 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (3/3): epel/x86_64 | 3.9 MB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): Loading mirror speeds from cached hostfile
openstack_compute_instance_v2.terraform-test-server (remote-exec): Resolving Dependencies
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package nginx.x86_64 1:1.6.3-8.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: nginx-filesystem = 1:1.6.3-8.el7 for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: gd for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: GeoIP for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libGeoIP.so.1()(64bit) for package: 1:nginx-1.6.3-8.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package GeoIP.x86_64 0:1.5.0-9.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: gd-2.0.35-26.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libpng15.so.15()(64bit) for package: gd-2.0.35-26.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package gperftools-libs.x86_64 0:2.4-7.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libunwind.so.8()(64bit) for package: gperftools-libs-2.4-7.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package nginx-filesystem.noarch 1:1.6.3-8.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package fontconfig.x86_64 0:2.10.95-7.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.10.95-7.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libX11.x86_64 0:1.6.3-2.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libX11-common >= 1.6.3-2.el7 for package: libX11-1.6.3-2.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.3-2.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libXpm.x86_64 0:3.5.11-3.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libunwind.x86_64 2:1.1-5.el7_2.2 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libX11-common.noarch 0:1.6.3-2.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libxcb.x86_64 0:1.11-4.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.11-4.el7.x86_64
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): ---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed
openstack_compute_instance_v2.terraform-test-server (remote-exec): --> Finished Dependency Resolution

openstack_compute_instance_v2.terraform-test-server (remote-exec): Dependencies Resolved

openstack_compute_instance_v2.terraform-test-server (remote-exec): ========================================
openstack_compute_instance_v2.terraform-test-server (remote-exec):  Package
openstack_compute_instance_v2.terraform-test-server (remote-exec):         Arch   Version       Repository
openstack_compute_instance_v2.terraform-test-server (remote-exec):                                    Size
openstack_compute_instance_v2.terraform-test-server (remote-exec): ========================================
openstack_compute_instance_v2.terraform-test-server (remote-exec): Installing:
openstack_compute_instance_v2.terraform-test-server (remote-exec):  nginx  x86_64 1:1.6.3-8.el7 epel 508 k
openstack_compute_instance_v2.terraform-test-server (remote-exec): Installing for dependencies:
openstack_compute_instance_v2.terraform-test-server (remote-exec):  GeoIP  x86_64 1.5.0-9.el7   base 709 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  fontconfig
openstack_compute_instance_v2.terraform-test-server (remote-exec):         x86_64 2.10.95-7.el7 base 228 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  fontpackages-filesystem
openstack_compute_instance_v2.terraform-test-server (remote-exec):         noarch 1.44-8.el7    base 9.9 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  gd     x86_64 2.0.35-26.el7 base 146 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  gperftools-libs
openstack_compute_instance_v2.terraform-test-server (remote-exec):         x86_64 2.4-7.el7     base 272 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libX11 x86_64 1.6.3-2.el7   base 605 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libX11-common
openstack_compute_instance_v2.terraform-test-server (remote-exec):         noarch 1.6.3-2.el7   base 162 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libXau x86_64 1.0.8-2.1.el7 base  29 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libXpm x86_64 3.5.11-3.el7  base  54 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libpng x86_64 2:1.5.13-7.el7_2
openstack_compute_instance_v2.terraform-test-server (remote-exec):                              updates
openstack_compute_instance_v2.terraform-test-server (remote-exec):                                   213 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libunwind
openstack_compute_instance_v2.terraform-test-server (remote-exec):         x86_64 2:1.1-5.el7_2.2
openstack_compute_instance_v2.terraform-test-server (remote-exec):                              updates
openstack_compute_instance_v2.terraform-test-server (remote-exec):                                    56 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libxcb x86_64 1.11-4.el7    base 189 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  libxslt
openstack_compute_instance_v2.terraform-test-server (remote-exec):         x86_64 1.1.28-5.el7  base 242 k
openstack_compute_instance_v2.terraform-test-server (remote-exec):  nginx-filesystem
openstack_compute_instance_v2.terraform-test-server (remote-exec):         noarch 1:1.6.3-8.el7 epel  15 k

openstack_compute_instance_v2.terraform-test-server (remote-exec): Transaction Summary
openstack_compute_instance_v2.terraform-test-server (remote-exec): ========================================
openstack_compute_instance_v2.terraform-test-server (remote-exec): Install  1 Package (+14 Dependent packages)

openstack_compute_instance_v2.terraform-test-server (remote-exec): Total download size: 3.4 M
openstack_compute_instance_v2.terraform-test-server (remote-exec): Installed size: 10 M
openstack_compute_instance_v2.terraform-test-server (remote-exec): Downloading packages:
openstack_compute_instance_v2.terraform-test-server (remote-exec): (1/15): fontconfig | 228 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (2/15): GeoIP-1.5. | 709 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (3/15): fontpackag | 9.9 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (4/15): gd-2.0.35- | 146 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (5/15): gperftools | 272 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (6/15): libX11-com | 162 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (7/15): libX11-1.6 | 605 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (8/15): libXau-1.0 |  29 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (9/15): libXpm-3.5 |  54 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (10/15): libxcb-1. | 189 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (11/15): nginx-fil |  15 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (12/15): libpng-1. | 213 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (13/15): libunwind |  56 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (14/15): libxslt-1 | 242 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): (15/15): nginx-1.6 | 508 kB   00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): ----------------------------------------
openstack_compute_instance_v2.terraform-test-server (remote-exec): Total      8.5 MB/s | 3.4 MB  00:00
openstack_compute_instance_v2.terraform-test-server (remote-exec): Running transaction check
openstack_compute_instance_v2.terraform-test-server (remote-exec): Running transaction test
openstack_compute_instance_v2.terraform-test-server (remote-exec): Transaction test succeeded
openstack_compute_instance_v2.terraform-test-server (remote-exec): Running transaction
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libun [        ]  1/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libun [####    ]  1/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libun [####### ]  1/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libunwind-1.1-    1/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [        ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [#       ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [##      ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [###     ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [####    ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [#####   ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [######  ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperfto [####### ]  2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gperftools-libs-    2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [        ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [#       ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [##      ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [###     ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [#####   ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpac [######  ]  3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontpackages-fil    3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [        ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [#       ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [##      ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [###     ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [####    ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [#####   ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [######  ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontcon [####### ]  4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : fontconfig-2.10.    4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [        ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [#       ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [##      ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [###     ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [####    ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [#####   ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [######  ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpn [####### ]  5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 2:libpng-1.5.13-    5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [        ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [#       ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [##      ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [####    ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [#####   ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [######  ]  6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx-filesyst    6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [        ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [#       ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [##      ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [###     ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [####    ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [#####   ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [######  ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt [####### ]  7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxslt-1.1.28-5    7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXau- [        ]  8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXau- [##      ]  8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXau- [######  ]  8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXau- [####### ]  8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXau-1.0.8-2.1    8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [        ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [#       ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [##      ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [###     ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [####    ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [#####   ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [######  ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb- [####### ]  9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libxcb-1.11-4.el    9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [        ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [#       ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [##      ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [###     ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [####    ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [#####   ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [######  ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1 [####### ] 10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : GeoIP-1.5.0-9.el   10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [        ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [#       ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [##      ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [###     ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [####    ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [#####   ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [######  ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [####### ] 11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11-common-1.   11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [        ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [#       ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [##      ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [###     ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [####    ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [#####   ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [######  ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11- [####### ] 12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libX11-1.6.3-2.e   12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXpm- [        ] 13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXpm- [####    ] 13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXpm- [#####   ] 13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXpm- [####### ] 13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : libXpm-3.5.11-3.   13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [        ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [#       ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [##      ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [###     ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [####    ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [#####   ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [######  ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0. [####### ] 14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : gd-2.0.35-26.el7   14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [        ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [#       ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [##      ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [###     ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [####    ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [#####   ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [######  ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx [####### ] 15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Installing : 1:nginx-1.6.3-8.   15/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libX11-common-1.    1/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : GeoIP-1.5.0-9.el    2/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libX11-1.6.3-2.e    3/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libXau-1.0.8-2.1    4/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libxslt-1.1.28-5    5/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libXpm-3.5.11-3.    6/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : fontconfig-2.10.    7/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : 1:nginx-1.6.3-8.    8/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : 1:nginx-filesyst    9/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : gd-2.0.35-26.el7   10/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : libxcb-1.11-4.el   11/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : 2:libpng-1.5.13-   12/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : gperftools-libs-   13/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : fontpackages-fil   14/15
openstack_compute_instance_v2.terraform-test-server (remote-exec):   Verifying  : 2:libunwind-1.1-   15/15

openstack_compute_instance_v2.terraform-test-server (remote-exec): Installed:
openstack_compute_instance_v2.terraform-test-server (remote-exec):   nginx.x86_64 1:1.6.3-8.el7

openstack_compute_instance_v2.terraform-test-server (remote-exec): Dependency Installed:
openstack_compute_instance_v2.terraform-test-server (remote-exec):   GeoIP.x86_64 0:1.5.0-9.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   fontconfig.x86_64 0:2.10.95-7.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   fontpackages-filesystem.noarch 0:1.44-8.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   gd.x86_64 0:2.0.35-26.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   gperftools-libs.x86_64 0:2.4-7.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libX11.x86_64 0:1.6.3-2.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libX11-common.noarch 0:1.6.3-2.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libXau.x86_64 0:1.0.8-2.1.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libXpm.x86_64 0:3.5.11-3.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libpng.x86_64 2:1.5.13-7.el7_2
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libunwind.x86_64 2:1.1-5.el7_2.2
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libxcb.x86_64 0:1.11-4.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   libxslt.x86_64 0:1.1.28-5.el7
openstack_compute_instance_v2.terraform-test-server (remote-exec):   nginx-filesystem.noarch 1:1.6.3-8.el7

openstack_compute_instance_v2.terraform-test-server (remote-exec): Complete!
openstack_compute_instance_v2.terraform-test-server (remote-exec): Redirecting to /bin/systemctl start  nginx.service
openstack_compute_instance_v2.terraform-test-server (remote-exec): Note: Forwarding request to 'systemctl enable nginx.service'.
openstack_compute_instance_v2.terraform-test-server (remote-exec): Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
openstack_compute_instance_v2.terraform-test-server: 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

terraform show コマンドで設定した内容を確認可能です。

[komei@host01 ~]$ terraform show
openstack_compute_instance_v2.terraform-test-server:
  id = 40659bf0-5eb1-455f-b9b0-94b4c7f1a20d
  access_ip_v4 = 192.168.10.11
  access_ip_v6 =
  availability_zone = nova
  flavor_id = 10
  flavor_name = small
  image_id = c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599
  image_name = centos-7.2.1511
  key_pair = komei_key
  metadata.# = 0
  name = test-server
  network.# = 1
  network.0.access_network = false
  network.0.fixed_ip_v4 = 192.168.10.11
  network.0.fixed_ip_v6 =
  network.0.floating_ip =
  network.0.mac = fa:16:3e:94:22:46
  network.0.name = v20
  network.0.port =
  network.0.uuid = 3ae4048e-f16d-4590-b25c-fb3ef169b145
  security_groups.# = 5
  security_groups.13268037741= accept_tcp22_in
  security_groups.3814588631 = default
  volume.# = 0

tfstateファイルにも記載があります。このファイルが競合するとやっかいなので今ではS3などにアップして共有する方法があるようです。

[komei@host01 ~]$ more terraform.tfstate
{
    "version": 1,
    "serial": 10,
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "openstack_compute_instance_v2.terraform-test-server": {
                    "type": "openstack_compute_instance_v2",
                    "primary": null,
                    "tainted": [
                        {
                            "id": "e0e3c21b-7c48-8906-8bae-72bd22219b1a",
                            "attributes": {
                                "availability_zone": "nova",
                                "flavor_id": "10",
                                "id": "e0e3c21b-7c48-8906-8bae-72bd22219b1a",
                                "image_id": "c35df0ed-f26a8-4s65-abfs8-2c7c1dd3f599",
                                "key_pair": "komei",
                                "name": "test-server",
                                "network.#": "1",
                                "network.0.access_network": "false",
                                "network.0.fixed_ip_v4": "",
                                "network.0.fixed_ip_v6": "",
                                "network.0.floating_ip": "",
                                "network.0.mac": "",
                                "network.0.name": "v20",
                                "network.0.port": "",
                                "network.0.uuid": "",
                                "security_groups.#": "5",
                                "security_groups.1326803771": "accept_tcp22_in",
                                "security_groups.3814588631": "default",
                                "volume.#": "0"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

nova show で インスタンスを確認すると正しくできているかと思います。 以下の通り削除も可能です。

[komei@host01 ~]$ terraform destroy -force
openstack_compute_instance_v2.terraform-test-server: Refreshing state... (ID: 40659bf0-5eb1-455f-b9b0-94b4c7f1a20d)
openstack_compute_instance_v2.terraform-test-server: Destroying...
openstack_compute_instance_v2.terraform-test-server: Destruction complete

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
[komei@host01 ~]$ terraform show

[komei@host01 ~]$ more terraform.tfstate
{
    "version": 1,
    "serial": 13,
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {}
        }
    ]
}

シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加

DevOpsを支えるHashiCorpツール大全 Think IT Books

DevOpsを支えるHashiCorpツール大全 Think IT Books

広告を非表示にする