CoursesSaltBeginner

Remote execution & targeting

Run commands across the fleet instantly.

Intermediate12 min · lesson 2 of 12

Remote execution is the fastest way to feel Salt’s power: salt ‘<target>’ <function> runs an execution-module function on every matching minion in parallel and returns their results. test.ping confirms connectivity, cmd.run runs a shell command, pkg.install installs a package, service.restart restarts a service — hundreds of functions across modules. This is imperative and immediate, the counterpart to declarative states.

terminal
$ salt '*' test.ping
web1: True
web2: True
db1: True
$ salt 'web*' service.restart nginx
web1: True
web2: True
$ salt '*' pkg.version openssl # gather a fact from the whole fleet at once

Targeting

The target selects which minions a command hits, and Salt has many targeting methods: globbing on minion id (web*), grains (-G ‘os:Ubuntu’), pillar, subnet (-S), regex, or node groups defined on the master. Compound targeting combines them (-C ‘web* and G@os:Ubuntu’). Precise targeting is essential — it is the difference between restarting nginx on the web tier and restarting it everywhere.

terminal
$ salt -G 'os:Ubuntu' pkg.upgrade # target by grain
$ salt -C 'web* and G@datacenter:us-east' cmd.run 'uptime' # compound
$ salt -N webservers state.apply nginx # target a node group from the master config
salt '*' cmd.run is a fleet-wide root shell
Remote execution runs as root on every targeted minion instantly, so salt '*' cmd.run '<anything>' is a loaded gun — a wrong target or a bad command executes everywhere with no undo. Test targeting first (salt '<target>' test.ping shows exactly who you would hit), prefer specific execution modules over cmd.run, and restrict who can issue commands via the master’s ACLs.