I'm new to puppet and ruby, and just tried to write custom fact but ... Having the following issue
Facter.add("vsphere_installed") do
confine :operatingsystem => :windows
setcode do
if Facter::Util::Resolution.exec('c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "Get-WmiObject -Class Win32_Product | Select-Object -DisplayName | ? {$_.DisplayName -Match "vsphere"}"') = true
result = "vSphere installed"
else
result = "false"
end
end
end
I don't know how exactly to do this, I want to list installed programs and search for one and if true(find) to return that it's installed. This example so far returns only false ....
It feels like you are treating Puppet as procedural at the moment and Puppet is more about the desired state. You determine what is installed, you shouldn't necessarily ask.
So on certain server roles you would say the end state is that you need vSphere and also other software.
You get to make those decisions, you shouldn't use Puppet to discover the state, but to tell it the state and let it do what it does best.
Discovery is something you can do out of band with the tool exploring a machine, try puppet resource package
and you will see what I mean.
But to answer your question, you should probably use a custom executable fact and just use PowerShell directly, because the command string still needs to be escaped in the double quotes (and may also need to be escaped in the way that you used apostrophe then double quotes) - the docs also point to using Facter::Core::Execution.exec
and not Facter::Util::Resolution.exec
.
Use Custom Executable Facts instead.
Also don't use Win32_Product - Win32_Product class can trigger Windows Installer to do a repair on all MSI installed software as a consistency check. It can really cause a machine to do a lot of unnecessary work - its just not a good idea to use it. I'd suggest querying the uninstaller registry keys directly instead.