The underlying architecture of Salt is based on the idea of executing commands remotely. This is not a new concept; all networking is designed around some aspect of remote execution. This could be as simple as asking a remote Web server to display a static Web page, or as complex as using a shell session to interactively issue commands against a remote server.
Under the hood, Salt is an example of one of the more complex types of remote execution. But whereas most Internet users are used to interacting with only one server at a time (so far as they are aware), Salt is designed to allow users to explicitly target and issue commands to multiple machines directly.
Salt is based around the idea of a Master, which controls one or more Minions. Commands are normally issued from the Master to a target group of Minions, which then execute the tasks specified in the commands and then return any resulting data back to the Master.
The first facet of the
salt command is targeting. A target must be specified with each execution, which matches one or more Minions. By default, the type of target is a glob, which is the style of pattern matching used by many command shells. Other types of targeting are also available, by adding a flag. For instance, to target a group of machines inside a particular subnet, the
-S option is used:
# salt -S 192.168.0.0/24 test.ping
The following are most of the available target types, along with some basic usage examples. Not all target types are covered here; Range, for example, extends beyond the scope of this book. However, the most common types are covered.
salt command is issued from most command shells, wildcard characters must be protected from shell expansion:
# salt '*' test.ping # salt \* test.ping
When using Salt from an API or from other user interfaces, quoting and escaping wildcard characters is generally not required.
When more complex pattern matching is required, a Perl Compatible Regular Expression (PCRE) can be used. This type of targeting was added to the earliest versions of Salt, and was meant largely to be used inside shell scripts. However, its power can still be realized from the command line:
# salt -E '^[m|M]in.[e|o|u]n$' test.ping
This option allows multiple Minions to be specified as a comma-separated list. The items in this list do not use pattern matching such as globbing or regular expressions; they must be declared explicitly:
# salt -L web1,web2,db1,proxy1 test.ping
# salt -S 192.168.0.42 test.ping # salt -S 192.168.0.0/16 test.ping
As of Salt version 2015.5, IPv6 addresses cannot be targeted by a specific command line option. However, there are other ways to target IPv6 addresses. One way is to use Grain matching.
Salt can target Minions based on individual pieces of information that describe the machine. This can range from the OS to CPU architecture to custom information (covered in more detail later in this chapter). Because some network information is also available as Grains, IP addresses can also be targeted this way.
Since Grains are specified as key/value pairs, both the name of the key and the value must be specified. These are separated by a colon:
# salt -G 'os:Ubuntu' test.ping # salt -G 'os_family:Debian' test.ping
Some Grains are returned in a multi-level dictionary. These can be accessed by separating each key of the dictionary with a colon:
# salt -G 'ip_interfaces:eth0:192.168.11.38'
Grains which contain colons may also be specified, though it may look strange. The following will match the local IPv6 address (
::1). Note the number of colons used:
# salt -G 'ipv6:::1' test.ping
Short Version: (not available)
# salt --grain-pcre 'os:red(hat|flag)' test.ping
# salt -I 'my_var:my_val' test.ping
Compound targets allow the user to specify multiple target types in a single command. By default, globs are used, but other target types may be specified by preceding the target with the corresponding letter followed by the
PCRE Minion ID
The following command will target the Minions that are running Ubuntu, have the
role Pillar set to
web, and are in the
# salt -C 'G@os:Ubuntu and I@role:web and [email protected]/24' test.ping
# salt -C 'min* or *ion' test.ping # salt -C 'web* or *qa,G@os:Arch' test.ping
While node groups are used internally in Salt (all targeting ultimately results in the creation of an on-the-fly nodegroup), it is much less common to explicitly use them from the command line. Node groups must be defined as a list of targets (using compound syntax) in the Salt Master's configuration before they can be used from the command line. Such a configuration might look like the following:
nodegroups: webdev: 'I@role:web,G@cluster:dev' webqa: 'I@role:web,G@cluster:qa' webprod: 'I@role:web,G@cluster:prod'
Once a nodegroup is defined and the master configuration reloaded, it can be targeted from Salt:
# salt -N webdev test.ping
After a target is specified, a function must be declared. The preceding examples all use the
test.ping function but, obviously, other functions are available. Functions are actually defined in two parts, separated by a period:
<module> . <function>
Inside a Salt command, these follow the
target, but precede any arguments that might be added for the function:
salt <target> <module>.<function> [arguments...]
For instance, the following Salt command will ask all Minions to return the text,
salt '*' test.echo 'Hello world'
A number of execution modules ship with the core Salt distribution, and it is possible to add more. Version 2015.5 of Salt ships with over 200 execution modules. Not all modules are available for every platform; in fact, by design, some modules will only be available to the user if they are able to detect the required underlying functionality.
For instance, all functions in the test module are necessarily available on all platforms. These functions are designed to test the basic functionality of Salt and the availability of Minions. Functions in the Apache module, however, are only available if the necessary commands are located on the Minion in question.
Execution modules are the basic building blocks of Salt; other modules in Salt use them for their heavy lifting. Because execution modules are generally designed to be used from the command line, an argument for a function can usually be passed as a string. However, some arguments are designed to be used from other parts of Salt. To use these arguments from the command line, a Python-like data structure is emulated using a JSON string.
This makes sense, since Salt is traditionally configured using YAML, and all JSON is syntactically-correct YAML. Be sure to surround the JSON with single quotes on the command line to avoid shell expansion, and use double quotes inside the string. The following examples will help.
A list is declared using brackets:
A dictionary is declared using braces (that is, curly brackets):
A list can include a dictionary, and a dictionary can include a list:
There are a few modules which can be considered core to Salt, and a handful of functions in each that are widely used.
This is the most basic Salt command. Ultimately, it only asks the Minion to return True. This function is widely used in documentation because of its simplicity, and to check whether a Minion is responding. Don't worry if a Minion doesn't respond right away; that doesn't necessarily mean it's down. A number of variables could cause a slower-than-usual return. However, successive failed attempts may be cause for concern.
This function does little more than the
test.ping command; it merely asks the Minion to echo back a string that is passed to it. A number of other functions exist that perform similar tasks, including
A slightly more advanced testing scenario may require a Minion to sleep for a number of seconds before returning True. This is often used to test or demonstrate the utilities that make use of the jobs system. The
test.rand_sleep function is also useful for test cases where it is desirable to check the return from a large number of Minions, with the return process spread out.
In a large enough infrastructure, a number of Minions are bound to be running in a different version of Salt than others. When troubleshooting issues specific to certain versions of Salt, it helps to be able to take a quick look at the Salt version on each Minion. This is the simplest way to check that. Checking the version of other packages that are maintained by the system packaging system can be performed with
Every package manager in Salt (as of version 2015.5) supports installing a package. This function can be as simple as asking for a single package name, or as complex as passing through a list of packages, each with a specific version. When using an execution module, you generally do not need to specify more than just a single package name, but inside the State module (covered later) the advanced functionality becomes more important.
This matches the
pkg.install function, allowing a certain package to be removed. Because versions are not so important when removing packages, this function doesn't get so complex. But it does allow passing a list of packages to be removed (using the
pkgs argument) as a Python list. From the command line, this can be done using a JSON string.
sed command is one of the oldest members of the Unix administrator's toolkit. It has been the go-to command largely for tasks that involve editing files in-line, and performing search and replace tasks. There have been a few attempts over the years to duplicate the functionality of the
sed command. Initially, the
file.sed function simply wrapped the Unix
sed command. The
file.psed function provided a Python-based replacement. However,
sed is more than just a find/replace tool; it is a full language that can be problematic when used incorrectly. The
file.replace function was designed from the ground up to provide the find/replace functionality that most users need, while avoiding the subtle nuances that can be caused by wrapping
A number of common Unix commands have been added to the
file function. The following functions complement the Unix command set for managing files and their metadata:
file.mknod, and a number of others.
The Unix toolset for managing users and groups is also available in Salt and includes
user.chgroups, and many, many more.
By design, every public function in every execution module must be self-documenting. The documentation that appears at the top of the function should contain a description just long enough to describe the general use of the function, and must include at least one CLI example demonstrating the usage of that function.
This documentation is available from the Minion using the
sys.doc function. Without any arguments, it will display all the functions available on a particular Minion. Adding the name of a module will show only the available functions in that module, and adding the name of a function will show only the documentation for that function, if it is available. This is an extremely valuable tool, both for providing simple reminders of how to use a function and for discovering which modules are available.