interprism's blog

インタープリズム株式会社の開発者ブログです。

Ansibleでmacの開発環境を構築してみた

この投稿は インタープリズム的「俺達私達の進捗を上げる25個前後のTips」 Advent Calendar 2015 - Qiitaの9日目 の記事です。

初めまして、suganoです。 現場で使用していたMacBookを修理に出すことになり、修理から返ってくるまでの間、別のMacで作業することになってしまいました。 一から開発環境を整えるのはなかなか手間なので、以前知り合いからAnsibleで開発環境の構築を自動化したよという話でt-wadaのブログ Mac の開発環境構築を自動化する (2015 年初旬編) を紹介していたので自分もやってみました。

まずは準備から

Homebrewをインストールするため、App StoreからXcodeをインストールし、Xcode Command Line Toolsを入れる。 入れたら、Homebrewの公式ページにある以下のスクリプトを実行。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrewを入れたら、updateしてAnsibleをインストールします。

brew update
brew install python
brew install ansible

事前の準備は以上になります。

Ansibleを使ってみる

まず、実行対象となるホストを指定するためインベントリファイルを作ります。

今回対象となるのは自分のマシンなので、localhostのみになります。ファイル名は適当にhostsにします。

echo 'localhost' > hosts

次にplaybookに実行する内容を書いていきます。 ファイル名はplaybook.ymlとします。

・playbook.yml

- hosts: localhost # 実行対象のホストを指定
  connection: local
  gather_facts: no
  sudo: no
  vars: # インストールしたいものや追加したいリポジトリをここで定義
    homebrew_taps:
        - homebrew/php
        - caskroom/cask
        - sanemat/font
    homebrew_packages:
        - { name: ansible }
        - { name: brew-cask }
        - { name: colordiff }
        - { name: composer }
        - { name: coreutils }
        - { name: git }
        - { name: hub }
        - { name: jq }
        - { name: nkf }
        - { name: php56, install_options: with-homebrew-apxs }
        - { name: php56-mcrypt }
        - { name: php56-xdebug }
        - { name: phpunit }
        - { name: python }
        - { name: reattach-to-user-namespace }
        - { name: rename }
        - { name: sqlite }
        - { name: tig }
        - { name: tree }
        - { name: unrar }
        - { name: wget }
        - { name: sl }
        - { name: zsh }
        - { name: zsh-completions }
        - { name: peco }
        - { name: subversion }
    homebrew_cask_packages:
        - { name: sourcetree }
        - { name: google-chrome }
        - { name: atom }
        - { name: virtualbox }
        - { name: firefox }
        - { name: intellij-idea }
        - { name: vagrant }
  tasks:  # 実行したい動作をtasksに記述
    # brew tap
    - name: brew tap
      homebrew_tap: tap="{{ item }}" state=present
      with_items: homebrew_taps
    # update homebrew
    - name: brew update
      homebrew: update_homebrew=yes
    # brew
    - name: install brew package
      homebrew: >
        name={{ item.name }}
        state={{ item.state | default('latest') }}
        install_options={{
          item.install_options | default() | join(',')
          if item.install_options is not string
          else item.install_options
        }}
      with_items: homebrew_packages
      register: brew_result
    # cask
    - name: install homebrew-cask
      homebrew: name=brew-cask state=latest
    - name: install cask package
      homebrew_cask: name={{ item.name }} state={{ item.state|default('installed') }}
      with_items: homebrew_cask_packages
      register: cask_result
    # oh-my-zsh
    - name: install oh-my-zsh
      shell: curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
      args:
        creates: ~/.oh-my-zsh/
    # Ricty
    - name: install xquartz
      homebrew_cask: name=xquartz
    - name: install fontforge
      homebrew: name=fontforge
    - name: install Ricty
      homebrew: name=ricty
    - name: copy font file
      shell: cp -f $(brew --cellar ricty)/*/share/fonts/Ricty*.ttf ~/Library/Fonts/
      args:
        creates: ~/Library/Fonts/Ricty-Bold.ttf
      notify: run fc-cache # タスク実行後に実行する動作

  handlers: # タスク実行後に実行したい動作をhandlersに定義
    - name: run fc-cache
      shell: fc-cache -vf

内容としてはAnsibleのhomebrewモジュールを使ってソフトをインストールするものになってます。 playbookを作成したら、

HOMEBREW_CASK_OPTS="--appdir=/Applications" ansible-playbook -i hosts -vv playbook.yml

でplaybookを実行します。

HOMEBREW_CASK_OPTSを設定している理由としては、mawatari.jp HomebrewとAnsibleでMacの開発環境構築を自動化する でものによって/Applications~/Applicationsシンボリックリンクを張ってしまうということが紹介されているので設定しています。 -iはインベントリファイルの指定するオプション、-vデバッグログの出力する情報量を指定するオプションになってます。 -vvで実行したタスクと実行結果がわかるくらいのログを表示してくれます。(最大で-vvvまで指定できる)

これで自動でplaybookの内容を実行してくれます。 後は前のマシンから持ってきたdotfilesを持って来れば終わりです。

思ったこと

個人で開発環境構築用のplaybookを作りましたが、チーム単位で作ったら幸せになれるだろうなあと思ったり。 apacheの設定やらで苦労したので・・・。

インタープリズム的「俺達私達の進捗を上げる25個前後のTips」 Advent Calendar 2015 - Qiita10日目の記事

インタープリズムのページ

PAGE TOP