Table of contents
1.
Introduction
2.
Grains
2.1.
Listing Grains
2.2.
Assigning Grains to Minions
2.2.1.
GRAINS IN THE MINION CONFIG
2.2.2.
GRAINS IN /ETC/SALT/GRAINS
2.2.3.
Matching Grains In Top File
2.3.
Writing Custom Grains
2.3.1.
When to Use Custom Grains
2.3.2.
Loading Custom Grains
3.
Frequently Asked Questions
3.1.
What is Salt?
3.2.
What is a Grain in Salt?
3.3.
What is the Event System in Salt?
3.4.
What is an Event bus in salt?
3.5.
What are Key events in salt?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Overview of Grains in Salt

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Salt is a configuration management and distributed remote execution system. It was created with the goal to improve, simplify, and further customize the best remote execution solutions. It can maintain the remote nodes in defined states and query and execute commands on various nodes.

saltstack

In this blog, we will discuss grains in the salt system.

Grains

Grain is a salt interface that derives information about the underlying system. Grains are collected for the IP address, domain name, OS, kernel, OS type, and various other system properties. 
The salt modules have access to the grains interface so that the correct salt-minion commands are automatically available on the correct systems.

Let’s discuss how to list grains.

Listing Grains

The available grains can be listed using the 'grains.ls' module:

salt '*' grains.ls
You can also try this code with Online Python Compiler
Run Code


The grains data can be listed using the ​​ 'grains.items' module:

salt '*' grains.items
You can also try this code with Online Python Compiler
Run Code

Assigning Grains to Minions

The multiple ways to assign grains to minions are:-

GRAINS IN THE MINION CONFIG

Grains can be assigned statically inside the minion configuration file. You just need to add the option grains and pass the options.

grains:
  roles:
    - webserver
    - memcache
  deployment: datacenter4
  cabinet: 13
  cab_u: 14-15
You can also try this code with Online Python Compiler
Run Code


GRAINS IN /ETC/SALT/GRAINS

Previously we have assigned the grains to the minion configuration file, but if you don't want to place your custom grains inside the minion configuration file. You can put them in /etc/salt/grains on the minion. Let’s see the below example:-

roles:
  - webserver
  - memcache
deployment: datacenter4
cabinet: 13
cab_u: 14-15
You can also try this code with Online Python Compiler
Run Code


Matching Grains In Top File

If the grains are correctly configured on minions, the top file used in Pillar can be made very efficient. Let’s see the following configuration:

'roles: webserver':
  - match: grain
  - state0

'roles:memcache':
  - match: grain
  - state1
  - state2
You can also try this code with Online Python Compiler
Run Code

Writing Custom Grains

The rules to write your own custom grains are:-

  • Grains are derived by executing all the public functions. Public functions are those function which does not begin with an underscore.
     
  • The functions in a grains module must return a Python dictionary, where the keys of the dictionary represent the name of the grains, and the values represent the value for the grain corresponding to that key.
     
  • The custom grains modules must be put in the _grains subfolder under the file_roots given in the master config file.
     
  • The custom grains modules are given to the minions when the state.highstate is executed, or by executing the saltutil.sync_grains or saltutil.sync_all functions.
     

Let’s see an example to understand how to write custom grains:

def codingninjas():
    # initialize a grains dictionary
    grains = {}
    # Some code for logic that sets grains like
    grains["codingninjas"] = True
    grains["anothergrain"] = "somevalue"
    return grains
You can also try this code with Online Python Compiler
Run Code


When to Use Custom Grains

Grains are used for the static data and the data that is helpful for targeting minions in the top file or the Salt CLI. If the data is likely to change, use other modules instead of Grains. 

Loading Custom Grains

If there are multiple functions specifying grains and are called from the main function, make sure to use an underscore as a prefix with the name of grains. Let’s consider the custom grain file:-

#!/usr/bin/env python
def _ninjas_custom_grain():
    ninjas_grains = {"hi": "hey", "hello": "ninjas"}
    return ninjas_grains


def main():
    # initialize a grains dictionary
    grains = {}
    grains["ninjas_grains"] = _ninjas_custom_grain()
    return grains
You can also try this code with Online Python Compiler
Run Code


The output of the above example will look like this:-

# salt-call --local grains.items
local:
    ----------
    <Snipped for brevity>
    ninjas_grains:
        ----------
        hi:
            hey
        hello:
            ninjas
You can also try this code with Online Python Compiler
Run Code


Let’s take a case where you have not prepended the underscore before the grain function name. In this case, the function will be rendered twice by Salt. The output for the above-taken example, in this case, will be:-

# salt-call --local grains.items
local:
----------
    <Snipped for brevity>
    hi:
        hey
    <Snipped for brevity>
    hello:
        ninjas
    <Snipped for brevity>
    ninjas_grains:
        ----------
        hi:
            hey
        hello:
            ninjas
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is Salt?

Salt is a configuration management and distributed remote execution system. It was created with the goal to improve, simplify, and further customize the best remote execution solutions. 

What is a Grain in Salt?

Grain is a salt interface that derives information about the underlying system. Grains are collected for the IP address, domain name, OS, kernel, OS type, and various other system properties.

What is the Event System in Salt?

The salt event system is used to fire off events like the Start event, Key event, Job event, etc., using third-party apps or with the help of an external process.

What is an Event bus in salt?

The event bus is used for both network transport and inter-process communication in salt. It consists of two components, the event socket and the event library.

What are Key events in salt?

These are the salt master events fired when accepting and rejecting the minion's keys on the salt master.

Conclusion

In this article, we have extensively discussed about grains in the salt system. I hope you enjoyed this blog on Overview of Grains in Salt. If you want to learn more, check out our articles on Salt Event SystemAbout Salt RunnersTarget Minions in SaltAbout Salt Engine, and Pillar Walkthrough

Also, check out these exciting courses from coding ninjas to expand your knowledge, Coding CourseCode StudioInterview ExperienceGuided PathInterview ProblemsTest SeriesLibrary, and Resources

Happy Coding!

Live masterclass