My previous posts about grain data were to show how to add minion grain data during the creation of a new server with vRA using PowerShell. This post I want to show how I’m going to maintain the grain data that is not default out of the box grain data.
Windows Server grain data not available out of the of box that I want to add to every minion AND make sure the grain data stays up to date:
Windows Server Features | Roles
Windows Server Installed Software
Windows Server Running Services
These are the steps that I use to maintain custom grain data with a Windows Server:
Create a PowerShell Script that can run locally on a minion. Add the script to the SaltStack Config File Manager. Use salt-call to run salt.functions locally on a minion. See screen shot and my code examples below.
Create a SaltStack Config Job to execute the PowerShell Script remotely on the minion. See Screen Shot.
Create a SaltStack Config Schedule to run the job that you create. The job can run every day, once a week, every 4 hours. Whatever makes sense for your use case. See Screen Shot.
If you want to run a script locally on a minion and use salt functions that is where you use the salt-call command. After you learn the salt-functions and are logged into a Windows Server locally, you can still use salt commands by using salt-call.
1
2
3
4
5
6
7
8
9
10
11
12
# --- example codesalt-call--versionsalt-calltest.ping# --- Show Disk Usagesalt-calldisk.usage# --- Show Installed Features|Rolessalt-callwin_servermanager.list_installed# --- Show Installed Sofwtaresalt-callpkg.list_pkgs
<#
.SYNOPSIS This Script is used to Get Windows Servers Features and add as Grain Data
.DESCRIPTION Windows Servers Features
.PARAMETER No Parameters
.INPUTS No inputs
.OUTPUTS salt grain data
.NOTES Version: 1.00
Author: Dale Hassinger
Creation Date: 04/20/2022
Purpose/Change: Initial script development
Revisions:
.EXAMPLE N/A
#># ----- [ Start of Code ] ---------------------------------------------------------------------------# --- Minion Get Windows Server Features Installed# --- Delete existing grains features data$saltCommand='salt-call grains.delkey sem_Windows_Feature force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand$saltCommand='salt-call win_servermanager.list_installed --output=json'#$saltCommand# --- Run Salt Command$results=Invoke-Expression-Command$saltCommand#$results$features=$results|ConvertFrom-Json#$features$features=$features.local#$features$features=$features-split(":")$features=$features-split(";")$features=$features-replace("@{","")$features=$features-replace("}","")$features=$features.trim()$features=$features-replace("="," | ")$features=$features|Sort-Object#$featuresforeach($featurein$features){# --- Grains Append$saltCommand='salt-call grains.append sem_Windows_Feature "'+$feature+'"'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand}# End Foreach# --- Delete existing grains last update data$saltCommand='salt-call grains.delkey sem_last_grains_update force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Add a Date that grains last updated$grainsupdateDate=Get-Date$grainsupdateDate=$grainsupdateDate.ToString("MM/dd/yyyy | hh:mm")#$grainsupdateDate# --- Grains Append$saltCommand='salt-call grains.append sem_last_grains_update "'+$grainsupdateDate+'"'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Grains Sync$saltCommand='salt-call saltutil.sync_grains'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# ----- [ End of Code ] ---------------------------------------------------------------------------
- Add Windows Server Installed Packages|Software as grain data:
<#
.SYNOPSIS This Script is used to Get Windows Installed Packages and add as Grain Data
.DESCRIPTION Windows Servers Installed Packages
.PARAMETER No Parameters
.INPUTS No inputs
.OUTPUTS salt grain data
.NOTES Version: 1.00
Author: Dale Hassinger
Creation Date: 04/20/2022
Purpose/Change: Initial script development
Revisions:
.EXAMPLE N/A
#># ----- [ Start of Code ] ---------------------------------------------------------------------------# --- Minion Get Windows Server Installed Packages|Software# --- Delete existing grains features data$saltCommand='salt-call grains.delkey sem_Windows_Installed_Software force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand$saltCommand='salt-call pkg.list_pkgs --output=json'#$saltCommand# --- Run Salt Command$results=Invoke-Expression-Command$saltCommand#$results$installedPackages=$results|ConvertFrom-Json$installedPackages=$installedPackages.local$installedPackages=$installedPackages-split(":")$installedPackages=$installedPackages-split(";")$installedPackages=$installedPackages-replace("@{","")$installedPackages=$installedPackages-replace("}","")$installedPackages=$installedPackages.trim()$installedPackages=$installedPackages-replace("="," | ")$installedPackages=$installedPackages|Sort-Object#$installedPackagesforeach($installedPackagein$installedPackages){# --- Grains Append$saltCommand='salt-call grains.append sem_Windows_Installed_Software "'+$installedPackage+'"'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand}# End Foreach# --- Delete existing grains last update data$saltCommand='salt-call grains.delkey sem_last_grains_update force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Add a Date that grains last updated$grainsupdateDate=Get-Date$grainsupdateDate=$grainsupdateDate.ToString("MM/dd/yyyy | hh:mm")#$grainsupdateDate# --- Grains Append$saltCommand='salt-call grains.append sem_last_grains_update "'+$grainsupdateDate+'"'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Grains Sync$saltCommand='salt-call saltutil.sync_grains'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# ----- [ End of Code ] ---------------------------------------------------------------------------
<#
.SYNOPSIS This Script is used to Get Windows Servers Running Services and add as Grain Data
.DESCRIPTION Windows Servers Running Services
.PARAMETER No Parameters
.INPUTS No inputs
.OUTPUTS salt grain data
.NOTES Version: 1.00
Author: Dale Hassinger
Creation Date: 04/20/2022
Purpose/Change: Initial script development
Revisions:
.EXAMPLE N/A
#># ----- [ Start of Code ] ---------------------------------------------------------------------------# --- Minion Get Windows Server Running Services# --- Delete existing grains running services data$saltCommand='salt-call grains.delkey sem_Windows_Services_Running force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Get all Running Services$serviceNames=Get-Service|Where-Object{$_.Status-eq'Running'}# --- Create Array$servicesGrains=@()# --- Add Service data to the arrayforeach($serviceNamein$serviceNames){$grainString=$serviceName.Name+' | '+$serviceName.DisplayName$servicesGrains=$servicesGrains+$grainString}# End Foreach#$servicesGrains# --- Add Windows Running Services as Grain Dataforeach($servicesGrainin$servicesGrains){# --- Grains Append$saltCommand='salt-call grains.append sem_Windows_Services_Running "'+$servicesGrain+'"'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand}# End Foreach# --- Delete existing grains last update data$saltCommand='salt-call grains.delkey sem_last_grains_update force=True'#$saltCommand# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Add a Date that grains last updated$grainsupdateDate=Get-Date$grainsupdateDate=$grainsupdateDate.ToString("MM/dd/yyyy | hh:mm")#$grainsupdateDate# --- Grains Append$saltCommand='salt-call grains.append sem_last_grains_update "'+$grainsupdateDate+'"'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# --- Grains Sync$saltCommand='salt-call saltutil.sync_grains'# --- Run Salt CommandInvoke-Expression-Command$saltCommand# ----- [ End of Code ] ---------------------------------------------------------------------------
SaltStack Config Target using Windows Server Running Service (SQL Server):
Lessons Learned:
Windows Server Features|Roles make a great way to target minions.
Windows Server installed software also makes a great way to target minions.
Windows Server Running Services is a good way to target minions.
If you add custom grain data when the server is created you also need a way to maintain the grain data so it stays up to date and is current.
When I write about vRealize Automation ("vRA") I always say there are many ways to accomplish the same task. SaltStack Config is the same way. I am showing what I felt was important to see but every organization/environment will be different. There is no right or wrong way to use Salt. This is a GREAT Tool that is included with your vRealize Suite Advanced/Enterprise license. If you own the vRealize Suite, you own SaltStack Config.
If you like wearing Crocs and want to get a pair like I wear, follow this link to Amazon:
My Favorite Crocs
If you found this Blog article useful and it helped you, Buy me a coffee to start my day.