MacにVagrantでCentOS+FuelPHPの環境を作成する(その3. AnsibleのPlaybookを分割してディレクトリ構成も変えてみる)
その2で作成したPlaybookを分割してディレクトリ構成も変えてみる
実施環境
- OS: OS X Yosemite (10.10.5)
- Vagrant: 1.7.4
- Virtualbox: 5.0.10 r104061
- Ansible: 1.9.4
目標
Ansible + Vagrantを使用して下記環境を構築する
今回のゴール
vagrant up
だけでFuelPHPのWelcomeが表示できるようにする
今回の成果物
こちらに置いてあります
negibouze/vagrant-fuelphp_part3
1. 各ツールの準備(インストール済みの場合不要)
step1. Homebrewをインストールする
(省略)
step2. Homebrew Caskをインストールする
(省略)
step3. VirtualBoxをインストールする
(省略)
step4. Vagrantをインストールする
(省略)
step5. Vagrant-vbguest pluginをインストールする
(省略)
step6. Ansibleをインストールする
(省略)
2. Ansibleのディレクトリ構成変更
step1. ベストプラクティスを参考にディレクトリとファイルを作成する(最終的に今回は使わなかったディレクトリもあります)
roles/ hosts site.yml apservers.yml common/ files/ handlers/ main.yml tasks/ main.yml webtier/ files/ httpd.conf php.ini handlers/ main.yml tasks/ curl.yml fuelphp.yml git.yml httpd.yml main.yml php.yml
3. AnsibleのPlaybookの記述を分割する
step1. site.ymlの編集
--- - include: apservers.yml
step2. apservers.ymlの編集
--- - hosts: apservers sudo: true user: vagrant roles: - common - webtier
step3. common/tasks/main.ymlの編集
--- - name: change timezone command: cp -p /usr/share/zoneinfo/Japan /etc/localtime - name: remove all rules from iptables command: /sbin/iptables -F - name: iptables stop command: /sbin/service iptables stop - name: iptables off command: /sbin/chkconfig iptables off
step4. webtier/handlers/main.ymlの編集
--- - name: copy httpd.conf copy: src=../files/httpd.conf dest=/etc/httpd/conf/httpd.conf - name: enable httpd launch settings command: /sbin/chkconfig httpd on - name: copy php.ini copy: src=../files/php.ini dest=/etc/php.ini - name: restart httpd service: name=httpd state=restarted
step5. webtier/tasks/main.ymlの編集
今回はgitとcurlは不要だったのでコメントアウト(せっかくなので.ymlは作成しています)
--- - include: httpd.yml # - include: git.yml - include: php.yml # - include: curl.yml - include: fuelphp.yml
step6. webtier/tasks/配下(main.yml以外)の編集
httpd.yml
--- - name: install httpd yum: name=httpd state=present notify: - copy httpd.conf - enable httpd launch settings
git.yml
--- - name: install dependency packages yum: pkg={{item}} state=present with_items: - curl-devel - expat-devel - gettext-devel - openssl-devel - zlib-devel - perl-ExtUtils-MakeMaker - stat: path=/usr/local/src/git register: git_repo - name: temporary install old version git yum: name=git state=present when: not git_repo.stat.exists - name: clone(or pull) latest version git git: repo=https://git.kernel.org/pub/scm/git/git.git dest=/usr/local/src/git - name: remove old git yum: name=git state=absent when: not git_repo.stat.exists - name: compile git shell: "{{ item }}" args: chdir: /usr/local/src/git with_items: - make prefix=/usr/local all - make prefix=/usr/local install
php.yml
--- - name: install epel yum: name=http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm state=present - name: install remi yum: name=http://rpms.famillecollet.com/enterprise/remi-release-6.rpm state=present - name: install php & packages (& dependencies) yum: pkg={{item}} state=present enablerepo=remi,remi-php56 with_items: - php - php-common - php-cli notify: copy php.ini
curl.yml
--- - name: install latest version curl yum: pkg={{item}} state=present with_items: - http://www.city-fan.org/ftp/contrib/yum-repo/city-fan.org-release-1-13.rhel6.noarch.rpm - libcurl
fuelphp.yml
--- - stat: path=/usr/local/src/fuelphp-1.7.3.zip register: fuelphp_zip - name: download fuelphp1.7.3 get_url: url=http://fuelphp.com/files/download/34 dest=/usr/local/src/fuelphp-1.7.3.zip when: not fuelphp_zip.stat.exists - stat: path=/srv/example/fuelphp register: fuelphp - name: create target directory command: mkdir -p /srv/example - name: unzip fuelphp command: unzip -o /usr/local/src/fuelphp-1.7.3.zip -d /srv/example when: not fuelphp.stat.exists - name: rename unzip directory command: mv fuelphp-1.7.3 fuelphp args: chdir: /srv/example/ when: not fuelphp.stat.exists - name: install oil shell: get.fuelphp.com/oil | sh when: not fuelphp.stat.exists - name: refine install command: php oil refine install args: chdir: /srv/example/fuelphp notify: restart httpd when: not fuelphp.stat.exists
4. 動作確認
step1. 仮想マシンを作成する
$ vagrant up
step2. 動作確認
ブラウザで「192.168.100.11」を開いて下記画面が表示されればOK
5. ちょっとした感想
webtier/tasksの配下が分割しすぎな気もしますが、インストール有無をmain.ymlのコメントアウトで制御できるのは良いかもしれません
6. 参考にしたサイト
実践!Ansibleベストプラクティス(前編) - さくらのナレッジ
Ansibleを使い出す前に押さえておきたかったディレクトリ構成のベストプラクティス - 双六工場日誌