The previous article, we have explained introduction about Salt SSH and it’s basic usage. In this guide, you are going to see the next level of Salt SSH Adhoc Commands. Here we have some basic examples to understand the functionality of Salt Stack automation tool.
Also, Check out the Video for more details:
Salt SSH Adhoc Commands and Basics Modules
# salt-ssh <target> <command> <arguments>
Let’s run the first salt command to test the connectivity with target minions
# salt-ssh -i ‘*’ test.ping
Note – “*” means all minions/systems in the roaster.
You can select the individual target minions by have then command separated in the target.
# salt-ssh –L ‘appserver1,appserver2’ test.ping
Also, targets can be filtered by the regular expression:
# salt-ssh -E ‘appserver[1-2]’ test.ping
Salt SSH makes use of different modules to perform the operation on the target minions , below is example
# salt-ssh ‘*’ disk.usage
# salt-ssh ‘*’ pkg.install httpd
# salt-ssh ‘*’ network.interfaces
Salt SSH allows to run shell commands remotely on multiple target clients using cmd.run:
# salt-ssh ‘*’ cmd.run ‘uname -a;date;ifconfig -a‘
More Info on modules and their option: https://docs.saltstack.com/en/stage/ref/modules/all/index.html
The Salt States or SLS
Salt states are used to automate the system setup and configurations in the Infrastructure.
# salt-ssh <target> state.<option> <state file>
In this demo we will use /etc/salt/master file , which is default configuration file for salt master. Uncomment below entries.
base: – /srv/salt/
You can create different base environments in the setup ..ex /srv/salt/dev or /srv/salt/prod
Grouping of minions of similar minions; similar functionality: /srv/salt/top.sls
base: # Apply SLS files from the dir root for the ‘base‘env.
‘app*’: # All minions with servers that begins with app prefix
– webserver # Apply the state file named ‘apache.sls‘
Sample State file to install httpd and start the service: /srv/salt/webserver.sls
– name: httpd
– enable: True
Apply the state to the client machines/minions
# salt-ssh ‘app*’ state.apply webserver