主に備忘録

基本的には自分用の備忘録っぽいもの。

MacにVagrantでCentOS+FuelPHPの環境を作成する(その2. Ansibleを使ってみる)

その1で作成したshellをAnsibleに変えてみる

実施環境

目標

Ansible + Vagrantを使用して下記環境を構築する

今回のゴール

vagrant upだけでFuelPHPのWelcomeが表示できるようにする

今回の成果物

こちらに置いてあります

negibouze/vagrant-fuelphp_part2

1. 各ツールの準備(インストール済みの場合不要)

step1. Homebrewをインストールする

(省略)

step2. Homebrew Caskをインストールする

(省略)

step3. VirtualBoxをインストールする

(省略)

step4. Vagrantをインストールする

(省略)

step5. Vagrant-vbguest pluginをインストールする

(省略)

step6. Ansibleをインストールする

$ brew install ansible

2. Vagrantfileの編集

step1. $scriptを削除

下記は不要になるため削除

$script = <<SCRIPT
  # 中略
SCRIPT

step2. provisionを編集

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "provisioning/site.yml"
  ansible.inventory_path = "provisioning/hosts"
  ansible.limit = 'all'
end

ここまででこんな感じ

Vagrant.configure(2) do |config|

  config.vm.box = "puppetlabs/centos-6.6-64-nocm"
  config.vm.box_url = "https://vagrantcloud.com/puppetlabs/boxes/centos-6.6-64-nocm/versions/1.0.2/providers/virtualbox.box"

  config.vm.define :"web1" do |host|
    host.vm.hostname = "web1"
    host.vm.network :private_network, ip: "192.168.100.11", netmask: "255.255.255.0"
  end

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/site.yml"
    ansible.inventory_path = "provisioning/hosts"
    ansible.limit = 'all'
  end
end

3. Ansibleのファイルを作成

step1. Ansible用のディレクトリ作成(任意)

$ mkdir provisioning && cd $_

step2. inventory fileを作成

$ touch hosts

step3. Playbookを作成

$ touch site.yml

4. hostsを編集する

step1. 対象サーバのIPアドレスを追加

[server]
192.168.100.11

5. Playbookを編集する

その1で記述したshellの内容をYAMLに書き換える

step1. 書き始め

---
- hosts: server
  sudo: true
  user: vagrant
  tasks:
  handlers:

step2. Timezoneを変更

- name: change timezone
  command: cp -p /usr/share/zoneinfo/Japan /etc/localtime

step3. iptablesを無効にする

- 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. httpd(apache)をインストールする

notifyを使用してhttpd.confをコピーと起動設定を行う

tasks

- name: install httpd
  yum: name=httpd state=present
  notify:
    - copy httpd.conf
    - enable launch settings

handlers

  - name: copy httpd.conf
    copy: src=../httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: enable launch settings
    command: /sbin/chkconfig httpd on

step5. Gitをインストールする(やらなくても良い)

FuelPHPをgitで持ってくるのをやめたためこの手順は不要でした(ここは時間がかかるのでやらない方が良いです)
* 多少古くても良い場合

- name: install old version git
  yum: name=git state=present
  • 最新版を入れる場合
- 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

step6. PHP5.6をインストールする

notifyを使用してphp.iniをコピーする

tasks

- 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

handlers

- name: copy php.ini
  copy: src=../php.ini dest=/etc/php.ini

step7. curlを更新する(やらなくても良い)

- 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

step8. FuelPHPをインストールする

$ oil create <project_name>

tasks

- 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
- name: refine install
  command: php oil refine install
  args:
    chdir: /srv/example/fuelphp
  notify: restart httpd

handlers

- name: restart httpd
  service: name=httpd state=restarted

6. 動作確認

step1. 仮想マシンを作成する

$ vagrant up

step2. 動作確認

ブラウザで「192.168.100.11」を開いて下記画面が表示されればOK
f:id:ita3y:20151123194535p:plain