2

Puppet version is 3.7 and OS is Windows 7.

I am trying to create Puppet exec that would only execute if certain Windows registry value does not exist. I am trying to use code like this:

exec { 'example':
            path => 'C:\Windows\System32',
            command => 'something',
            unless => 'reg query "HKEY_LOCAL_MACHINE\Software\My key" /f 5.1',
}

If I use reg query on command line I get:

C:\>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\My key" /f 5.1
HKEY_LOCAL_MACHINE\SOFTWARE\My key REG_SZ 5.1
End of search: 1 match(es) found.
C:\>echo %errorlevel%
0

Since result this command is 0 and unless should execute only if result is not 0 the command should not execute. However it still gets executed every time.

I also tried using unless => 'cmd.exe /C reg query "HKEY_LOCAL_MACHINE\Software\My key" /f 5.1', but it executes the command every time as well.

Similar question here indicates that this way should work: Exec onlyif registry value is not present.

What am I doing wrong here?

EDIT: Debug shows that Puppet does not find the key at all:

Debug: Exec[update](provider=windows): Executing check 'reg query "HKLM\SOFTWARE\My key" /f 5.1'
Debug: Executing 'reg query "HKLM\SOFTWARE\My key" /f 5.1'
Debug: /Stage[main]/Example/Exec[update]/unless: ERROR: The system was unable to find the specified registry key or value.

If I run the same reg query command on the command line it finds the key as shown above.

Community
  • 1
  • 1
Madoc Comadrin
  • 498
  • 1
  • 12
  • 27
  • Can you add a pertinent excerpt from the output of `puppet agent --test --debug` ? – Felix Frank Feb 25 '15 at 14:18
  • Debug gave new information. Puppet fails to find the registry key I am looking for although the same command finds it when executed on command line. – Madoc Comadrin Feb 26 '15 at 06:47
  • This might be a quoting issue after all. I'm not sure whether the `windows` provider will do the right thing here. Tokenization can be tricky. – Felix Frank Feb 26 '15 at 15:16

2 Answers2

1

Make sure you are not subject to registry redirection or file system redirection. This is usually the case - if you are on a 64-bit Windows OS, it is preferred that you use a 64-bit version of Puppet.

We've noted file system redirector and workarounds in troubleshooting. We've also touched on registry redirection for custom facts. In your case, it could be you are subject to file system redirection as you are attempting to call c:\windows\system32\cmd.exe.

The short of it is, you should ensure that you are using the 64 bit version of cmd.exe, which is either in c:\windows\sysnative or c:\windows\system32. This is addressed by the $system32 fact starting with Puppet 3.7.3:

exec { 'example':
  path => "$system32',
  command => 'something',
  unless => 'reg query "HKEY_LOCAL_MACHINE\Software\My key" /f 5.1',
}
ferventcoder
  • 11,952
  • 3
  • 57
  • 90
0

General tip, previously when we faced the same problem, we escaped the backslashes in the registry key name (and possibly the space character) to get it to work.

    exec { 'example':
                path => 'C:\Windows\System32',
                command => 'something',
                unless => 'reg query \"HKEY_LOCAL_MACHINE\\SOFTWARE\\My\ key\" /f 5.1',
    }

Also, if you want use the same with the "cmd" as follows:

exec { "example":
                command => "something",
                unless => 'cmd /c "C:\\Windows\\System32\\reg.exe query \"HKEY_LOCAL_MACHINE\\SOFTWARE\\My\ key\" /f 5.1"',
 }   
user3278897
  • 984
  • 10
  • 23
  • You haven't overcome the possibility of 32-bit Puppet running on a 64-bit system. So if these were run on a 32-bit Puppet, they would both be subject to Windows File System Redirector. See https://puppetlabs.com/blog/how-avoid-common-windows-gotchas-puppet and the comments. – ferventcoder Jun 04 '15 at 12:29
  • First of all thank you @ferventcoder I didnot know this (I know windows Wow64 implementation on windows).The puppet labs link you provided a good puppetlabs-registry, it is really good, thank you. "ferventcoder" but my question to you, if we specify "C:\Windows\System32\reg query" on a 64-bit windows, then as per the documentation ( the link you provided) it should be redirected to c:\windows\SysWOW64 because it is done by windows directly right? So, my answer should also run on the 32 or 64 bit as routing is done by windows internally(I am sorry 64bitsystem is not available withme right now – user3278897 Jun 05 '15 at 04:56
  • Yes, in a 32-bit process on a 64-bit system the call to C:\Windows\System32 is redirected to C:\Windows\SysWOW64. What can happen though is that will open a 32-bit view of the registry, which may not include the values you see when you normally open regedit (HKLM:\SOFTWARE\Wow6432Node is now HKLM:\SOFTWARE). It's not obvious that Windows does this, and produces the issues like the OP had where Puppet wasn't finding a registry value to even exist - the 64-bit apps exists, they just are not visible to the 32-bit registry view. – ferventcoder Jun 05 '15 at 15:10