Packer 0.9の新機能 リモートからのAnsible Provisionerが追加されました

2016.02.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

藤本です。

昨日、Packerのバージョン0.9がリリースされました。Chef ProvisionerのWindows対応や、OVA形式でのAWSへのインポートなどいくつかの機能が追加されました。今回はその中でもAnsible Provisionerについてご紹介します。

リリースノートはこちら

概要

Packerには今までAnsibleのProvisionerとして、Ansible Local Provisionerがありました。Ansible Local ProvisionerはPlaybookをビルドOSに送信して、ローカルでAnsibleコマンドを実行するというものでした。事前にAnsibleをインストールしていなくてはならないことでAWSなどで標準OS AMIを利用する場合、Shell ProvisionerでゴニョゴニョとAnsible外で設定を入れえなくてはいけなかったり、RoleやVarsのパスを全て指定しなくてはいけなかったり、インストールされるAnsibleのバージョンを意識しなくてはいけなかったり、というちょっといただけない仕様でした。

Ansible Local Provisionerは弊社ブログでもPacker+AnsibleによるAMIの作成で紹介しています。

それが遂にPacker実行環境となるリモートからAnsibleによる設定が可能なProvisionerが追加されました。

ansible-probisioner

それでは早速試してみましょう。

環境

  • Packer実行環境
    • OSX
    • Packer : 0.9.0
  • Packerによるイメージ作成環境
    • 環境:AWS
    • AMI:Amazon Linux

インストール

インストールはOSXの場合、公式サイトからバイナリをダウンロードするか、Homebrewによりインストールできます。今回はHomebrewによりインストールしました。

# brew install packer

既にインストール済みの場合、update & upgradeによりパッケージをアップデートできます。

# brew update && brew upgrade

インストールを確認します。

# brew list packer
/usr/local/Cellar/packer/0.9.0/bin/packer

# packer version
Packer v0.9.0

バージョン0.9.0のPackerがインストールされました。

Packer+AnsibleでカスタムAMIを作ってみる

Ansible Playbook作成

今回は単純にhttpdをインストールだけのPlaybookを作成します。

# cat packer.yml
- hosts: all
  sudo: yes
  tasks:
    - yum: name=httpd state=installed

Packer Template作成

BuilderをAWS環境に、ProvisionerをAnsibleに指定し、Packerのテンプレートファイルを作成します。

# cat ansible-remote.template
{
  "builders" : [{
    "type" : "amazon-ebs",
    "region" : "ap-northeast-1",
    "instance_type" : "t2.nano",
    "source_ami" : "ami-59bdb937",
    "ssh_username" : "ec2-user",
    "ami_name" : "packer-ansible-remote"
  }],
  "provisioners" : [{
    "type" : "ansible",
    "playbook_file" : "packer.yml"
  }]
}

Packer実行

作成したテンプレートファイルを指定し、packer buildコマンドを実行します。

# packer build ansible-remote.template
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Inspecting the source AMI...
==> amazon-ebs: Creating temporary keypair: packer 56cc4dd5-729a-26b1-047f-8b1ba51868ff
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing access to port 22 the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
    amazon-ebs: Instance ID: i-07272ea2
==> amazon-ebs: Waiting for instance (i-07272ea2) to become ready...
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with Ansible...
==> amazon-ebs: SSH proxy: serving on 127.0.0.1:60999
==> amazon-ebs: Executing Ansible: ansible-playbook /Users/fujimoto.shinji/Techs/packer/ansible-remote/packer.yml -i /var/folders/vp/2b51_dy91r3b5bbjsv1lbj4c0000gq/T/packer-provisioner-ansible587654638 --private-key /var/folders/vp/2b51_dy91r3b5bbjsv1lbj4c0000gq/T/ansible-key955181579
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61000 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61000 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61001 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61001 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61002 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61002 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61003 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61003 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61004 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61004 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs: SSH proxy: accepted connection
==> amazon-ebs: authentication attempt from 127.0.0.1:61005 to 127.0.0.1:60999 as packer-ansible using none
==> amazon-ebs: authentication attempt from 127.0.0.1:61005 to 127.0.0.1:60999 as packer-ansible using publickey
    amazon-ebs:
    amazon-ebs: PLAY [all] ********************************************************************
    amazon-ebs:
    amazon-ebs: GATHERING FACTS ***************************************************************
    amazon-ebs: ok: [default]
    amazon-ebs:
    amazon-ebs: TASK: [yum name=httpd state=installed] ****************************************
    amazon-ebs: changed: [default]
    amazon-ebs:
    amazon-ebs: PLAY RECAP ********************************************************************
    amazon-ebs: default                    : ok=2    changed=1    unreachable=0    failed=0
    amazon-ebs:
==> amazon-ebs: shutting down the SSH proxy
==> amazon-ebs: Stopping the source instance...
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating the AMI: packer-ansible-remote
    amazon-ebs: AMI: ami-4d5e5d23
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:

ap-northeast-1: ami-4d5e5d23

Ansibleコマンドの実行結果が表示されているので、ansibleの実行結果も分かりやすくてよいですね。ちなみにAnsibleが失敗した場合、Packerコマンドも中断してくれます。

IDがami-4d5e5d23のAMIが作成されたようです。

動作確認

AMI作成状況

AMIを確認してみます。

EC2_Management_Console

IDがami-4d5e5d23のAMIが作成されています。

インスタンスの設定確認

作成されたAMIからEC2インスタンスを作成しましょう。AMIを選択し、「作成」を押下します。

EC2_Management_Console

ポチポチとウィザードを進めて、作成を開始します。

EC2_Management_Console

AMI作成元として作成されたインスタンスがterminated状態となっていますね。

作成されたEC2インスタンスにSSHログインし、httpdのインストール状況を確認します。

# ssh 52.xxx.xxx.xxx
The authenticity of host '52.xxx.xxx.xxx (52.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 00:b2:5d:97:33:7e:72:a2:3a:65:f7:cc:bf:56:6f:d5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '52.xxx.xxx.xxx' (RSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/

[ec2-user@ip-172-31-25-157 ~]$ rpm -q httpd
httpd-2.2.31-1.6.amzn1.x86_64

はい、インストールされていますね。

まとめ

いかがでしたでしょうか。
Hashicorp製品は様々な環境、様々な製品と連携してくれるのが嬉しいところです。