08 - Tags¶
Learn how to use tags to selectively run parts of your configuration.
What You'll Learn¶
- Adding tags to steps
- Filtering execution with
--tagsflag - Organizing workflows with tags
- Combining tags with conditionals
Quick Start¶
cd examples/08-tags
# Run all steps (no tag filter)
mooncake run --config config.yml
# Run only development steps
mooncake run --config config.yml --tags dev
# Run only production steps
mooncake run --config config.yml --tags prod
# Run test-related steps
mooncake run --config config.yml --tags test
# Run multiple tag categories
mooncake run --config config.yml --tags dev,test
What It Does¶
Demonstrates different tagged workflows: - Development setup - Production deployment - Testing - Security audits - Staging deployment
Key Concepts¶
Adding Tags¶
Tag Filtering Behavior¶
No tags specified: - All steps run (including untagged steps)
Tags specified (--tags dev):
- Only steps with matching tags run
- Untagged steps are skipped
Multiple tags (--tags dev,prod):
- Steps run if they have ANY of the specified tags
- OR logic: matches dev OR prod
Tag Organization Strategies¶
By Environment:
By Phase:
By Component:
By Role:
Multiple Tags Per Step¶
Steps can have multiple tags:
This runs with:
- --tags test ✓
- --tags prod ✓
- --tags security ✓
- --tags dev ✗
Real-World Examples¶
Development Workflow¶
# Install dev tools only
mooncake run --config config.yml --tags dev,tools
# Run tests
mooncake run --config config.yml --tags test
Production Deployment¶
# Deploy to production
mooncake run --config config.yml --tags prod,deploy
# Run security checks
mooncake run --config config.yml --tags security,prod
Staging Environment¶
Combining Tags and Conditionals¶
- name: Install Linux dev tools
shell: apt install build-essential
become: true
when: os == "linux"
tags:
- dev
- tools
Both must match:
1. Condition must be true (os == "linux")
2. Tag must match (if --tags specified)
Testing Different Tag Filters¶
# Preview what runs with dev tag
mooncake run --config config.yml --tags dev --dry-run
# Run dev and test steps
mooncake run --config config.yml --tags dev,test
# Run only setup steps
mooncake run --config config.yml --tags setup
Best Practices¶
- Use consistent naming - Pick a scheme (env, phase, role) and stick to it
- Multiple tags per step - Makes filtering more flexible
- Document your tags - In README or comments
- Combine with conditionals - For environment + OS filtering
Next Steps¶
Continue to 09-sudo to learn about privilege escalation.