LoginSignup
0
4

More than 5 years have passed since last update.

VirtualBox+Vagrant+AnsibleでLAMP環境を構築

Last updated at Posted at 2017-07-27

やりたいこと

著者はWindows7をメイン機で使っているが、
直接WindowsにAnsibleやVagrantなどのツールを
インストールしたくないため、VMにて開発環境ホストを構築し、
その中に実際の開発環境(LAMP)を構築したい。

ホスト構成

本当の意味でのホスト      : Windows 7
開発環境ホスト(VMWare)   :ubuntu 16.04 LTS
開発環境ゲスト(Virtualbox)  :CentOS 6.7

環境構成

1.開発環境ホスト(ubuntu)で行うこと

  • VirtualBoxのインストール
  • Vagrantのインストール
  • Ansibleのインストール

2.開発環境ゲスト(centos)で行うこと

  • Apache HTTPDのインストール
  • PHPのインストール
  • MySQLのインストール

前提

Windows7のマシンにVMWareワークステーションをインストールしていたので、
VMのOSとしてUbuntuをインストールした。
以降の操作は開発環境ホスト(ubuntu)上で行ったものである。

※VMWare上でさらにVirtualBoxを利用するためには、
VMWareの仮想マシン設定を以下のように変更する必要がある。
仮想マシン設定 > ハードウェア > プロセッサ
仮想化エンジンの項目
「Intel VT-x/EPT または AMD-V/RVIを仮想化」のチェックをONにする。

Virtualboxのインストール

~$ sudo vi /etc/apt/sources.list

[sudo] {USER} のパスワード: 
~$ sudo wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

OK

~$ sudo apt-get update

取得:1 http://download.virtualbox.org/virtualbox/debian raring InRelease [7,152 B]
ヒット:2 http://archive.ubuntulinux.jp/ubuntu xenial InRelease                                                        
取得:3 http://download.virtualbox.org/virtualbox/debian raring/contrib amd64 Packages [1,337 B]                       
無視:4 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial InRelease                                              
取得:5 http://download.virtualbox.org/virtualbox/debian raring/contrib i386 Packages [1,322 B]                        
ヒット:6 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial Release                                              
ヒット:8 http://jp.archive.ubuntu.com/ubuntu xenial InRelease                                                         
ヒット:9 http://jp.archive.ubuntu.com/ubuntu xenial-updates InRelease                                                 
ヒット:10 http://jp.archive.ubuntu.com/ubuntu xenial-backports InRelease                                              
取得:11 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]            
112 kB を 1秒 で取得しました (86.0 kB/s)
パッケージリストを読み込んでいます... 完了
W: http://download.virtualbox.org/virtualbox/debian/dists/raring/InRelease: 鍵 7B0FAB3A13B907435925D9C954422A4B98AB5139 による署名は弱い digest アルゴリズム (SHA1) を使用しています
~$ sudo apt-get install virtualbox
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
  libpango1.0-0 libpangox-1.0-0 linux-headers-4.4.0-21 linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
  linux-image-extra-4.4.0-21-generic ubuntu-core-launcher
これを削除するには 'sudo apt autoremove' を利用してください。
以下の追加パッケージがインストールされます:
  dkms libgsoap8 libqt4-opengl libsdl1.2debian libvncserver1 virtualbox-dkms virtualbox-qt
提案パッケージ:
  vde2 virtualbox-guest-additions-iso
以下のパッケージが新たにインストールされます:
  dkms libgsoap8 libqt4-opengl libsdl1.2debian libvncserver1 virtualbox virtualbox-dkms virtualbox-qt
アップグレード: 0 個、新規インストール: 8 個、削除: 0 個、保留: 59 個。
23.0 MB のアーカイブを取得する必要があります。
この操作後に追加で 99.0 MB のディスク容量が消費されます。
続行しますか? [Y/n] y
取得:1 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dkms all 2.2.0.3-2ubuntu11.3 [66.1 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 libgsoap8 amd64 2.8.28-1 [216 kB]
取得:3 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 libqt4-opengl amd64 4:4.8.7+dfsg-5ubuntu2 [301 kB]
取得:4 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 libsdl1.2debian amd64 1.2.15+dfsg1-3 [168 kB]
取得:5 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libvncserver1 amd64 0.9.10+dfsg-3ubuntu0.16.04.1 [122 kB]
取得:6 http://jp.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 virtualbox-dkms all 5.0.40-dfsg-0ubuntu1.16.04.1 [623 kB]
取得:7 http://jp.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 virtualbox amd64 5.0.40-dfsg-0ubuntu1.16.04.1 [14.3 MB]
取得:8 http://jp.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 virtualbox-qt amd64 5.0.40-dfsg-0ubuntu1.16.04.1 [7,195 kB]
23.0 MB を 3秒 で取得しました (6,402 kB/s) 
以前に未選択のパッケージ dkms を選択しています。
(データベースを読み込んでいます ... 現在 241623 個のファイルとディレクトリがインストールされています。)
.../dkms_2.2.0.3-2ubuntu11.3_all.deb を展開する準備をしています ...
dkms (2.2.0.3-2ubuntu11.3) を展開しています...
以前に未選択のパッケージ libgsoap8:amd64 を選択しています。
.../libgsoap8_2.8.28-1_amd64.deb を展開する準備をしています ...
libgsoap8:amd64 (2.8.28-1) を展開しています...
以前に未選択のパッケージ libqt4-opengl:amd64 を選択しています。
.../libqt4-opengl_4%3a4.8.7+dfsg-5ubuntu2_amd64.deb を展開する準備をしています ...
libqt4-opengl:amd64 (4:4.8.7+dfsg-5ubuntu2) を展開しています...
以前に未選択のパッケージ libsdl1.2debian:amd64 を選択しています。
.../libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb を展開する準備をしています ...
libsdl1.2debian:amd64 (1.2.15+dfsg1-3) を展開しています...
以前に未選択のパッケージ libvncserver1:amd64 を選択しています。
.../libvncserver1_0.9.10+dfsg-3ubuntu0.16.04.1_amd64.deb を展開する準備をしています ...
libvncserver1:amd64 (0.9.10+dfsg-3ubuntu0.16.04.1) を展開しています...
以前に未選択のパッケージ virtualbox-dkms を選択しています。
.../virtualbox-dkms_5.0.40-dfsg-0ubuntu1.16.04.1_all.deb を展開する準備をしています ...
virtualbox-dkms (5.0.40-dfsg-0ubuntu1.16.04.1) を展開しています...
以前に未選択のパッケージ virtualbox を選択しています。
.../virtualbox_5.0.40-dfsg-0ubuntu1.16.04.1_amd64.deb を展開する準備をしています ...
virtualbox (5.0.40-dfsg-0ubuntu1.16.04.1) を展開しています...
以前に未選択のパッケージ virtualbox-qt を選択しています。
.../virtualbox-qt_5.0.40-dfsg-0ubuntu1.16.04.1_amd64.deb を展開する準備をしています ...
virtualbox-qt (5.0.40-dfsg-0ubuntu1.16.04.1) を展開しています...
man-db (2.7.5-1) のトリガを処理しています ...
libc-bin (2.23-0ubuntu9) のトリガを処理しています ...
systemd (229-4ubuntu17) のトリガを処理しています ...
ureadahead (0.100.0-19) のトリガを処理しています ...
hicolor-icon-theme (0.15-0ubuntu1) のトリガを処理しています ...
shared-mime-info (1.5-2ubuntu0.1) のトリガを処理しています ...
bamfdaemon (0.5.3~bzr0+16.04.20160824-0ubuntu1) のトリガを処理しています ...
Rebuilding /usr/share/applications/bamf-2.index...
gnome-menus (3.13.3-6ubuntu3.1) のトリガを処理しています ...
desktop-file-utils (0.22-1ubuntu5.1) のトリガを処理しています ...
mime-support (3.59ubuntu1) のトリガを処理しています ...
dkms (2.2.0.3-2ubuntu11.3) を設定しています ...
libgsoap8:amd64 (2.8.28-1) を設定しています ...
libqt4-opengl:amd64 (4:4.8.7+dfsg-5ubuntu2) を設定しています ...
libsdl1.2debian:amd64 (1.2.15+dfsg1-3) を設定しています ...
libvncserver1:amd64 (0.9.10+dfsg-3ubuntu0.16.04.1) を設定しています ...
virtualbox-dkms (5.0.40-dfsg-0ubuntu1.16.04.1) を設定しています ...
Loading new virtualbox-5.0.40 DKMS files...
First Installation: checking all kernels...
Building only for 4.4.0-87-generic
Building initial module for 4.4.0-87-generic
Done.

vboxdrv:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-87-generic/updates/dkms/

vboxnetadp.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-87-generic/updates/dkms/

vboxnetflt.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-87-generic/updates/dkms/

vboxpci.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.4.0-87-generic/updates/dkms/

depmod........

DKMS: install completed.
virtualbox (5.0.40-dfsg-0ubuntu1.16.04.1) を設定しています ...
vboxweb.service is a disabled or a static unit, not starting it.
virtualbox-qt (5.0.40-dfsg-0ubuntu1.16.04.1) を設定しています ...
libc-bin (2.23-0ubuntu9) のトリガを処理しています ...
systemd (229-4ubuntu17) のトリガを処理しています ...
ureadahead (0.100.0-19) のトリガを処理しています ...

Vagrantのダウンロードとインストール

~$ mkdir work

~$ cd work/

~/work$ wget https://releases.hashicorp.com/vagrant/1.9.7/vagrant_1.9.7_x86_64.deb

--2017-07-26 10:31:59--  https://releases.hashicorp.com/vagrant/1.9.7/vagrant_1.9.7_x86_64.deb
releases.hashicorp.com (releases.hashicorp.com) をDNSに問いあわせています... 151.101.73.183, 2a04:4e42:11::439
releases.hashicorp.com (releases.hashicorp.com)|151.101.73.183|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 79603338 (76M) [application/x-debian-package]
`vagrant_1.9.7_x86_64.deb' に保存中

vagrant_1.9.7_x86_64.deb      100%[================================================>]  75.92M  4.84MB/s    in 19s     

2017-07-26 10:32:18 (3.95 MB/s) - `vagrant_1.9.7_x86_64.deb' へ保存完了 [79603338/79603338]


~/work$ sudo dpkg --install vagrant_1.9.7_x86_64.deb 
[sudo] {USER} のパスワード: 
以前に未選択のパッケージ vagrant を選択しています。
(データベースを読み込んでいます ... 現在 242296 個のファイルとディレクトリがインストールされています。)
vagrant_1.9.7_x86_64.deb を展開する準備をしています ...
vagrant (1:1.9.7) を展開しています...
vagrant (1:1.9.7) を設定しています ...
~/work$ 

Ansibleのダウンロードとインストール

~$ sudo apt-get install software-properties-common

[sudo] {USER} のパスワード: 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
software-properties-common はすでに最新バージョン (0.96.20.7) です。
software-properties-common は手動でインストールしたと設定されました。
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
  libpango1.0-0 libpangox-1.0-0 linux-headers-4.4.0-21 linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
  linux-image-extra-4.4.0-21-generic ubuntu-core-launcher
これを削除するには 'sudo apt autoremove' を利用してください。
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 59 個。
~$ sudo apt-add-repository ppa:ansible/ansible
 Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/
 詳しい情報: https://launchpad.net/~ansible/+archive/ubuntu/ansible
[ENTER] を押すと続行します。ctrl-c で追加をキャンセルできます

gpg: 鍵リング「/tmp/tmptboy98lz/secring.gpg」ができました
gpg: 鍵リング「/tmp/tmptboy98lz/pubring.gpg」ができました
gpg: 鍵7BB9C367をhkpからサーバkeyserver.ubuntu.comに要求
gpg: /tmp/tmptboy98lz/trustdb.gpg: 信用データベースができました
gpg: 鍵7BB9C367: 公開鍵"Launchpad PPA for Ansible, Inc."をインポートしました
gpg: 処理数の合計: 1
gpg:               インポート: 1  (RSA: 1)
OK
~$ sudo apt-get update
ヒット:1 http://archive.ubuntulinux.jp/ubuntu xenial InRelease
ヒット:2 http://jp.archive.ubuntu.com/ubuntu xenial InRelease                                                         
ヒット:3 http://download.virtualbox.org/virtualbox/debian raring InRelease                                            
無視:4 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial InRelease                                              
取得:5 http://jp.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]                                          
ヒット:6 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial Release                                              
取得:7 http://jp.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]                                        
取得:8 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease [18.1 kB]                                     
取得:10 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]                                          
取得:11 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [304 kB]                        
取得:12 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [197 kB]                           
取得:13 http://jp.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [163 kB]                    
取得:14 http://jp.archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [212 kB]                       
取得:15 http://jp.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 DEP-11 Metadata [5,888 B]                 
取得:16 http://jp.archive.ubuntu.com/ubuntu xenial-backports/main amd64 DEP-11 Metadata [3,328 B]                     
取得:17 http://jp.archive.ubuntu.com/ubuntu xenial-backports/universe amd64 DEP-11 Metadata [4,684 B]                 
取得:18 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main amd64 Packages [552 B]                   
取得:19 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main i386 Packages [552 B]
取得:20 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main Translation-en [340 B]
取得:21 http://security.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [59.1 kB]
取得:22 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [54.2 kB]
取得:23 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [40.7 kB]
取得:24 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [56.2 kB]
1,426 kB を 2秒 で取得しました (529 kB/s)                               
パッケージリストを読み込んでいます... 完了
W: http://download.virtualbox.org/virtualbox/debian/dists/raring/InRelease: 鍵 7B0FAB3A13B907435925D9C954422A4B98AB5139 による署名は弱い digest アルゴリズム (SHA1) を使用しています
~$ sudo apt-get install ansible
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
  libpango1.0-0 libpangox-1.0-0 linux-headers-4.4.0-21 linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
  linux-image-extra-4.4.0-21-generic ubuntu-core-launcher
これを削除するには 'sudo apt autoremove' を利用してください。
以下の追加パッケージがインストールされます:
  python-crypto python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-paramiko python-pkg-resources
  python-setuptools python-six python-yaml sshpass
提案パッケージ:
  python-crypto-dbg python-crypto-doc python-jinja2-doc python-setuptools-doc
以下のパッケージが新たにインストールされます:
  ansible python-crypto python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-paramiko
  python-pkg-resources python-setuptools python-six python-yaml sshpass
アップグレード: 0 個、新規インストール: 12 個、削除: 0 個、保留: 59 個。
2,987 kB のアーカイブを取得する必要があります。
この操作後に追加で 21.0 MB のディスク容量が消費されます。
続行しますか? [Y/n] y
取得:1 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-markupsafe amd64 0.23-2build2 [15.5 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-jinja2 all 2.8-1 [109 kB]           
取得:3 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-yaml amd64 3.11-3build1 [105 kB]
取得:4 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 python-crypto amd64 2.6.1-6ubuntu0.16.04.2 [246 kB]
取得:5 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-six all 1.10.0-3 [10.9 kB]
取得:6 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-ecdsa all 0.13-2 [34.0 kB]
取得:7 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-paramiko all 1.16.0-1 [109 kB]
取得:8 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main amd64 ansible all 2.3.1.0-1ppa~xenial [2,037 kB]
取得:9 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-httplib2 all 0.9.1+dfsg-1 [34.2 kB]
取得:10 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-pkg-resources all 20.7.0-1 [108 kB]
取得:11 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 python-setuptools all 20.7.0-1 [169 kB]
取得:12 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 sshpass amd64 1.05-1 [10.5 kB]
2,987 kB を 5秒 で取得しました (572 kB/s)                      
以前に未選択のパッケージ python-markupsafe を選択しています。
(データベースを読み込んでいます ... 現在 248574 個のファイルとディレクトリがインストールされています。)
.../python-markupsafe_0.23-2build2_amd64.deb を展開する準備をしています ...
python-markupsafe (0.23-2build2) を展開しています...
以前に未選択のパッケージ python-jinja2 を選択しています。
.../python-jinja2_2.8-1_all.deb を展開する準備をしています ...
python-jinja2 (2.8-1) を展開しています...
以前に未選択のパッケージ python-yaml を選択しています。
.../python-yaml_3.11-3build1_amd64.deb を展開する準備をしています ...
python-yaml (3.11-3build1) を展開しています...
以前に未選択のパッケージ python-crypto を選択しています。
.../python-crypto_2.6.1-6ubuntu0.16.04.2_amd64.deb を展開する準備をしています ...
python-crypto (2.6.1-6ubuntu0.16.04.2) を展開しています...
以前に未選択のパッケージ python-six を選択しています。
.../python-six_1.10.0-3_all.deb を展開する準備をしています ...
python-six (1.10.0-3) を展開しています...
以前に未選択のパッケージ python-ecdsa を選択しています。
.../python-ecdsa_0.13-2_all.deb を展開する準備をしています ...
python-ecdsa (0.13-2) を展開しています...
以前に未選択のパッケージ python-paramiko を選択しています。
.../python-paramiko_1.16.0-1_all.deb を展開する準備をしています ...
python-paramiko (1.16.0-1) を展開しています...
以前に未選択のパッケージ python-httplib2 を選択しています。
.../python-httplib2_0.9.1+dfsg-1_all.deb を展開する準備をしています ...
python-httplib2 (0.9.1+dfsg-1) を展開しています...
以前に未選択のパッケージ python-pkg-resources を選択しています。
.../python-pkg-resources_20.7.0-1_all.deb を展開する準備をしています ...
python-pkg-resources (20.7.0-1) を展開しています...
以前に未選択のパッケージ python-setuptools を選択しています。
.../python-setuptools_20.7.0-1_all.deb を展開する準備をしています ...
python-setuptools (20.7.0-1) を展開しています...
以前に未選択のパッケージ sshpass を選択しています。
.../sshpass_1.05-1_amd64.deb を展開する準備をしています ...
sshpass (1.05-1) を展開しています...
以前に未選択のパッケージ ansible を選択しています。
.../ansible_2.3.1.0-1ppa~xenial_all.deb を展開する準備をしています ...
ansible (2.3.1.0-1ppa~xenial) を展開しています...
man-db (2.7.5-1) のトリガを処理しています ...
python-markupsafe (0.23-2build2) を設定しています ...
python-jinja2 (2.8-1) を設定しています ...
python-yaml (3.11-3build1) を設定しています ...
python-crypto (2.6.1-6ubuntu0.16.04.2) を設定しています ...
python-six (1.10.0-3) を設定しています ...
python-ecdsa (0.13-2) を設定しています ...
python-paramiko (1.16.0-1) を設定しています ...
python-httplib2 (0.9.1+dfsg-1) を設定しています ...
python-pkg-resources (20.7.0-1) を設定しています ...
python-setuptools (20.7.0-1) を設定しています ...
sshpass (1.05-1) を設定しています ...
ansible (2.3.1.0-1ppa~xenial) を設定しています ...

Vagrant box 追加

~/work/centos67$ vagrant box list

There are no installed boxes! Use `vagrant box add` to add some.


~/work/centos67$ vagrant box add centos67https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos67' (v0) for provider: 
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
==> box: Successfully added box 'centos67' (v0) for 'virtualbox'!


~/work/centos67$ vagrant box list
centos67 (virtualbox, 0)

Vagrant box 初期化

Vagrantfileの作成

~/work/centos67$ vagrant init centos67

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.


~/work/centos67$ ll

合計 12
drwxrwxr-x 2 {USER} {USER} 4096  7月 26 13:02 ./
drwxrwxr-x 3 {USER} {USER} 4096  7月 26 12:01 ../
-rw-rw-r-- 1 {USER} {USER} 3015  7月 26 13:02 Vagrantfile

Vagrantfileの中身の確認

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos67"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false
  #config.vm.box_check_update = true

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  #config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
  #config.vm.network "public_network"
  config.vm.network "public_network", ip: "192.168.33.10", bridge: "ens33"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL

  # Disable the new default behavior introduced in Vagrant 1.7, to
  # ensure that all Vagrant machines will use the same SSH key pair.
  # See https://github.com/mitchellh/vagrant/issues/5005
  config.ssh.insert_key = false
end

Vagrant box 起動

~/work/centos67$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> 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: Adapter 2: bridged
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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 reset. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 5.0
==> default: Configuring and enabling network interfaces...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Mounting shared folders...
    default: /vagrant => /home/{USER}/work/centos67
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

ansible-examplesをGitから取得する

AnsibleにてLAMP構築するためのymlがGitで公開されているのそれを利用する。
ubuntuにgitをインストール後、以下を実行する
bash
git clone https://github.com/ansible/ansible-examples.git

ansible-examplesには色々なサンプルが混在しているが、
その中のlamp_simpleを利用する。

ansible-examplesのhostsを書き換える

IPは、開発環境ゲストのものを指定する。
AnsibleがsshでVMにアクセスする際にssh鍵関係のエラーが出るので、hostsファイルにansible_sshの設定を追加する
```bash
[servers]
192.168.33.10 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/home/{USER}/work/centos67/.vagrant/machines/default/virtualbox/private_key

[webservers]
192.168.33.10 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/home/{USER}/work/centos67/.vagrant/machines/default/virtualbox/private_key

[dbservers]
192.168.33.10 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/home/{USER}/work/centos67/.vagrant/machines/default/virtualbox/private_key
```

Ansible実行

~/work/ansible_lamp$ cd ansible-examples/lamp_simple

~/work/ansible_lamp/ansible-examples/lamp_simple$ ansible-playbook -i hosts site.yml -s -K

SUDO password: 

PLAY [apply common configuration to all nodes] **********************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [common : Install ntp] *****************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [common : Configure ntp file] **********************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [common : Start the ntp service] *******************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [common : test to see if selinux is running] *******************************************************************************************************************************************************
ok: [192.168.33.10]

PLAY [configure and deploy the webservers and application code] *****************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [web : Install http and php etc] *******************************************************************************************************************************************************************
ok: [192.168.33.10] => (item=[u'httpd', u'php', u'php-mysql', u'git', u'libsemanage-python', u'libselinux-python'])

TASK [web : insert iptables rule for httpd] *************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [web : http service state] *************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [web : Configure SELinux to allow httpd to connect to remote database] *****************************************************************************************************************************
skipping: [192.168.33.10]

TASK [web : Copy the code from repository] **************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [web : Creates the index.php file] *****************************************************************************************************************************************************************
ok: [192.168.33.10]

PLAY [deploy MySQL and configure the databases] *********************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [db : Install Mysql package] ***********************************************************************************************************************************************************************
ok: [192.168.33.10] => (item=[u'mysql-server', u'MySQL-python', u'libselinux-python', u'libsemanage-python'])

TASK [db : Configure SELinux to start mysql on any port] ************************************************************************************************************************************************
skipping: [192.168.33.10]

TASK [db : Create Mysql configuration file] *************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [db : Start Mysql Service] *************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [db : insert iptables rule] ************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [db : Create Application Database] *****************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [db : Create Application DB User] ******************************************************************************************************************************************************************
ok: [192.168.33.10]

PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.33.10              : ok=18   changed=0    unreachable=0    failed=0   

~/work/ansible_lamp/ansible-examples/lamp_simple$ 

設定完了確認

$ vagrant ssh
$ mysql -u root
$ show databases;
-- foodbができていることを確認
開発環境ホストからブラウザで[IP]/index.phpへアクセスし、
下記文字列が表示されたら成功


Homepage 
Hello, World! I am a web server configured using Ansible and I am : localhost.localdomain
List of Databases: 
information_schema foodb mysql test

参考

VagrantでCentOS6.7環境構築 (1)
http://qiita.com/NAZA/items/adb849e795861d81a520

今更だけどAnsibleでUbuntuにLAMPを構築する
http://qiita.com/Yaruki00/items/3efe174de0197a07a610

サンプルAnsible
https://github.com/ansible/ansible-examples

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4