Published on

Ansible Playbook for your Macbook Homebrew Packages



In this tutorial I will demonstrate how to use Ansible for Homebrew Configuration Management. The aim for using Ansible to manage your homebrew packages helps you to have a consistent list of packages on your macbook.

For me personally, when I get a new laptop it's always a mission to get the same packages installed as what I had before, and ansible solves that for us to have all our packages defined in configuration management.

Install Ansible

Install ansible with python and pip:

python3 -m pip install ansible==4.9.0

Ansible Configuration

Create the ansible.cfg configuration file:

inventory = inventory.ini
deprecation_warnings = False

Our inventory.ini will define the information about our target host, which will be localhost as we are using ansible to run against our local target which is our macbook:

my.laptop  ansible_connection=local

ansible_python_interpreter = /usr/bin/python3

Ansible Playbook

Our playbook homebrew.yaml will define the tasks to add the homebrew taps, cask packages and homebrew packages. You can change the packages as you desire, but these are the ones that I use:

- hosts: localhost
  name: Macbook Playbook
  gather_facts: False
    TFENV_ARCH: amd64
    - name: Ensures taps are present via homebrew
        name: "{{ item }}"
        state: present
        - hashicorp/tap

    - name: Ensures packages are present via homebrew cask
        name: "{{ item }}"
        state: present
        install_options: 'appdir=/Applications'
        - visual-studio-code
        - multipass
        - spotify

    - name: Ensures packages are present via homebrew
        name: "{{ item }}"
        path: "/Applications"
        state: present
        - openssl
        - readline
        - sqlite3
        - xz
        - zlib
        - jq
        - yq
        - wget
        - go
        - kubernetes-cli
        - fzf
        - sshuttle
        - hugo
        - helm
        - kind
        - awscli
        - gnupg
        - kubectx
        - helm
        - stern
        - terraform
        - tfenv
        - pyenv 
        - jsonnet
      ignore_errors: yes
        - packages

Deploy Playbook

Now you can run the playbook using:

ansible-playbook homebrew.yaml

Source Code

The code can be found in my github repository:

Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscrube to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee