
AnsibleでMacの環境構築をしてみた
2021.12.20
AnsibleでMacの環境構築をしたい!

(株)ライトコードの北村です!
過去にAnsibleでMacの環境構築するネタをネットで拝見してまして、いつかやってみたいな~!と思ってました。
この度、会社からMac支給される運びとなり、これはビックウェーブに乗るしかないなと思い、ついに、機会が来たのでやってみます!
ハードル上げちゃうとあれなんで、よく使うツールが数コマンドで使えるようになれば良いなぁ、と
Ansible(アンシブル)とは?
Ansibleとは、構成管理ツールの1つで、主にサーバ構築に使われます。
(インフラ屋さんがよく使う認識)
「使うと何が嬉しいのか」を簡単に言うと『冪等性(べきとうせい)の担保』。
そのため、複数台のサーバを同じ構成で設定する場合とか便利です。
類似ツールとしてChef、Puppetと言うのがあります。
しかし、Ansibleは、yaml形式で設定を記述することから、記述内容を読める人が多く、かつ学習コストが低いというメリットがあります。
(Chef、Puppetは、独自形式に設定を記載)
Macを新調する度、Ansible使えば、ばっちりツールが使えるようにセットアップされる(はずです!)

Macにインストールしたいツールたちはこちら
CLI
- zsh
- git
- wget
- vim
- tree
- openssl
GUI
- alfred
- iterm2
- Google日本語入力
- ブラウザ(chrome、firefox)
- エディタ(vscode、CotEditor)
- vagrant
- virtualbox
- slack
- postman
- teamsql
- フォント(ricty-diminished)
必要ソフトのインストール
まずは、必要なソフトをインストールしていきます。
Xcode
素直にAppStoreから。重たいんですよね。。。
Homebrew
準備/インストールをしていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | kitamuranoMacBook-Pro:setup tamken$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ==> This script will install: /usr/local/bin/brew (中略) Press RETURN to continue or any other key to abort # む。素直にリターンキーを押す ==> /usr/bin/sudo /bin/mkdir -p /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/share /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks Password: # む。素直にパスワードを入力する (中略) ==> Installation successful! ==> Homebrew has enabled anonymous aggregate formulae and cask analytics. Read the analytics documentation (and how to opt-out) here: https://docs.brew.sh/Analytics ==> Homebrew is run entirely by unpaid volunteers. Please consider donating: https://github.com/Homebrew/brew#donations ==> Next steps: - Run `brew help` to get started - Further documentation: https://docs.brew.sh |
インストール完了!
いちお確認
1 2 3 | kitamuranoMacBook-Pro:setup tamken$ brew -v Homebrew 2.0.4 Homebrew/homebrew-core (git revision 07d6; last commit 2019-03-12) |
OKです!(バージョン古いですね。ご愛嬌)
Ansible
いれたてのHomebrewでインストールしていきます。
1 2 3 4 5 6 7 8 | # 検索してみる kitamuranoMacBook-Pro:setup tamken$ brew search ansible ==> Formulae ansible ansible-lint ansible@2.0 ansible-cmdb ansible@1.9 terraform-provisioner-ansible ==> Casks homebrew/cask/ansible-dk |
いろいろ出てきたけど「ansible」をインストール。
1 2 3 4 5 | kitamuranoMacBook-Pro:setup tamken$ brew install ansible Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). (中略) |
インストール開始!
1 2 3 4 5 6 7 8 9 10 11 | (中略) ==> Installing dependencies for ansible: libyaml, openssl, gdbm, readline, sqlite, xz and python (中略) ==> Pouring openssl-1.0.2q.mojave.bottle.tar.gz ==> Caveats (中略) 🍺 /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.1MB ==> Installing ansible dependency: gdbm ==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1.mojave.bottle.1.tar.gz … ######################################################################## 100.0% |
あ゛。
openssl、はansibleの依存関連でインストールされる模様。
なのでplaybook(のvars)に、opensslの指定は不要ですね。
(でも、記載があっても影響無いからそのままにしとく)
1 2 3 | kitamuranoMacBook-Pro:setup tamken$ ansible --version ansible 2.7.8 (中略) |
インストール完了です!
playbook等設定ファイルの作成
続いて、playbook等のAnsibleの設定ファイルを作成します。
playbookは、平たくいえば構築手順を定義したファイルでyaml形式で記述します。
Ansibleはこのファイルを元に環境を構築していくみたいですね。
ファイル構成
以下な感じでいきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - setup - inventory - setup.yml - roles - homebrew - tasks - main.yml - vars - main.yml - homebrew_cask - tasks - main.yml - vars - main.yml |
インベントリ
環境構築対象サーバの情報を記載。自分相手なのでlocalhostを指定します。
(~/setup/inventory)
1 2 | [localhost] 127.0.0.1 |
setup.yml
playbook。
エントリポイントです。
(~/setup/setup.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 | --- # file ~/setup/setup.yml - name: mac setup hosts: localhost connection: local # sudo実行有無(しないのでno) become: no # 対象サーバの情報収集(しないのでno) gather_facts: no # タスクの呼び出し roles: - homebrew - homebrew_cask |
roles/homebrew
CLIツールのインストール設定を記載
- /tasks/main.yml … タスク
- /vars/main.yml … 変数(ここにインストールしたいツールを定義)
(./setup/roles/homebrew/tasks/main.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | --- # file: ~/setup/roles/homebrew/tasks/main.yml - block: - name: homebrew update/upgrade homebrew: update_homebrew: true upgrade_all: yes - name: homebrew tap homebrew_tap: name: "{{ item }}" state: present with_items: "{{ taps }}" - name: homebrew install homebrew: name: "{{ item.name }}" state: "{{ item.state }}" with_items: "{{ packages }}" tags: - homebrew |
(./setup/roles/homebrew/vars/main.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | --- # file: ~/setup/roles/homebrew/vars/main.yml taps: - homebrew/cask - homebrew/cask-versions - homebrew/cask-fonts - homebrew/core packages: - { name: zsh, state: present } - { name: git, state: present } - { name: wget, state: present } - { name: vim, state: present } - { name: tree, state: present } - { name: openssl, state: present } |
roles/homebrew_cask
GUIツールのインストール設定を記載
- /tasks/main.yml … タスク
- /vars/main.yml … 変数(ここにインストールしたいツールを定義)
(./setup/roles/homebrew_cask/tasks/main.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | --- # file: ~/setup/roles/homebrew_cask/tasks/main.yml - block: - name: homebrew cask install environment: HOMEBREW_CASK_OPTS: --appdir=/Applications homebrew_cask: name: "{{ item.name }}" state: "{{ item.state }}" with_items: "{{ packages }}" tags: - homebrew_cask |
(./setup/roles/homebrew_cask/vars/main.yml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | --- # file: ~/setup/roles/homebrew_cask/vars/main.yml packages: - { name: alfred, state: present } - { name: iterm2, state: present } - { name: google-japanese-ime, state: present } - { name: google-chrome, state: present } - { name: firegox, state: present } - { name: visual-studio-code, state: present } - { name: coteditor, state: present } - { name: vagrant, state: present } - { name: virtualbox, state: present } - { name: slack, state: present } - { name: postman, state: present } - { name: teamsql, state: present } - { name: font-ricty-diminished, state: present } |
playbookの実行
準備完了、setupディレクトリへ移動し以下実行します。
1 2 3 4 5 6 | # setupディレクトリへ移動 $ cd ~/setup # 構文チェック(一応) $ ansible-playbook --syntax-check setup.yml -i inventory # playbook実行 $ ansible-playbook setup.yml -i inventory |
ansible実行
機は熟しました。
ついに、ansible実行します。
構文チェック
まずは、構文チェックまで。
1 2 3 4 5 6 7 | # ディレクトリ移動 kitamuranoMacBook-Pro:setup tamken$ cd ~/setup # 構文チェック kitamuranoMacBook-Pro:setup tamken$ ansible-playbook --syntax-check setup.yml -i inventory playbook: setup.yml |
む。
なにも出ない(分かりづらいですが、何も出なければ構文エラーは無い模様)。
続いて本番
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | itamuranoMacBook-Pro:setup tamken$ ansible-playbook setup.yml -i inventory PLAY [mac setup] **************************************************************************************************************************************************** TASK [homebrew : homebrew update/upgrade] *************************************************************************************************************************** changed: [127.0.0.1] (中略) TAP TASK [homebrew : homebrew tap] ************************************************************************************************************************************** changed: [127.0.0.1] => (item=homebrew/cask) changed: [127.0.0.1] => (item=homebrew/cask-versions) changed: [127.0.0.1] => (item=homebrew/cask-fonts) ok: [127.0.0.1] => (item=homebrew/core) |
CLI群
1 2 3 4 5 6 7 8 | TASK [homebrew : homebrew install] ********************************************************************************************************************************** changed: [127.0.0.1] => (item={'name': 'zsh', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'git', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'wget', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'vim', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'tree', 'state': 'present'}) ok: [127.0.0.1] => (item={'name': 'openssl', 'state': 'present'}) # opensslはインストール済なので「ok」でok |
GUI群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | TASK [homebrew_cask : homebrew cask install] ************************************************************************************************************************ changed: [127.0.0.1] => (item={'name': 'alfred', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'iterm2', 'state': 'present'}) Password: # む。パスワード聞かれる(おとなしく入力) changed: [127.0.0.1] => (item={'name': 'google-japanese-ime', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'google-chrome', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'firefox', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'visual-studio-code', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'coteditor', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'vagrant', 'state': 'present'}) # いい感じ failed: [127.0.0.1] (item={'name': 'virtualbox', 'state': 'present'}) => {"changed": false, "item": {"name": "virtualbox", "state": "present"}, "msg": "Updating Homebrew... # あら changed: [127.0.0.1] => (item={'name': 'slack', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'postman', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'teamsql', 'state': 'present'}) changed: [127.0.0.1] => (item={'name': 'font-ricty-diminished', 'state': 'present'}) to retry, use: --limit @/Users/tamken/setup/setup.retry PLAY RECAP ********************************************************************************************************************************************************** 127.0.0.1 : ok=3 changed=3 unreachable=0 failed=1 |
virtualboxが失敗、以外はOKな模様。
(ちなみに上記実行後のbrew list)
1 2 3 4 5 6 7 8 9 10 | # CLI kitamuranoMacBook-Pro:setup tamken$ brew list ansible git libyaml openssl perl ruby vim zsh gdbm libidn2 lua pcre python sqlite wget gettext libunistring ncurses pcre2 readline tree xz # GUI kitamuranoMacBook-Pro:setup tamken$ brew cask list alfred firefox google-chrome iterm2 slack vagrant coteditor font-ricty-diminished google-japanese-ime postman teamsql visual-studio-code |
virtualboxはカーネルを書き換えるため警告がでるとのこと。
[システム環境設定]>[セキュリティとプライバシー]の[一般]タブを選択 → 右下の「許可」ボタン(「Oracle〜のシステムソフトウェアの読み込みがブロックされました。」が表示されている箇所)
を押す必要がある模様。
許可して、もう一回ansible実行。
1 2 3 4 5 6 7 8 | kitamuranoMacBook-Pro:setup tamken$ ansible-playbook --syntax-check setup.yml -i inventory (中略) Password: # パスワード聞かれる changed: [127.0.0.1] => (item={'name': 'virtualbox', 'state': 'present'}) (中略) PLAY RECAP ********************************************************************************************************************************************************** 127.0.0.1 : ok=4 changed=1 unreachable=0 failed=0 |
はい、エラーなし。
1 2 3 | kitamuranoMacBook-Pro:setup tamken$ brew cask list alfred coteditor font-ricty-diminished google-japanese-ime postman teamsql virtualbox cheatsheet firefox google-chrome iterm2 slack vagrant visual-studio-code |
virtualbox入った!
以上で、ansibleでMacの環境構築できました!(インストールしたソフトを起動して確認)
オススメ本
さいごに
ansible化するメリットは、Mac変更する際にソフト何入れればいーのか一々旧マシン調べなくて良いのと、ちまちまコマンド打たなくて良いの2点に尽きるかと思いました。
(playbook等をバージョン管理等しているのが前提)
機会あれば、ぜひansibleでMacの環境構築してみてください!
こちらの記事もオススメ!

https://github.com/tamken/mac-brew-ansible
【追記】
teamsqlオワコンでしたorz。かわりにdbeaver入れました。
書いた人はこんな人
IT技術10月 27, 2023LocalStackを使ってファンアウトしてみた
IT技術6月 13, 2023【ISUCON部】ゆうれい部員がやってきた!
IT技術3月 17, 2022どんなCSVも楽々DBに取り込めるようにしてみた
IT技術6月 19, 2019AnsibleでMacの環境構築をしてみた