This can include things like the operating system version, CPU type, and even whether or not there’s coffee in the break room (just kidding, but you get the idea).
Now, why would we want to use Python scripts with Grains? Well, because sometimes that information isn’t enough on its own. Maybe you need to do some fancy calculations based on CPU usage and memory allocation, or maybe you just want to check if a certain package is installed before running a script. That’s where the magic happens!
Here’s an example of how we can use Python scripts with Grains in SaltStack:
1. First, let’s create a new file called `my_grain.py` and put it in our SaltStack state directory (usually located at /srv/pillar). This is where all of our custom grains will live.
2. In the `my_grain.py` file, let’s add some code that checks if a certain package is installed on each server:
#!/usr/bin/env python3
# This line specifies the interpreter to be used to run the script
import os # Importing the os module to access system commands
def __virtual__():
# This function is used to determine if the grain should be loaded or not
return 'my_grain' # Returning the name of the grain to be loaded
def my_package():
# This function checks if a certain package is installed on each server
cmd = "dpkg -s {} | grep '^Status: install ok$'" # Command to check if the package is installed
result = os.popen(cmd.format('mypackage')).read() # Running the command and storing the output in a variable
if len(result) == 0: # Checking if the package is not found
return '' # If not found, return an empty string
else: # If the package is found
version = result.split()[2] # Extracting the version number from the output
return {'my_package': version} # Returning the version number as a grain value
3. Save the file and exit. Now let’s test our new custom grain by running this command in SaltStack:
# This script is used to run a state that checks if 'mypackage' is installed on each server using a custom grain called 'my_grain'.
# First, we need to import the necessary modules for SaltStack to run the script.
# This includes the 'state' module which allows us to run states and the 'sls' function which specifies which state to run.
import salt.modules.state as state
from salt.modules.state import sls
# Now we can use the 'sls' function to run the 'my_package' state.
# This state will use our custom grain to check if 'mypackage' is installed on each server.
sls('my_package')
4. If everything works correctly, you should see the version number of `mypackage` printed out for each server in your SaltStack output:
bash
# This script is used to display the version number of 'mypackage' for each server in the SaltStack output.
# It is assumed that the 'state.sls my_package' command has already been run on multiple servers.
# Loop through each server in the output
for server in $(salt '*' grains.item fqdn | grep my_server | awk '{print $1}'); do
# Use the 'salt' command to retrieve the version number of 'mypackage' for each server
version=$(salt $server grains.get my_package | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+')
# Print out the server name and its corresponding version number
echo "$server: $version"
done
bash
# This script is used to display the version number of 'mypackage' for each server in the SaltStack output.
# It is assumed that the 'state.sls my_package' command has already been run on multiple servers.
# Loop through each server in the output
for server in $(salt '*' grains.item fqdn | grep my_server | awk '{print $1}'); do
# Use the 'salt' command to retrieve the version number of 'mypackage' for each server
version=$(salt $server grains.get my_package | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+') # Use the 'grep' command to extract the version number from the output
# Print out the server name and its corresponding version number
echo "$server: $version" # Print the server name and version number in a readable format
done
- The script is used to display the version number of 'mypackage' for each server in the SaltStack output.
- The 'state.sls my_package' command is assumed to have already been run on multiple servers.
- The 'for' loop is used to iterate through each server in the output.
- The 'salt' command is used to retrieve the version number of 'mypackage' for each server.
- The 'grep' command is used to extract the version number from the output.
- The 'awk' command is used to print only the server name from the output of the 'salt' command.
- The 'echo' command is used to print the server name and version number in a readable format.
And that’s it! You now have a custom grain in SaltStack using Python scripts. Pretty cool, huh?