VMware vRealize SaltStack Config as a Windows Server Admin - Part 7
The more I use VMware vRealize SaltStack Config as a Windows Server Admin the more I see the importance of having grain information added to the minion to be able to target servers many different ways. During a recent discussion it was suggested to have the ability to target Windows Server minions by the Windows server Features|Roles that were installed on the server and by which software was installed on a server. There are built-in salt functions to get Features|Roles and installed software. Here is the code I used to get the information and add to the minion grain file using PowerShell.
# The POSH-SSH PowerShell module MUST be installed to use this code.# Name of Minion that you want to add grain data$minion='DBH-210'# ----- [ SSH Connection to SalStack Config Server ] ------------------------------------Write-Host'Making SSH Connection to SaltStack Config Server...'$Server='ssc.vCROCS.local'$userName='root'$Password='VMware#1'# The next line is how to create the encrypted password$psPassword=ConvertTo-SecureString-String$Password-AsPlainText-Force$creds=new-object-typenameSystem.Management.Automation.PSCredential-argumentlist$username,$psPassword$Params=@{"ComputerName"=$Server"Credential"=$creds}# End Params# SSH Connection to SaltStack Server$sshSession=Get-SSHSessionif($sshSession.SessionId-eq0){Write-Host'SSH Connection to SSC Server already completed'}# End Ifelse{Write-Host'Creating new SSH Connection to SSC Server'New-SSHSession@Params}# End Else# ----- [ Start Adding Grain Data ] ---------------------------------------------------------------------------# ----- [ Minion Get Windows Server Features|Roles Installed ] ------------------------------------Write-Host'Getting Windows Server Features|Roles Installed...'$sshCommand='salt "'+$minion+'" win_servermanager.list_installed --output=json'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End Params$results=Invoke-SSHCommand@Params$features=$results.Output$features=$results.Output|ConvertFrom-Json$features=$features.PsObject.Properties.Value$features=$features|ConvertTo-Xml# Append Each Feature|Role information to grainsWrite-Host'Appending Windows Server Features|Roles Installed to grains...'$grainsKey='vCROCS_Windows_Feature'foreach($featurein$features.Objects.Object.Property){$grainsValue=$feature.Name+' | '+$feature.'#text'# Grains Append$sshCommand='salt "'+$minion+'" grains.append '+$grainsKey+' "'+$grainsValue+'"'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End ParamsInvoke-SSHCommand@Params}# End Foreach# ----- [ Minion Get Windows Server Installed Software ] ------------------------------------Write-Host'Getting Windows Server Installed Software...'# Get Installed Software$sshCommand='salt "'+$minion+'" pkg.list_pkgs --output=json'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End Params$results=Invoke-SSHCommand@Params$installedPackages=$results.Output$installedPackages=$installedPackages|ConvertFrom-Json$installedPackages=$installedPackages.PsObject.Properties.Value$installedPackages=$installedPackages|ConvertTo-Xml$grainsKey='vCROCS_Windows_Installed_Software'# Append Windows Server Installed Software to grainsWrite-Host'Appending Windows Server Installed Software to grains...'foreach($installedPackagein$installedPackages.Objects.Object.Property){$grainsValue=$installedPackage.Name+' | '+$installedPackage.'#text'# Grains Append$sshCommand='salt "'+$minion+'" grains.append '+$grainsKey+' "'+$grainsValue+'"'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End ParamsInvoke-SSHCommand@Params}# End Foreach# ----- [ Add a Date that grains last updated ] ----------------------------------------------------------------$grainsupdateDate=Get-Date$grainsValue=$grainsupdateDate.ToString("MM/dd/yyyy")$grainsKey='vCROCS_last_grains_update'# Grains Append$sshCommand='salt "'+$minion+'" grains.append '+$grainsKey+' "'+$grainsValue+'"'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End ParamsInvoke-SSHCommand@Params# ----- [ End Adding Grain Data ] ---------------------------------------------------------------------------# ----- [ Sync minion Grain Data ] ------------------------------------------------------------------Write-Host'Syncing Minion Grain Data...'# Sync Grains Data$sshCommand='salt "'+$minion+'" saltutil.sync_grains'#$sshCommand$Params=@{"SessionId"=0"Command"=$sshCommand}# End ParamsInvoke-SSHCommand@Params# ----- [ Disconnect from SaltStack Config Server ] ------------------------------------------------------------------Write-Host'Disconnecting from SaltStack Config Server...'Remove-SSHSession-SessionId0# ----- [ End of Code ] ---------------------------------------------------------------------------
Example grains file after running script
The default location of the grains file is in directory “C:\salt\conf"
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.