この記事は Ansible Advent Calendar 2022 13日目の記事です。
はじめに
私が個人的に使用している Mac 製品に搭載されている macOS を Ansible で管理しています。Ansible で管理している範囲として、macOS の設定、Mac App Store のアプリケーション管理、Homebrew のパッケージ管理です。
Mac 製品を購入や macOS の初期化した際に、macOS を使いやすい状態へ持っていくために、Ansible を使用します。
事前準備
・Ansible をインストールする
macOS に Homebrew を使用して、Ansible をインストールします。Homebrew のインストール方法については、Homebrew の公式サイトを参考にしてください。
brew install ansible
・mas-cli をインストールする
Ansible を使用して、Mac App Store のアプリケーション管理を行うために、mas-cli をインストールします。
brew install mas
・ansible.cfg を作成して、下記の内容を設定する
interpreter_python に Mac で使用する Python のフルパスを設定します。
[defaults]
interpreter_python=/opt/homebrew/bin/python3.10
・hosts.ini を作成して、下記の内容を設定する
localhost
・playbook.yml を作成して、下記の内容を設定する
---
- name: update macOS
hosts: all
connection: local
gather_facts: false
vars:
# Mac App Store でインストールするソフトウェアを指定
var_mas:
- 539883307 # LINE
- 803453959 # Slack
# Homebrew に追加するリポジトリを指定
var_homebrew_tap:
- hashicorp/tap
# Homebrew でインストールするパッケージを指定
var_homebrew:
- awscli
- azure-cli
- cdktf
- consul
- packer
- terraform
- vault
# Homebrew Cask でインストールするパッケージを指定
var_homebrew_cask:
- docker
- firefox
- google-cloud-sdk
tasks:
# macOS
- name: Software Update
community.general.osx_defaults:
domain: /Library/Preferences/com.apple.SoftwareUpdate
key: AutomaticCheckEnabled
type: int
value: 1
become: true
tags:
- os
- name: Safari Debug Menu
community.general.osx_defaults:
domain: com.apple.Safari
key: IncludeInternalDebugMenu
type: bool
value: true
state: present
tags:
- os
# Mac App Store
- name: Upgrade all installed Mac App Store apps
community.general.mas:
upgrade_all: true
tags:
- mas
- name: Install Mac App Store apps
community.general.mas:
id: "{{ var_mas }}"
state: present
upgrade_all: true
tags:
- mas
# Homebrew
- name: Tap a Homebrew repository
community.general.homebrew_tap:
name: "{{ var_homebrew_tap }}"
state: present
tags:
- homebrew
- name: Install Homebrew
community.general.homebrew:
name: "{{ var_homebrew }}"
state: present
tags:
- homebrew
- name: Install Homebrew Cask
community.general.homebrew_cask:
name: "{{ var_homebrew_cask }}"
state: present
tags:
- homebrew
Ansible で macOS の管理を実施する
・Ansible を実行して、macOS の設定を行う
ansible-playbook playbook.yml -i hosts.ini --tags os
・Ansible を実行して、Mac App Store のソフトウェア管理を行う
ansible-playbook playbook.yml -i hosts.ini --tags mas
・Ansible を実行して、Homebrew のパッケージ管理を行う
ansible-playbook playbook.yml -i hosts.ini --tags homebrew
・タグを指定せずに全てのタスクを実行する
ansible-playbook playbook.yml -i hosts.ini
まとめ
非常に簡単ではありますが、Ansible を使用して macOS が管理できることを紹介しました。必要に応じてリファクタリング、変数の変更を実施して、Playbook を使用してください。
macOS の初期設定を手作業で実施することは、Mac を触れた方であれば難しくないと思います。ただ、手作業、繰り返す、定量作業であるため、自動化することで Mac を使用するまでの期間短縮を実現することができます。
Ansible を使用する方の参考になれば幸いです。
[appendix] macOS を管理するための Ansible モジュール
macOS を管理するために役立つ Ansible モジュールの一部を紹介します。macOS 専用モジュールは多くはありません。Linux OS の管理でよく使用するモジュールを、macOS でも使用することができます。
- macOS の管理でよく使用するモジュール
- osx_defaults module – Manage macOS user defaults
- mas module – Manage Mac App Store applications with mas-cli
- homebrew module – Package manager for Homebrew
- homebrew_cask module – Install and uninstall homebrew casks
- homebrew_tap module – Tap a Homebrew repository
- launchd module – Manage macOS services
- 上記以外
- copy module – Copy files to remote locations
- cron module – Manage cron.d and crontab entries
- file module – Manage files and file properties
- find module – Return a list of files based on specific criteria
- gather_facts module – Gathers facts about remote hosts
- get_url module – Downloads files from HTTP, HTTPS, or FTP to node
- git module – Deploy software (or files) from git checkouts
- git module – Deploy software (or files) from git checkouts
- pip module – Manages Python library dependencies
- tempfile module – Creates temporary files and directories