Why Targeting Matters
A Salt command always has the form: salt 'WHO' WHAT ARGUMENTS. The target (WHO) decides which minions run the job. Good targeting is the safety belt of Salt: a wrong target on a destructive command is expensive.
Match by Minion ID
Match by Grains
Grains are facts about the machine (see the Grains lesson). Matching on grains lets you target by OS, role, virtualisation, kernel and more.
Match by Pillar, Network and Others
| Flag | Matcher | Example |
|---|---|---|
| (none) | Glob on id | salt 'web*' ... |
-E | Regex on id | salt -E 'web[0-9]+' ... |
-L | List of ids | salt -L 'a,b' ... |
-G | Grain | salt -G 'os:Ubuntu' ... |
-P | Grain regex | salt -P 'os:(Ubuntu|Debian)' ... |
-I | Pillar | salt -I 'env:prod' ... |
-J | Pillar regex | salt -J 'env:pr.*' ... |
-S | Subnet or IP | salt -S '10.0.0.0/8' ... |
-N | Nodegroup | salt -N db ... |
-C | Compound (mix of the above) | salt -C 'G@os:Ubuntu and web*' ... |
Compound Matchers: Combine Conditions
With -C you combine matchers with and, or, not, and parentheses. Each part has a letter prefix.
| Prefix | Matches |
|---|---|
| (none) | Glob on minion id |
G@ | Grain |
P@ | Grain regex |
I@ | Pillar |
J@ | Pillar regex |
E@ | Regex on id |
L@ | List of ids |
S@ | Subnet/IP |
N@ | Nodegroup |
Nodegroups: Name a Set of Minions
Nodegroups give meaningful names to groups, so your team types -N webservers instead of a long compound expression. Keep them in Git.
Batch: Run on a Few Machines at a Time
With --batch-size (or -b) Salt runs the job on a limited number of minions at once and waits before the next group. This is the simplest way to do a rolling restart or update.
Dry Runs and Safety Checks
--preview-target (or a harmless test.ping) with the same target and check the list of machines.
Other Useful Options
| Option | Meaning |
|---|---|
-t 30, --timeout=30 | Seconds to wait for returns (default 5) |
--out=json, --out=yaml, --out=table | Output format |
--static | Print all results together after everyone returns |
-v, --verbose | Show the job id and targeted minions |
--async | Start the job and return the job id immediately |
--subset=N | Run on any N of the matched minions |
--no-color, --hide-timeout | Cleaner output for scripts |
Targeting in Top Files
The same matchers work in top.sls (states) and the pillar top file. Without -C, add match: grain or match: compound.
Practice Tasks
- Set a custom grain
roles: webon two minions, then target them with-G - Create a nodegroup called
webserversand use-N webservers test.ping - Write a compound target that selects all RedHat family minions that are not named
db* - Run
service.restartwith--batch-size 1 --batch-wait 10on three minions - Use
--preview-targetto check a target before running a package upgrade
Conclusion
Targeting decides who runs a job. Use globs for ids, -G and -I for grains and pillar, -C for combinations and nodegroups for names. Preview targets and run risky jobs in batches.

