この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
ども、大瀧です。
Vagrantを利用すると"検証環境の作っては消し"が簡単にできて非常に便利なのですが、仮想マシンを作るたびに毎回同じパッケージのインストールや設定ファイルの配置作業をするのは面倒ですよね。そこで今回は、構成管理ツールのAnsibleをVagrantに連携し、仮想マシンの作成から初期構成までをまとめてやってみたいと思います。
動作確認環境
- OS X Yosemite
- Vagrant 1.7.0
- Ansible 1.8.2(事前にMacにインストールしておく)
準備するもの
Ansibleの自動構成のためのPlaybookを用意するだけです!今回はDockerがインストール済みの仮想マシンが欲しかったので、Docker最新バージョンのインストールとdockerコマンドをsudoなしで実行できるよう、gpasswdコマンドの実行を登録しています。これを任意の場所に作成します(今回はカレントディレクトリにplaybook.ymlとして作成)。
playbook.yml
---
- hosts: all
tasks:
- name: install latest docker
shell: curl -sSL https://get.docker.com/ubuntu/ | sudo sh
- name: add vagrant user to docker group
command: gpasswd -a vagrant docker
sudo: yes
Vagrantの設定ファイル、Vagrantfileは以下の通りです。Ansibleの設定は後半の3行ですね。
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "utopic64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
本来のAnsibleでは、実行するホストを管理するInventoryファイルとHostsファイルが必要なところなのですが、そこはVagrantが自動で用意してくれるようになっています。すごく楽です。
動作確認
では、vagrant upで仮想マシン作成を試してみましょう。
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'utopic64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: docker-otrs_default_1423820467010_76986
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 80 => 8080 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if its present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => /Users/ryuta/Repos/docker-otrs
==> default: Running provisioner: ansible...
PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --private-key=/Users/ryuta/Repos/docker-otrs/.vagrant/machines/default/virtualbox/private_key --user=vagrant --connection=ssh --limit='default' --inventory-file=/Users/ryuta/Repos/docker-otrs/.vagrant/provisioners/ansible/inventory playbook.yml
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [default]
TASK: [install latest docker] *************************************************
changed: [default]
TASK: [add vagrant user to docker group] **************************************
changed: [default]
PLAY RECAP ********************************************************************
default : ok=3 changed=2 unreachable=0 failed=0
$
うまく実行できたみたいです!SSHで接続し、Dockerがインストール済みか確認してみます。
$ vagrant ssh
Welcome to Ubuntu 14.10 (GNU/Linux 3.16.0-28-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Feb 13 09:45:17 UTC 2015
System load: 0.27 Processes: 95
Usage of /: 3.8% of 39.34GB Users logged in: 0
Memory usage: 38% IP address for eth0: 10.0.2.15
Swap usage: 0% IP address for docker0: 172.17.42.1
Graph this data and manage this system at:
https://landscape.canonical.com/
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
Last login: Fri Feb 13 09:43:38 2015 from XXXX
vagrant@vagrant-ubuntu-utopic-64:~$ which docker
/usr/bin/docker
vagrant@vagrant-ubuntu-utopic-64:~$ docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client): a8a31ef
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
vagrant@vagrant-ubuntu-utopic-64:~$
ちゃんとセットアップできてますね!
まとめ
Ansibleのドキュメントを見ると事前に準備する設定ファイルがいくつかあって面倒かなぁと思っていたのですが、Vagrantと組み合わせるとメチャクチャ簡単に使えることがわかりました。Playbookではもっと様々な処理が記述できますし、弊社ブログのいくつか使用例も参考になるかもしれません。Ansibleをガンガン活用していきましょう!