Have you ever wanted to find out how much memory every one of your servers has? Or how about target a command to be run against just your CentOS machines?
If you have your servers under Salt configuration management, you would use grains to perform these tasks. In this article, we will use salt grains to identify minions.
List All Grains From All Minions
You can get a list of all of the grains supported on a minion by using the grains.ls command. Beware, you will get a screenful!
Example: Get a list of all available grains from all minions by typing the following from the salt master
# salt '*' grains.ls
You can also get the actual data from the grain on the minion using the grain.items command.
Example: Get the value for a specific grain from the db2.uptimemadeeasy.com minion using the grep command
# salt 'db2.uptimemadeeasy.com' grains.items | grep os_family os_family: RedHat
or get a list of all of the processors running in your salt minions:
Example: Get a list of the cpu models for our minions using the grep command
# salt '*' grains.items | grep cpu_model cpu_model: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz cpu_model: Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz
The examples above show how get a specific grain by receiving all of the grains and grepping on the specific grain that we need. We can also get the specific grain if we know the grain name by listing it specifically. Below are a couple examples:
Example: Show the cpu_model grain for each of our minions by listing the grain specifically
$ salt '*' grains.item cpu_model email.uptimemadeeasy.com: cpu_model: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz ftp.uptimemadeeasy.com: cpu_model: Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz
Example: Show the mem_total grain for each of our minions by listing the grain specifically
# salt '*' grains.item mem_total email.uptimemadeeasy.com: mem_total: 1998 ftp.uptimemadeeasy.com: mem_total: 1992
So far, we have been working with default grains that are defined on your minions by default which can be very helpful. However, sometimes, defining our own grains can be useful also.
Create Your Own Grains on Your Minions
Some of the typical custom grains that you may want to create would be descriptive grains defining the minion’s location, purpose, or other descriptive information. You can define
Edit the minion (/etc/salt/minion) file, find the “grains:” section and then define the grains you need. The grain is followed by a “:” and the grain’s value follows.
Example: Defining custom salt grains in the minion’s minion file
grains: Address: 1234 Main Street, Suite 300, Apple View, CA 93103 Floor: 3rd Floor Room: Room 318 Server Room Rack: Rack 3
To make the new custom grains take effect, the salt-minion service must be restarted after editing the minion file.
Example: Restarting the salt-minion service on the salt minion
# service salt-minion restart Stopping salt-minion daemon: [ OK ] Starting salt-minion daemon: [ OK ]
Now back to the salt master, we can display the custom grains that we just defined.
Example: List the custom grains defined on the salt-minion
# salt 'ftp.uptimemadeeasy.com' grains.items ftp.uptimemadeeasy.com: Address: 1234 Main Street, Suite 300, Apple View, CA 93103 Floor: 3rd Floor Rack: Rack 3 Room: Room 318 Server Room ...
A second location where you can define your salt grains is in the /etc/salt/grains file
Server Role: FTP Server Environment: Production
Once again, you will have to restart the salt-minion service like we did above.
After the service is restarted, you can see the grains defined in the /etc/salt/grains file that we just set above:
# salt 'www4.collegeatlas.org' grains.items | more www4.collegeatlas.org: Address: 1234 Main Street, Suite 300, Apple View, CA 93103 Environment: Production Floor: 3rd Floor Rack: Rack 3 Room: Room 318 Server Room Server Role: FTP Server
Use Salt Grains to Target Minions
So, using salt grains can be pretty helpful in identifying your machines, finding attributes remotely, etc…, but perhaps the best use of gains is in allowing you to target specific minions based on their grains. By using the “-G” option in our salt command, we can target commands directly at all minions that have the grain value we specify.
Example: using an OS grain to target all Fedora based systems for package updates.
$ salt -G 'os:Fedora' cmd.run "yum update -y"
For more information on targeting minions using grains and otherwise, you may want to review a prior uptimemadeeasy.com article: More Salt Notes – Targeting Minions
Latest posts by Jeff Staten (see all)
- Configure Your HP Procurve Switch with SNTP - May 5, 2015
- Configuring HP Procurve 2920 Switches - May 1, 2015
- Troubleshooting Sendmail - November 28, 2014