LoginSignup
49
49

More than 5 years have passed since last update.

Macの環境構築をAnsibleに任せる

Last updated at Posted at 2016-06-13

今までMacで開発環境を構築するたびにブログなどに書いておいた一連の手順を追いながらひとつひとつ作業していましたが、それも面倒なのでこれからは「動くドキュメント」としてAnsibleのPlaybookを書いておいてそれを流すだけで構築できるようにしようと思います。

事前準備

まずはAnsible自体をインストールしなければならないので、そのために自分の場合はhomebrewを入れておきます。homebrewを入れるためにXcodeとXcodeのCommand Line Toolを入れておきます。

ローカル環境構築の設定1

hosts.ini
[default]
localhost
playbook.yml
- hosts: localhost
  connection: local

ローカル環境でよく使いそうなmodule

homebrew

Ansibleにはhomebrewモジュールがあり、homebrewでインストールするライブラリを管理できます。

playbook.yml
- name: Update homebrew itself first
  homebrew: update_homebrew=yes
playbook.yml
- name: Install homebrew packages
  homebrew: name={{ item }}
  with_items:
    - git
    - ag
    - tree

homebrew_cask

homebrew caskを使えばDropboxやEvernoteなど、GUIアプリケーションもコマンドラインから管理できます。そしてbrew caskのモジュールもAnsibleには用意されています。

下の例では、まずhomebrew_tapでhomebrew cask用のリポジトリを追加し、その後に各アプリケーションをインストールしています。

playbook.yml
- name: Add homebrew tap repositories
  homebrew_tap: tap={{ item }} state=present
  with_items: 
    - caskroom/cask
playbook.yml
- name: Install Homebrew cask packages
  homebrew_cask: name={{ item }}
  with_items:
    - dropbox
    - evernote
    - coteditor

git

gitはサーバー環境設定でもよく使うと思いますが、 ローカルでもdotfileをpullしてきたりgithub上のリポジトリをcloneしてきたりと 使う場面が多いと思います。

下の例では、自分のgithubアカウントなどにあらかじめpushしておいたdotfilesをpullし、更新があればホームディレクトリにコピーしています。 force=noとなっているので既に存在していればコピーはされないようになっていますがこの辺は好みですかね。

playbook.yml
- name: Pull dotfiles
  git: repo=git@github.com:foo/dotfiles.git dest=/path.to/tmp/dotfiles
  register: pull_dotfiles_result
playbook.yml
- name: Copy dotfiles
  copy: src=/path.to/tmp/dotfiles/{{ item }} dest=~/{{ item }} force=no
  with_items:
    - '.bashrc'
    - '.vimrc'
    - '.gitconfig'
  when: pull_dotfiles_result | changed

そのほかやりたい事

Karabinerの設定を同期したい

Macのキーボード設定の定番となっているKarabinerも同期したいと思います。事前準備として、あらかじめKarabinerの設定をスクリプト化して、githubなどに上げておきます。

/Applications/Karabiner.app/Contents/Library/bin/karabiner export > /path.to/karabiner-import.sh

このスクリプトを実行すれば設定をimportできるのでansibleのscriptモジュールを使って実行します。

playbook.yml
- name: Pull karabiner settings
  git: repo=git@github.com:foo/karabiner-settings.git dest=/path.to/tmp/karabiner version=master
  register: pull_karabiner_settings_result
playbook.yml
- name: Import Karabiner settings
  script: /path.to/tmp/karabiner/karabiner-import.sh
  when: pull_karabiner_settings_result | changed

システム環境設定を変更したい

トラックパッドの設定やDockの設定など、Macのシステム環境設定を変更したいですが、今のところまだできていません。Apple Scriptでできたりしないか検討中です。

参考


  1. ローカルで動かす場合はこれらの記述はなくても動くかもしれませんが未検証です 

49
49
1

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
49
49