Madoc Comadrin
Madoc Comadrin

Reputation: 568

Puppet: Conditional exec using unless-option

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.

Upvotes: 2

Views: 3274

Answers (2)

user3278897
user3278897

Reputation: 1004

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"',
	}			

Upvotes: 0

ferventcoder
ferventcoder

Reputation: 12551

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',
}

Upvotes: 1

Related Questions