Packer で IDCFクラウドに仮想マシンテンプレートを作成する

# 概要
いつの間にか cloudstack builder が support されていたので IDCFクラウドに仮想マシンテンプレートを作成。

# 環境

- Packer v1.0.0


# IDCFクラウド側の事前準備

- エンドポイント URL, API_KEY, SECRET_KEY 取得
- ソースのパブリック IP アドレス ID を取得※
- Keypair 登録

※packer にて temporary で public IP address を取得可能ですが、IDCFクラウドにおいては以下のエラーが出るため、デフォルトでついてくるソース IP アドレスを利用

```
==> cloudstack: Failed to associate public IP address: CloudStack API error 431 (CSExceptionErrorCode: 4350): Unable to figure out zone to assign ip to
```

# template 作成

```json:template.json
{
  "variables": {
    "api_url": "https://compute.jp-east.idcfcloud.com/client/api",
    "api_key": "YOUR_API_KEY",
    "secret_key": "YOUR_SECRET_KEY",
    "ip_address_id" : "SOURCE_IP_ADDRESS_ID"
  },


  "builders": [
    {
      "type": "cloudstack",
      "communicator": "ssh",
      "api_url": "{{user `api_url`}}",
      "api_key": "{{user `api_key`}}",
      "secret_key": "{{user `secret_key`}}",
      "http_get_only": true,
      "public_ip_address": "{{user `ip_address_id`}}",
      "use_local_ip_address": false,

      "cidr_list": ["0.0.0.0/0"],
      "hypervisor": "VMware",
      "network": "newton-network1",
      "service_offering": "light.S1",
      "source_template": "CentOS 7.3 64-bit",
      "zone": "newton",
      "keypair": "YOUR_KEYPAIR_NAME",
      "ssh_username": "root",
      "ssh_private_key_file": "/path/to/private_key_file",
      "ssh_timeout": "15m",

      "template_name": "Template-{{isotime \"2006-01-02\"}}",
      "template_display_text": "{{isotime \"2006-01-02\"}}",
      "template_featured": true,
      "template_password_enabled": false,
      "template_scalable": true,
      "template_os": "Other CentOS (64-bit)"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sudo yum -y install epel-release",
        "sudo yum -y install gcc openssl-devel libffi-devel python-devel python-crypto python-pip",
        "sudo yum -y update",
        "sudo pip install ansible",
        "sudo pip install -U pip",
        "sudo pip install -U setuptools"
      ]
    },
    {
      "type": "file",
      "source": "playbook/files",
      "destination": "/tmp/packer-provisioner-ansible-local/"
    },
    {
      "type": "file",
      "source": "playbook/templates",
      "destination": "/tmp/packer-provisioner-ansible-local/"
    },
    {
      "type": "ansible-local",
      "playbook_file": "playbook/provision.yml"
    }
  ]
}

```

**構築環境**

- region: 東日本リージョン
- zone: newton
- OS: CentOS 7.3 64-bit
- マシンタイプ: light.S1

**構築内容**
provisioner で Ansible インストールまでして、その他諸々は ansible-local でプロビジョニングする。


**ディレクトリ構成**

```
root/
　├ template.json
　└ playbook/
　   ├ provision.yml
　   ├ files/
   　└ templates/
```

# 仮想マシンテンプレート作成

ビルド実行

```
$ packer build template.json
```

実行結果

```
==> cloudstack: Preparing config...
==> cloudstack: Creating instance...
==> cloudstack: Setup networking...
==> cloudstack: Waiting for SSH to become available...
==> cloudstack: Connected to SSH!
==> cloudstack: Provisioning with shell script: /tmp/packer-shell086017542
==> cloudstack: Uploading playbook/files => /tmp/packer-provisioner-ansible-local/
==> cloudstack: Uploading playbook/templates => /tmp/packer-provisioner-ansible-local/
==> cloudstack: Provisioning with Ansible...
==> cloudstack: Shutting down instance...
==> cloudstack: Creating template: Template-2017-05-15
==> cloudstack: Cleanup networking...
==> cloudstack: Deleting instance...
Build 'cloudstack' finished.

==> Builds finished. The artifacts of successful builds are:
--> cloudstack: A template was created: Template-2017-05-15
```