読者です 読者をやめる 読者になる 読者になる

初老のボケ防止日記

おっさんのひとりごとだから気にしないようにな。

スポンサーリンク

今更だけどVagrantでも使ってみるか(4)

技術 仮想化 Vagrant Packer Ansible


前回までのあらすじ

ようやくDockerを使ってみたものの昭和世代にはまだ早かったようなので今度はVagrantでAnsibleをしてみるのであった。

今更だけどVagrantでも使ってみるか(3) - 初老のボケ防止日記

環境

既にVirtualBoxとVagrantは使ってる状態。

OS Windows 8.1(x64)
VirtualBox 4.3.20
Vagrant 1.6.5
Python 2.7.7

Ansibleことはじめ

http://www.ansible.com/home

インストールしてみる。

$ pip install ansible
(snip)
error: Unable to find vcvarsall.bat

イヤな感じ。

とりあえず実行

$ ansible --help
Traceback (most recent call last):
  File "./ansible", line 25, in <module>
    from ansible.runner import Runner
  File "c:\Python27\lib\site-packages\ansible\runner\__init__.py", line 21, in <module>
    import pwd
ImportError: No module named pwd

色々調べたけど「スマン、Windowsサポートしてないわ。Cygwin使えYO」と言われたのでキレる。

解決策を探す。

AnsibleをゲストOSに押し込んでVagrant環境構築する - Qiita

Windows上でVagrantをAnsibleでprovisionしようとすると、Windows上のAnsible環境で頭を悩ませることになります。(調べた限り無理らしい)

そこで、Ansibleは大人しくゲストマシンに押し込んでしまいましょう。

その発想はなかった。グレート。コナン・ザ・グレート。

Ansible入りのboxを再度Packerでつくる

Ansibleインストール用のスクリプトを新規作成

こんな感じで追加した。

  • script/ansible.sh
#!/bin/bash -eux

echo "==> Installing Ansible"
apt-get -y install ansible

ubuntu1404-docker.jsonに"ansible.sh"を追加したテンプレートを作成。ついでにupdate=trueにした。

$ cp ubuntu1404-docker.json ubuntu1404-docker-ansible.json
$ vi ubuntu1404-docker-ansible.json
$ diff -u ubuntu1404-docker.json ubuntu1404-docker-ansible.json
--- ubuntu1404-docker.json      Fri Dec  5 11:36:26 2014
+++ ubuntu1404-docker-ansible.json      Fri Dec 12 17:19:59 2014
@@ -5,7 +5,7 @@
     "cm_version": "",
     "cleanup_pause": "",
     "headless": "",
-    "update": "false",
+    "update": "true",
     "http_proxy": "{{env `http_proxy`}}",
     "https_proxy": "{{env `https_proxy`}}",
     "ftp_proxy": "{{env `ftp_proxy`}}",
@@ -56,7 +56,7 @@
     }
   },
   {
-    "vm_name": "ubuntu1404-docker",
+    "vm_name": "ubuntu1404-docker-ansible",
     "type": "virtualbox-iso",
     "http_directory": "http",
     "iso_url": "{{ user `iso_url` }}",
@@ -139,12 +139,13 @@
       "script/vmtool.sh",
       "script/cmtool.sh",
       "script/docker.sh",
+      "script/ansible.sh",
       "script/cleanup.sh"
     ]
   }],
   "post-processors": [{
     "type": "vagrant",
     "keep_input_artifact": false,
-    "output": "box/{{.Provider}}/ubuntu1404-docker-{{user `cm`}}{{user `cm_vers
ion`}}-{{user `version`}}.box"
+    "output": "box/{{.Provider}}/ubuntu1404-docker-ansible-{{user `cm`}}{{user
`cm_version`}}-{{user `version`}}.box"
   }]
 }

Ansible入りのboxを再度Packerでつくってbox登録

Packerさん、新しいテンプレートよ!

$ packer build -only=virtualbox-iso ubuntu1404-docker-ansible.json

Vagrantさん、新しいboxよ!

$ vagrant box add ubuntu1404dockeransible box/virtualbox/ubuntu1404-docker-ansible-nocm-0.1.0.box
$ vagrant box list
ubuntu1404docker        (virtualbox, 0)
ubuntu1404dockeransible (virtualbox, 0)

NOマネーでfinish(特に意味は無い)

$ vagrant init ubuntu1404dockeransible

これで仕込みはOK。

Vagrant upでAnsibleかます準備

で、これからが本番。

ansible用ファイルを作成

参考記事の真似っ子構成。

ansible/
ansible/hellodb.yml
ansible/helloweb.yml
ansible/hosts
provision_db.sh
provision_web.sh
Vagrantfile
  • ansible/hosts

ターゲットホストを書く。ローカル実行なので自分だけしかいない。ロンリネス。

127.0.0.1 ansible_connection=local
  • ansible/hellodb.yml

Ansibleさんに渡すPlayBook。PlayBoyではない。今回はあくまでテスト

---
- hosts: all
  tasks:
    - shell: echo "Hello World from db"
  • ansible/helloweb.yml

Ansibleさんに渡すPlayBook。PlayMateではない。今回はあくまでテスト

---
- hosts: all
  tasks:
    - shell: echo "Hello World from web"
  • provision_db.sh

VagrantさんがVM側にsshで入って実行するスクリプト(つまりVM側でAnsibleが自分に対して実行される)

ansible-playbook -i /vagrant/ansible/hosts /vagrant/ansible/hellodb.yml
  • provision_web.sh

つまり、セルフまかないAnsibleである

ansible-playbook -i /vagrant/ansible/hosts /vagrant/ansible/helloweb.yml

Vagrantfileを編集

ここまでで「web」と「db」という二種類のファイルがあったのはVagrantでVMを2つ起動したかったから。決してWEB+DB PRESSが読みたかったわけではない(いや読みたいから下さい)

複数起動時の定義についてはこちらを参考にした。

[Vagrant] Vagrant で複数のVM を立ち上げて、お互いに通信できるようにするには [VirtualBox] - Qiita

  • Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu1404dockerabsible"
  config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=775', 'fmode=664']

  config.vm.define :web do | web |
    web.vm.hostname = "django"
    web.vm.provision "shell", :path => "provision_web.sh"
    web.vm.network :private_network, ip: "192.168.33.10", virtualbox__intnet: "intnet"
  end

  config.vm.define :db do | db |
    db.vm.hostname = "postgres"
    db.vm.provision "shell", :path => "provision_db.sh"
    db.vm.network :private_network, ip: "192.168.33.20", virtualbox__intnet: "intnet"
  end
end

Vagrant upでAnsibleぶちかます

$ vagrant status
Current machine states:

web                       not created (virtualbox)
db                        not created (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

Vagrantfileを見てるらしい。のび太さんのえっち。

シェイプアップ乱

$ vagrant up
Bringing machine 'web' up with 'virtualbox' provider...
Bringing machine 'db' up with 'virtualbox' provider...
==> web: Importing base box 'ubuntu1404dockerabsible'...
==> web: Matching MAC address for NAT networking...
==> web: Setting the name of the VM: test2_web_1418377031279_50567
==> web: Clearing any previously set network interfaces...
==> web: Preparing network interfaces based on configuration...
    web: Adapter 1: nat
    web: Adapter 2: intnet
==> web: Forwarding ports...
    web: 22 => 2222 (adapter 1)
==> web: Booting VM...
==> web: Waiting for machine to boot. This may take a few minutes...
    web: SSH address: 127.0.0.1:2222
    web: SSH username: vagrant
    web: SSH auth method: private key
    web: Warning: Connection timeout. Retrying...
==> web: Machine booted and ready!
==> web: Checking for guest additions in VM...
==> web: Setting hostname...
==> web: Configuring and enabling network interfaces...
==> web: Mounting shared folders...
    web: /vagrant => C:/AppData/Packer/test2
==> web: Running provisioner: shell...
    web: Running: C:/Users/osa_000/AppData/Local/Temp/vagrant-shell20141212-2280-1mu6mav.sh
==> web: stdin: is not a tty
==> web:
==> web: PLAY [all] ********************************************************************
==> web:
==> web: GATHERING FACTS ***************************************************************
==> web: ok: [127.0.0.1]
==> web:
==> web: TASK: [shell echo "Hello World from web"] *************************************
==> web: changed: [127.0.0.1]
==> web:
==> web: PLAY RECAP ********************************************************************
==> web: 127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0
==> db: Importing base box 'ubuntu1404dockerabsible'...
==> db: Matching MAC address for NAT networking...
==> db: Setting the name of the VM: test2_db_1418377087848_10745
==> db: Fixed port collision for 22 => 2222. Now on port 2200.
==> db: Clearing any previously set network interfaces...
==> db: Preparing network interfaces based on configuration...
    db: Adapter 1: nat
    db: Adapter 2: intnet
==> db: Forwarding ports...
    db: 22 => 2200 (adapter 1)
==> db: Booting VM...
==> db: Waiting for machine to boot. This may take a few minutes...
    db: SSH address: 127.0.0.1:2200
    db: SSH username: vagrant
    db: SSH auth method: private key
    db: Warning: Connection timeout. Retrying...
==> db: Machine booted and ready!
==> db: Checking for guest additions in VM...
==> db: Setting hostname...
==> db: Configuring and enabling network interfaces...
==> db: Mounting shared folders...
    db: /vagrant => C:/AppData/Packer/test2
==> db: Running provisioner: shell...
    db: Running: C:/Users/osa_000/AppData/Local/Temp/vagrant-shell20141212-2280-sbt14v.sh
==> db: stdin: is not a tty
==> db:
==> db: PLAY [all] ********************************************************************
==> db:
==> db: GATHERING FACTS ***************************************************************
==> db: ok: [127.0.0.1]
==> db:
==> db: TASK: [shell echo "Hello World from web"] *************************************
==> db: changed: [127.0.0.1]
==> db:
==> db: PLAY RECAP ********************************************************************
==> db: 127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0

シェイプアップ乱ニング

$ vagrant status
Current machine states:

web                       running (virtualbox)
db                        running (virtualbox)

一旦落としてみる

$ vagrant halt
==> db: Attempting graceful shutdown of VM...
==> web: Attempting graceful shutdown of VM...

でもって再びアップ

$ vagrant up                                                                                   
Bringing machine 'web' up with 'virtualbox' provider...                                        
Bringing machine 'db' up with 'virtualbox' provider...                                         
==> web: Clearing any previously set forwarded ports...                                        
==> web: Clearing any previously set network interfaces...                                     
==> web: Preparing network interfaces based on configuration...                                
    web: Adapter 1: nat                                                                        
    web: Adapter 2: intnet                                                                     
==> web: Forwarding ports...                                                                   
    web: 22 => 2222 (adapter 1)                                                                
==> web: Booting VM...                                                                         
==> web: Waiting for machine to boot. This may take a few minutes...                           
    web: SSH address: 127.0.0.1:2222                                                           
    web: SSH username: vagrant                                                                 
    web: SSH auth method: private key                                                          
    web: Warning: Connection timeout. Retrying...                                              
==> web: Machine booted and ready!                                                             
==> web: Checking for guest additions in VM...                                                 
==> web: Setting hostname...                                                                   
==> web: Configuring and enabling network interfaces...                                        
==> web: Mounting shared folders...                                                            
    web: /vagrant => C:/AppData/Packer/test2                                                   
==> web: Machine already provisioned. Run `vagrant provision` or use the `--provision`         
==> web: to force provisioning. Provisioners marked to run always will still run.              
==> db: Clearing any previously set forwarded ports...                                         
==> db: Fixed port collision for 22 => 2222. Now on port 2200.                                 
==> db: Clearing any previously set network interfaces...                                      
==> db: Preparing network interfaces based on configuration...                                 
    db: Adapter 1: nat                                                                         
    db: Adapter 2: intnet                                                                      
==> db: Forwarding ports...                                                                    
    db: 22 => 2200 (adapter 1)                                                                 
==> db: Booting VM...                                                                          
==> db: Waiting for machine to boot. This may take a few minutes...                            
    db: SSH address: 127.0.0.1:2200                                                            
    db: SSH username: vagrant                                                                  
    db: SSH auth method: private key                                                           
    db: Warning: Connection timeout. Retrying...                                               
==> db: Machine booted and ready!                                                              
==> db: Checking for guest additions in VM...                                                  
==> db: Setting hostname...                                                                    
==> db: Configuring and enabling network interfaces...                                         
==> db: Mounting shared folders...                                                             
    db: /vagrant => C:/AppData/Packer/test2                                                    
==> db: Machine already provisioned. Run `vagrant provision` or use the `--provision`          
==> db: to force provisioning. Provisioners marked to run always will still run.               

既にProvisioning済だからAnsibleは呼ばれない。Vagrant、チョットカシコイ

感想

別にAnsibleじゃなくてもshellでコマンド書いたらいいじゃんというツッコミはあるのだろうが、今どきの流行りが気になるおじさんであるので気にしたら負け。ようやく土台ができたので、夢が広がりング。

入門Ansible

入門Ansible

次回予告

ちゃんとAnsibleのPlayBookでミドルを入れてみる。

今更だけどVagrantでも使ってみるか(5) - 初老のボケ防止日記

スポンサーリンク