Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a platform check to homebrew role #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 4 additions & 193 deletions roles/homebrew/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,194 +1,5 @@
---
- name: Determine Homebrew ownership variables
set_fact:
homebrew_user: '{{ homebrew_user | default(ansible_user_id) }}'
homebrew_group: '{{ homebrew_group | default(ansible_user_gid) }}'

# Homebrew setup prerequisites.
- name: Ensure Homebrew parent directory has correct permissions (M1).
file:
path: "{{ homebrew_prefix }}"
owner: "{{ homebrew_user }}"
state: directory
become: true
when: ansible_machine == 'arm64'

- name: Ensure Homebrew parent directory has correct permissions (Intel).
when: ansible_machine == 'x86_64'
block:
- name: Ensure Homebrew parent directory has correct permissions (MacOS >= 10.13).
file:
path: "{{ homebrew_prefix }}"
owner: root
state: directory
become: true
when: "ansible_distribution_version is version('10.13', '>=')"

- name: Ensure Homebrew parent directory has correct permissions (MacOS < 10.13).
file:
path: "{{ homebrew_prefix }}"
owner: root
group: admin
state: directory
mode: "0775"
become: true
when: "ansible_distribution_version is version('10.13', '<')"

- name: Check if homebrew already exists.
stat:
path: "{{ homebrew_brew_bin_path }}/brew"
register: pre_installed_brew

- name: Ensure Homebrew directory exists.
file:
path: "{{ homebrew_install_path }}"
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
state: directory
mode: "0775"
become: true

# Clone Homebrew.
- name: Ensure Homebrew is installed.
git:
repo: "{{ homebrew_repo }}"
version: master
dest: "{{ homebrew_install_path }}"
update: false
depth: 1
become: true
become_user: "{{ homebrew_user }}"
when: not pre_installed_brew.stat.exists

# Adjust Homebrew permissions.
- name: Ensure proper permissions and ownership on homebrew_brew_bin_path dirs.
file:
path: "{{ homebrew_brew_bin_path }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
mode: "0775"
become: true

- name: Ensure proper ownership on homebrew_install_path subdirs.
file:
path: "{{ homebrew_install_path }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
recurse: true
follow: false
become: true

# Place brew binary in proper location and complete setup.
- name: Check if homebrew binary is already in place.
stat:
path: "{{ homebrew_brew_bin_path }}/brew"
register: homebrew_binary
check_mode: false

- name: Symlink brew to homebrew_brew_bin_path.
file:
src: "{{ homebrew_install_path }}/bin/brew"
dest: "{{ homebrew_brew_bin_path }}/brew"
state: link
when: not homebrew_binary.stat.exists
become: true

- name: Add missing folder if not on Apple-chipset
set_fact:
homebrew_folders_base: "{{ homebrew_folders_base + ['Homebrew'] }}"
when: ansible_machine != 'arm64'

- name: Ensure proper homebrew folders are in place.
file:
path: "{{ homebrew_prefix }}/{{ item }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
become: true
loop: "{{ homebrew_folders_base + homebrew_folders_additional }}"

- name: Collect package manager fact.
setup:
filter: ansible_pkg_mgr

- name: Perform brew installation.
# Privilege escalation is only required for inner steps when
# the `homebrew_user` doesn't match the `ansible_user_id`
become: "{{ (homebrew_user != ansible_user_id) | bool }}"
become_user: "{{ homebrew_user }}"
block:
- name: Force update brew after installation.
command: "{{ homebrew_brew_bin_path }}/brew update --force"
when: not pre_installed_brew.stat.exists

- name: Where is the cache?
command: "{{ homebrew_brew_bin_path }}/brew --cache"
register: homebrew_cache_path
changed_when: false
check_mode: false

# Tap.
- name: Ensure configured taps are tapped.
homebrew_tap:
tap: '{{ item.name | default(item) }}'
url: '{{ item.url | default(omit) }}'
state: present
loop: "{{ homebrew_taps }}"

# Cask.
- name: Ensure blacklisted cask applications are not installed.
homebrew_cask:
name: "{{ item }}"
state: absent
sudo_password: "{{ ansible_become_password | default(omit) }}"
loop: "{{ homebrew_cask_uninstalled_apps }}"

- name: Install configured cask applications.
homebrew_cask:
name: "{{ item.name | default(item) }}"
state: present
install_options: "{{ item.install_options | default('appdir=' + homebrew_cask_appdir) }}"
accept_external_apps: "{{ homebrew_cask_accept_external_apps }}"
sudo_password: "{{ ansible_become_password | default(omit) }}"
loop: "{{ homebrew_cask_apps }}"
notify:
- Clear homebrew cache

# Brew.
- name: Ensure blacklisted homebrew packages are not installed.
homebrew:
name: "{{ item }}"
state: absent
loop: "{{ homebrew_uninstalled_packages }}"

- name: Ensure configured homebrew packages are installed.
homebrew:
path: "{{ homebrew_brew_bin_path }}"
name: "{{ item.name | default(item) }}"
install_options: "{{ item.install_options | default(omit) }}"
state: "{{ item.state | default('present') }}"
loop: "{{ homebrew_installed_packages }}"
notify:
- Clear homebrew cache

- name: Upgrade all homebrew packages (if configured).
homebrew:
update_homebrew: true
upgrade_all: true
when: homebrew_upgrade_all_packages
notify:
- Clear homebrew cache

- name: Check for Brewfile.
stat:
path: "{{ homebrew_brewfile_dir }}/Brewfile"
register: homebrew_brewfile
check_mode: false

- name: Install from Brewfile.
command: "{{ homebrew_brew_bin_path }}/brew bundle"
args:
chdir: "{{ homebrew_brewfile_dir }}"
when: homebrew_brewfile.stat.exists and homebrew_use_brewfile
- name: Set up Homebrew on macOS only
when: ansible_os_family == "Darwin"
ansible.builtin.include_tasks:
file: setup_homebrew.yml
194 changes: 194 additions & 0 deletions roles/homebrew/tasks/setup_homebrew.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
- name: Determine Homebrew ownership variables
set_fact:
homebrew_user: '{{ homebrew_user | default(ansible_user_id) }}'
homebrew_group: '{{ homebrew_group | default(ansible_user_gid) }}'

# Homebrew setup prerequisites.
- name: Ensure Homebrew parent directory has correct permissions (M1).
file:
path: "{{ homebrew_prefix }}"
owner: "{{ homebrew_user }}"
state: directory
become: true
when: ansible_machine == 'arm64'

- name: Ensure Homebrew parent directory has correct permissions (Intel).
when: ansible_machine == 'x86_64'
block:
- name: Ensure Homebrew parent directory has correct permissions (MacOS >= 10.13).
file:
path: "{{ homebrew_prefix }}"
owner: root
state: directory
become: true
when: "ansible_distribution_version is version('10.13', '>=')"

- name: Ensure Homebrew parent directory has correct permissions (MacOS < 10.13).
file:
path: "{{ homebrew_prefix }}"
owner: root
group: admin
state: directory
mode: "0775"
become: true
when: "ansible_distribution_version is version('10.13', '<')"

- name: Check if homebrew already exists.
stat:
path: "{{ homebrew_brew_bin_path }}/brew"
register: pre_installed_brew

- name: Ensure Homebrew directory exists.
file:
path: "{{ homebrew_install_path }}"
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
state: directory
mode: "0775"
become: true

# Clone Homebrew.
- name: Ensure Homebrew is installed.
git:
repo: "{{ homebrew_repo }}"
version: master
dest: "{{ homebrew_install_path }}"
update: false
depth: 1
become: true
become_user: "{{ homebrew_user }}"
when: not pre_installed_brew.stat.exists

# Adjust Homebrew permissions.
- name: Ensure proper permissions and ownership on homebrew_brew_bin_path dirs.
file:
path: "{{ homebrew_brew_bin_path }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
mode: "0775"
become: true

- name: Ensure proper ownership on homebrew_install_path subdirs.
file:
path: "{{ homebrew_install_path }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
recurse: true
follow: false
become: true

# Place brew binary in proper location and complete setup.
- name: Check if homebrew binary is already in place.
stat:
path: "{{ homebrew_brew_bin_path }}/brew"
register: homebrew_binary
check_mode: false

- name: Symlink brew to homebrew_brew_bin_path.
file:
src: "{{ homebrew_install_path }}/bin/brew"
dest: "{{ homebrew_brew_bin_path }}/brew"
state: link
when: not homebrew_binary.stat.exists
become: true

- name: Add missing folder if not on Apple-chipset
set_fact:
homebrew_folders_base: "{{ homebrew_folders_base + ['Homebrew'] }}"
when: ansible_machine != 'arm64'

- name: Ensure proper homebrew folders are in place.
file:
path: "{{ homebrew_prefix }}/{{ item }}"
state: directory
owner: "{{ homebrew_user }}"
group: "{{ homebrew_group }}"
become: true
loop: "{{ homebrew_folders_base + homebrew_folders_additional }}"

- name: Collect package manager fact.
setup:
filter: ansible_pkg_mgr

- name: Perform brew installation.
# Privilege escalation is only required for inner steps when
# the `homebrew_user` doesn't match the `ansible_user_id`
become: "{{ (homebrew_user != ansible_user_id) | bool }}"
become_user: "{{ homebrew_user }}"
block:
- name: Force update brew after installation.
command: "{{ homebrew_brew_bin_path }}/brew update --force"
when: not pre_installed_brew.stat.exists

- name: Where is the cache?
command: "{{ homebrew_brew_bin_path }}/brew --cache"
register: homebrew_cache_path
changed_when: false
check_mode: false

# Tap.
- name: Ensure configured taps are tapped.
homebrew_tap:
tap: '{{ item.name | default(item) }}'
url: '{{ item.url | default(omit) }}'
state: present
loop: "{{ homebrew_taps }}"

# Cask.
- name: Ensure blacklisted cask applications are not installed.
homebrew_cask:
name: "{{ item }}"
state: absent
sudo_password: "{{ ansible_become_password | default(omit) }}"
loop: "{{ homebrew_cask_uninstalled_apps }}"

- name: Install configured cask applications.
homebrew_cask:
name: "{{ item.name | default(item) }}"
state: present
install_options: "{{ item.install_options | default('appdir=' + homebrew_cask_appdir) }}"
accept_external_apps: "{{ homebrew_cask_accept_external_apps }}"
sudo_password: "{{ ansible_become_password | default(omit) }}"
loop: "{{ homebrew_cask_apps }}"
notify:
- Clear homebrew cache

# Brew.
- name: Ensure blacklisted homebrew packages are not installed.
homebrew:
name: "{{ item }}"
state: absent
loop: "{{ homebrew_uninstalled_packages }}"

- name: Ensure configured homebrew packages are installed.
homebrew:
path: "{{ homebrew_brew_bin_path }}"
name: "{{ item.name | default(item) }}"
install_options: "{{ item.install_options | default(omit) }}"
state: "{{ item.state | default('present') }}"
loop: "{{ homebrew_installed_packages }}"
notify:
- Clear homebrew cache

- name: Upgrade all homebrew packages (if configured).
homebrew:
update_homebrew: true
upgrade_all: true
when: homebrew_upgrade_all_packages
notify:
- Clear homebrew cache

- name: Check for Brewfile.
stat:
path: "{{ homebrew_brewfile_dir }}/Brewfile"
register: homebrew_brewfile
check_mode: false

- name: Install from Brewfile.
command: "{{ homebrew_brew_bin_path }}/brew bundle"
args:
chdir: "{{ homebrew_brewfile_dir }}"
when: homebrew_brewfile.stat.exists and homebrew_use_brewfile