Okay, installing Homebrew on macOS using Puppet's exec resource is certainly possible, but it requires careful handling of idempotency and user context.
The standard Homebrew installation method involves downloading and running a shell script. You can wrap this script execution within an exec resource.
Here's how you can do it, along with considerations for making it idempotent and handling permissions correctly:
Understanding the Standard Install Command
The official Homebrew installation command is:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
This command fetches the script using curl and pipes it directly into bash for execution.
The Puppet exec Resource
The exec resource runs a command line. To make it suitable for Puppet, you need to add conditions so it only runs when necessary (idempotency).
Here's a basic exec definition:
exec { 'install homebrew':
command => '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
path => '/usr/bin:/bin:/usr/sbin:/sbin', # Essential paths for curl and bash
# Add conditions for idempotency and user context
}
Adding Idempotency (unless or onlyif)
You don't want to run the installer every time Puppet applies the catalog. A common way to check if Homebrew is installed is to see if the brew command exists.
The unless parameter runs a command, and the exec resource's command will only run if the unless command exits with a non-zero status (meaning the condition is false).
A robust check for the brew executable itself works well, as its location is predictable based on architecture:
- Intel Macs (
x86_64): /usr/local/bin/brew
- Apple Silicon Macs (
arm64): /opt/homebrew/bin/brew
We can check for either path:
exec { 'install homebrew':
command => '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
path => '/usr/bin:/bin:/usr/sbin:/sbin',
# Run this command *unless* brew is already found in either location
unless => 'test -e /usr/local/bin/brew || test -e /opt/homebrew/bin/brew',
}
The test -e <path> command checks if a file exists and exits 0 if it does. || is the logical OR operator. So, this unless command exits 0 if /usr/local/bin/brew exists OR /opt/homebrew/bin/brew exists. If neither exists, it exits non-zero, and the main command will run.
Handling User and Permissions
Homebrew is designed to be installed and run by a non-root user. Running the installer as root via Puppet's default execution context can lead to permission problems later.
The exec resource has a user parameter. You should specify the user who will own and manage the Homebrew installation.
Furthermore, the brew.sh script itself might need sudo permissions within the script to set up the /usr/local or /opt/homebrew directories initially if they don't exist or aren't writable by the user. When run non-interactively via exec user=..., the script won't be able to interactively prompt for sudo.
A more robust approach is to ensure the target directory exists and has the correct ownership before running the install script. We can determine the correct directory based on the system architecture using facter.
-
Get Architecture and Target Directory:
$brew_install_dir = $facts['architecture'] ? {
'arm64' => '/opt/homebrew',
'x86_64' => '/usr/local',
default => '/usr/local', # Default to Intel path if fact is missing or unexpected
}
# Replace 'myuser' and 'admin' with the actual user and group
$brew_user = 'myuser' # <<<< REPLACE THIS
$brew_group = 'admin' # <<<< REPLACE THIS (or the primary group of the user)
-
Ensure Target Directory Exists and has Correct Ownership:
file { $brew_install_dir:
ensure => directory,
owner => $brew_user,
group => $brew_group,
mode => '0775', # Typical permissions allowing owner/group read+write+execute, others read+execute
}
-
Run exec as the Target User and Require the Directory:
exec { 'install homebrew':
command => '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
path => '/usr/bin:/bin:/usr/sbin:/sbin',
user => $brew_user, # Execute as the specified user
unless => 'test -e /usr/local/bin/brew || test -e /opt/homebrew/bin/brew',
timeout => 600, # Allow up to 10 minutes for download/install
require => File[$brew_install_dir], # Ensure directory exists before running
}
Complete Example Code:
Combine the pieces into a manifest. Remember to replace 'myuser' and 'admin' with actual values appropriate for your environment.
# Determine the Homebrew installation directory based on architecture
$brew_install_dir = $facts['architecture'] ? {
'arm64' => '/opt/homebrew',
'x86_64' => '/usr/local',
default => '/usr/local', # Fallback
}
# Define the user and group who will own the Homebrew installation
# >>>>>> IMPORTANT: Replace 'myuser' and 'admin' with actual values <<<<<<
$brew_user = 'myuser'
$brew_group = 'admin'
# Ensure the target installation directory exists and has the correct permissions
file { $brew_install_dir:
ensure => directory,
owner => $brew_user,
group => $brew_group,
mode => '0775',
# If you needed to create parent directories as well (less common for /usr/local or /opt)
# recurse => true,
# force => true,
}
# Exec resource to run the Homebrew installation script
exec { 'install homebrew':
command => '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
# Standard paths where curl, bash, test, etc. are located
path => '/usr/bin:/bin:/usr/sbin:/sbin',
# Run the command as the specified user
user => $brew_user,
# This exec should only run if Homebrew is NOT already installed in either location
unless => 'test -e /usr/local/bin/brew || test -e /opt/homebrew/bin/brew',
# Set a timeout in case the download/install takes a long time
timeout => 600, # 10 minutes
# Ensure the target directory is set up before attempting installation
require => File[$brew_install_dir],
}
Important Considerations:
- Replace Placeholders: You must replace
'myuser' and 'admin' with the actual username and group on your macOS system that you want to own the Homebrew installation.
- User Existence: Ensure the user specified in
$brew_user exists on the target machine. You might need a separate Puppet resource (user and group) to manage this user's creation if it's not a standard system user.
- Internet Access: The machine needs internet access to download the install script and subsequently the Homebrew files.
- Passwordless Sudo: If the target directory (
/usr/local or /opt/homebrew) doesn't have correct permissions initially, the brew.sh script might still attempt to use sudo. Running exec user=... non-interactively can fail if the specified user requires a password for sudo. The file resource managing the directory permissions beforehand significantly mitigates this, but it's something to be aware of.
- Puppet Module: For a more robust and feature-rich solution, consider using a dedicated Homebrew Puppet module from the Puppet Forge (e.g.,
homebrew/brew). These modules are specifically designed to handle Homebrew installation and package management (brew install, brew upgrade, etc.) in an idempotent way and often manage users, paths, and architecture differences more gracefully than a raw exec.
Using the exec resource is viable for this specific task, but it requires careful consideration of dependencies, idempotency, and the unique requirements of the software you are installing (like Homebrew's preference for non-root installation).