継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

Macbook の初期セットアップを Ansible で取り組む(2020年9月版)

タダです.

業務で使う PC を変えたのでそのセットアップを勉強の一環で Ansible でやってみました.セットアップしたのは次のマシンです.今回は備忘録として行った内容を記事に書いていきます.

準備

準備として brew と Ansible のインストールを行います.

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
% brew install ansible

Ansible のバージョンは 2.9.11 です.

% ansible --version
ansible 2.9.11

定義ファイル

Ansible で導入するアプリケーションや導入したいコマンドを列挙していく Playbook ファイルと Ansible の実行先マシンを指定するインベントリのファイルを作成して実行しました.

setup.yml

- name: Mac Setup Setting
  hosts: 127.0.0.1
  connection: local
  vars:
    brew_taps:
      - homebrew/core
      - homebrew/cask
    brew_cask_apps:
      - clipy
      - docker
      - firefox
      - google-japanese-ime
      - iterm2
      - authy
    brew_packages:
      - git
      - jq
      - tree
      - wget

  tasks:
    - name: Homebrew tap
      homebrew_tap:
        name: "{{ item }}"
        state: "present"
      with_items: "{{ brew_taps }}"

    - name: Install Homebrew Cask Packages
      homebrew_cask:
        name: "{{ item }}"
        state: "present"
        install_options: "appdir=/Applications"
        accept_external_apps: yes
      with_items: "{{ brew_cask_apps }}"

    - name: Update Homebrew
      homebrew:
        update_homebrew: yes

    - name: Install Homebrew Packages
      homebrew:
        name: "{{ brew_packages }}"
        state: "present"

実行結果

PLAY [Mac Setup Setting] ********************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************
[WARNING]: Platform darwin on host 127.0.0.1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [127.0.0.1]

TASK [Homebrew tap] *****************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item=homebrew/core)
changed: [127.0.0.1] => (item=homebrew/cask)

TASK [Install Homebrew Cask Packages] ***********************************************************************************************************************************************************
changed: [127.0.0.1] => (item=clipy)
changed: [127.0.0.1] => (item=docker)
changed: [127.0.0.1] => (item=firefox)
Password:
changed: [127.0.0.1] => (item=google-japanese-ime)
changed: [127.0.0.1] => (item=iterm2)
changed: [127.0.0.1] => (item=authy)

TASK [Update Homebrew] **************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Install Homebrew Packages] ****************************************************************************************************************************************************************
changed: [127.0.0.1]

PLAY RECAP **************************************************************************************************************************************************************************************
127.0.0.1                  : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

まとめ

業務 PC を新調したのでそのセットアップに Ansible を使ってみました.構成管理で Ansible を使っていきたいのでこの機会をきっかけに使っていければと思います.

参考記事

qiita.com