Salt includes a built-in mechanism for determining static information about a system. These bits of information are referred to as grains within the Salt vocabulary. You might think of it as “little grains of information” about a machine. These grains of information include hardware and networking information, operating system details, and much more. These grains are also expandable to include other bits of static information that you’d like to have assigned to a machine. In this chapter we’ll explore the grains subsystem and learn how to leverage this data within our Salt infrastructure.
Once you’ve completed this chapter you should have an improved understanding of what grains are, how grains are queried, defined and synced. You should also understand the order of grain definition precedence.
If you’re not yet familiar with the grains system it is important to study this entire chapter. Upcoming chapters will make extensive use of the information outlined here.
Salt includes a set of “core grains” that should be available on any system. These grains should be detected properly on every supported operating system and distribution. In this section I’ll outline many of these core grains.
The list below defines the full set of core grains found on a CentOS Linux system. As you can see, these are just the keys without values, but it gives you an idea of what type of information is stored in grains.
As you can see, there are over fifty items defined for each system within your Salt infrastructure. These items will be used in upcoming chapters to demonstrate the flexibility of making your minion management more dynamic.
Let’s look at what some of these values store.
As you saw in the previous examples, there are a few different ways to query
for grains. Primarily you’ll use the
grains module and query for one, all
or specific grain values. If you know the name of the grain you’re looking for
you can query for that directly:
[root@minion ~]# salt '*' grains.item fqdn alpha: ---------- fqdn: alpha.domain.tld
[root@minion ~]# salt '*' grains.item kernelrelease alpha: ---------- kernelrelease: 2.6.32-431.29.2.el6.x86_64
[root@minion ~]# salt '*' grains.item mem_total alpha: ---------- mem_total: 7872
[root@minion ~]# salt '*' grains.item cpuarch alpha: ---------- cpuarch: x86_64
Beyond the “core grains” that are defined on every system, it is possible to define custom grains. These custom grains can be used to define additional attributes about your systems. Examples of this might be datacenter, rack, cabinet, or other internal or deployment-specific information. Grains are defined on a per-minion basis and append to the existing grains.
For more information about custom grains replacing existing grains, see the next section Precedence.
Custom grains can be defined in a couple of places. Again, because grains are unique per minion, custom grains are defined on a per-minion basis in one of two places:
There is a slight difference in the way custom grains are imported depending on
the location. First we’ll outline the
/etc/salt/minion method, followed by
Custom grains can be added directly to the minion config file, or included as a
new file in the
/etc/salt/minion.d/ directory. If custom grains are added
to either of these locations the whole structure needs to be prefixed with the
grains configuration option. See the example below:
grains: datacenter: va5 rack: 17 cabinet: 3 role: webserver
As you can see, custom grains are the same simple key-value pairs that the core grains are. These can be any arbitrary key-value pair that you want to define for your systems. In addition to the key-value pairs, you can define other more complex data structures such as lists or dictionaries. See the example below for more complex custom grains.
grains: role: - webserver - memcache owners: - tuttle - ewoolley
role: - webserver - memcache