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¶
cd examples/09-sudo
# Requires sudo password
mooncake run --config config.yml --sudo-pass <your-password>
# Preview what would run with sudo
mooncake run --config config.yml --sudo-pass <password> --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¶
Three ways to provide sudo password:
1. Command line (recommended):
2. Environment variable:
3. Interactive prompt: Some systems may prompt automatically (if configured)
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
- /tmp directory
- 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 handling - Be careful with password in shell history
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 --sudo-pass test --dry-run
# Run with sudo
mooncake run --config config.yml --sudo-pass <password>
# Check created system files
ls -la /opt/myapp/
Troubleshooting¶
"sudo: no tty present"
- Make sure to provide --sudo-pass flag
Permission denied without sudo
- Add become: true to 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.