Salt Stack‎ > ‎_Guides‎ > ‎

Salt Commands cheat sheet

REMOTE CMD

Start / Stop service on Minion
salt 'target' cmd.run "service apache2 stop"

Restart Minion on Win target
salt 'target' cmd.run 'start powershell "Restart-Service -Name salt-minion"'

Restart Minion on Linux target
salt 'target' cmd.run 'service salt-minion restart"'

Execute a script remotely
salt target cmd.execute python 'import sys; print sys.version'
2.7.8 GCC 4.9.1

salt target cmd.execute sh 'echo $PATH'
/usr/local/bin:/usr/local/sbin

SERVER DIAGNOSTICS

Test Connection

salt 'target' test.ping

Diagnostics
salt '*' status.uptime   // Uptime for Linux only
salt '*' disk.usage   // Disk Usage

Add Minions to Master
salt-key -L (show pending to be accepted)
salt-key -A (accept all pending)
salt-key -a target (accept by hostname)

Remove inactive minions from Salt
salt-run manage.down removekeys=True

Remove minions by name
salt-key -D targetName

Show Grain data

salt '*' grains.ls   
salt '*' grains.items

Show Minions by State (Up/Down)
salt-run manage.up
salt-run manage.down
salt-run manage.status (show all by status)
Compliance and Audit
to get a compliance result, run a State check with test=True
> salt \* state.highstate test=True
This will return any differences from existing configuration to whats in the Top file
 
Show Salt Master version
> salt --versions-report

Show Salt Minion version
> salt-call --versions-report

Start Minion in Debug mode
salt-master --log-level=debug


Restart everything on Master:
pkill salt-minion  //Kill minion
pkill salt-syndic  // Kill Syndic
salt-run cache.clear_all   //Clear all cache
salt '*' saltutil.sync_grains   //Sync grains
salt-master -d  //Start master daemon
salt-minion -d  //Start minion daemon
salt-syndic -d  //Start syndic daemon

PACKAGES AND INSTALLATION

Targeting minions

salt -G os:Windows cmd.run "net stop Firewall"   (By Grain using OS grain)
salt –G 'server_type:app and env:prod' state.highstate (By other Grains)
salt –C 'server_type:web and clo*' state.sls nginx (compound match) salt –L 'appserver,dbserver,webserver' state.sls ntp (list based match) salt –N ny_db_servers cmd.run 'ps –ef | grep mysql' (nodegroup based match)

Verbose output
salt 'target' state.hightstate -t 300 -v

Show package version
salt 'target' pkg.version apache

install package on minions
salt 'target' pkg.install apache


Uninstall pkg
salt 'target' pkg.remove 'npp'
salt 'target' pkg.purge 'npp'



Show Installed Packages or Software
salt 'target' pkg.list_pkgs

Check if a file on the Salt minions contains a certain regex (search file on minions):
salt "*" file.contains_regex /etc/resolv.conf "timeout.4"

Push or upload or copy a file from the Salt master to the Salt minions:
salt-cp "*" some_script.py /target-dir/some_script.py


JOBS AND PROCESS CONTROL

Show all Salt jobs run history
salt-run jobs.list_jobs

Show active Salt jobs
salt-run jobs.active    // returns a Job ID

Show currently running processes on a minion
salt '*' saltutil.running

Kill active job

salt 'target' saltutil.kill_job $JOB_ID
salt '*' saltutil.term_job <job id>

Clear Job cache
salt '*' saltutil.clear_cache

REGEX and SEARCH

Check if file contains a string (true/false)
salt '*' file.contains /etc/ssh/sshd_config 'Port'


Find a file
salt '*' file.find /etc name=host\*.\*
result
- /etc/host.conf - /etc/hosts.allow - /etc/hosts.deny'
Replace contents of file with new value
salt '*' file.sed /etc/ssh/sshd_config 'Port 22' 'Port 2201'


FILE OPERATIONS
Create folder
salt '*' file.makedirs /tmp/testFolder/ (for windows use native Win syntax, ie C:/temp/dir)

Delete folder
salt '*' file.remove /tmp/testFolder

Create new file
salt '*' file.touch /tmp/testFolder/emptyFile



USER AND GROUP MGMT

Set user's password to 123123
salt '*' shadow.set_password user02 '$6$EYk3o52W$DaSUIfHpYMBkSShFYXdODyrHbmQlCNKFghNpRABSdrJWkIMcXay.l9FZzZshUn240GCOn5szQ3piyBMtt/x4m.'

Here is two ways to generate you own password:

  • python -c "import crypt, getpass, pwd; print crypt.crypt('yourpassword', '\$6\$SALTsalt\$')"
  • openssl passwd -1

Add User

salt target user.add Joe

Remove User
salt '*' user.delete Joe remove=True force=True
Show all users on a target
salt target user.list_users

Info on all users on a target

salt target user.getent

Info on specific user
salt target user.info Joe

Add User to Group

salt '*' user.chgroups Joe Administrator, LocalAdmin True


Show users Groups

salt target user.list_groups Joe

Change Users Shell
salt '*' user.chshell user02 /bin/bash


STATES AND PILLARS

Highstate

salt '*' state.highstate

Deploy specific state

salt '*' state.sls webserver

Run multiple state executions on same minion at once (by design Salt limits 1 state per minion at once)

salt target state.sls yourState concurrent=True



















Comments