09 - Sudo / Privilege Escalation¶
Learn how to execute commands and operations with elevated privileges.
What You'll Learn¶
- Using
become: truefor sudo operations - Providing sudo password via CLI
- System-level operations
- OS-specific privileged operations
Quick Start¶
# Interactive prompt (recommended)
mooncake run --config config.yml --ask-become-pass
# Or using short flag
mooncake run --config config.yml -K
# Preview what would run with sudo
mooncake run --config config.yml -K --dry-run
⚠️ Warning: This example contains commands that require root privileges. Review the config before running!
What It Does¶
- Runs regular command (no sudo)
- Runs privileged command with sudo
- Updates package list (Linux)
- Installs system packages
- Creates system directories and files
Key Concepts¶
Basic Sudo¶
Add become: true to run with sudo:
Providing Password¶
Four ways to provide sudo password (mutually exclusive):
1. Interactive prompt (recommended):
Password is hidden while typing. Most secure option.2. File-based (secure for automation):
echo "mypassword" > ~/.mooncake/sudo_pass
chmod 0600 ~/.mooncake/sudo_pass
mooncake run --config config.yml --sudo-pass-file ~/.mooncake/sudo_pass
3. SUDO_ASKPASS (password manager integration):
Uses external helper program for password input.4. Command line (insecure, not recommended):
⚠️ WARNING: Password visible in shell history and process list. Requires explicit--insecure-sudo-pass flag.
Security Features:
- Passwords are automatically redacted from all log output
- Only one password method can be used at a time
- File permissions are strictly validated
Which Operations Need Sudo?¶
Typically require sudo:
- Package management (
apt,yum,dnf) - System file operations (
/etc,/opt,/usr/local) - Service management (
systemctl) - User/group management
- Mounting filesystems
- Network configuration
Don't require sudo:
- User-space operations
- Home directory files
/tmpdirectory- Homebrew on macOS (usually)
File Operations with Sudo¶
Create system directories:
Create system files:
- name: Create system config
file:
path: /etc/myapp/config.yml
state: file
content: "config: value"
become: true
OS-Specific Sudo¶
# Linux package management
- name: Install package (Linux)
shell: apt install -y curl
become: true
when: os == "linux" and package_manager == "apt"
# macOS typically doesn't need sudo for homebrew
- name: Install package (macOS)
shell: brew install curl
when: os == "darwin"
Security Considerations¶
- Review before running - Check what commands will execute with sudo
- Use dry-run - Preview with
--dry-runfirst - Minimize sudo usage - Only use on steps that require it
- Specific commands - Don't use
become: trueon untrusted commands - Password input - Use interactive prompt or file-based methods, avoid CLI flag
- Password redaction - Passwords are automatically redacted from logs (debug, stdout, stderr)
- File permissions - If using
--sudo-pass-file, ensure 0600 permissions - Platform support - Only works on Linux and macOS (explicitly fails on Windows)
Common Use Cases¶
Package Installation¶
- name: Install system packages
shell: |
apt update
apt install -y nginx postgresql
become: true
when: os == "linux"
System Service Setup¶
- name: Create systemd service
template:
src: ./myapp.service.j2
dest: /etc/systemd/system/myapp.service
mode: "0644"
become: true
- name: Enable service
shell: systemctl enable myapp
become: true
System Directory Setup¶
- name: Create application directories
file:
path: "{{ item }}"
state: directory
mode: "0755"
become: true
with_items:
- /opt/myapp
- /etc/myapp
- /var/log/myapp
Testing¶
# Preview what will run with sudo
mooncake run --config config.yml -K --dry-run
# Run with sudo
mooncake run --config config.yml -K
# Check created system files
sudo ls -la /opt/myapp/
# Verify password redaction in debug logs
mooncake run --config config.yml -K --log-level debug | grep -i password
# Should show [REDACTED] instead of actual password
Troubleshooting¶
"step requires sudo but no password provided"
- Provide password using
--ask-become-pass,--sudo-pass-file, orSUDO_ASKPASS
"--sudo-pass requires --insecure-sudo-pass flag"
- CLI password flag requires explicit security acknowledgment
- Use
--ask-become-passinstead (more secure)
"password file must have 0600 permissions"
- Fix permissions:
chmod 0600 /path/to/password/file - Verify ownership:
ls -l /path/to/password/file
"only one password method can be specified"
- Remove conflicting password flags
- Use only one of:
--ask-become-pass,--sudo-pass-file, or--sudo-pass
"become is not supported on windows"
- Privilege escalation only works on Linux and macOS
- Use platform-specific conditionals with
when
Permission denied without sudo
- Add
become: trueto the step
Command not found
- Check if command exists:
which <command> - Some commands need full paths with sudo
Next Steps¶
→ Continue to 10-multi-file-configs to learn about organizing large configurations.