Ask Your Question

how to run a class with multiple execs only if a condition is true?

asked 2015-06-13 08:08:34 -0600

Yeayu gravatar image

updated 2015-06-13 09:01:03 -0600

Hello all,

I need to configure an application by modifying a set of registry keys. I am planning to do that by running "reg.exe /add windows command a few times (once per key to be modified), as follows:

class modifyApp {
    exec { "reg1":
        command => "C:\\Windows\\System32\\reg.exe add HKLM\\SOFTWARE\\application /v Data1 /t REG_BINARY /d test1 ",
        unless => "C:\Windows\\System32\\reg.exe query HKLM\\SOFTWARE\\application /v Data1 | findstr test1",}
   exec { "reg2":
        command => "C:\\Windows\\System32\\reg.exe add HKLM\\SOFTWARE\\application /v Data2 /t REG_BINARY /d test2",
        unless => "C:\Windows\\System32\\reg.exe query HKLM\\SOFTWARE\\application /v Data2 | findstr test2",}

However, I need to only execute those commands if the application is installed (application's file path exists for example). How can I implement that at the class level if I am already using the unless parameters for the execs defined? I have thought to create a custom fact (application exists), so I can rely on an if condition, but I was wondering whether there is any other way.

Any help would be much appreciated.

edit retag flag offensive close merge delete


Why don't you do it by a single resource ? Like you can define a subshell and execute multiple commands using only one unless

Tawha gravatar imageTawha ( 2015-06-13 16:27:50 -0600 )edit

ummm... I am not entirely sure how to do this, can you please give me an example? Thanks

Yeayu gravatar imageYeayu ( 2015-06-13 17:30:01 -0600 )edit

you want to do it on windows right ? i am not very much familier with the windows forum , i have been testing this on Solaris and Linux, in solaris and linux you can separate commands using ";" to execute multiple commands. Another way of doing it is to invoke subshell.

Tawha gravatar imageTawha ( 2015-06-14 05:45:53 -0600 )edit

Subshell can be invoked by "bash -c ". I can share the full command if you want. but as i mentioned, i am a linux geek :)

Tawha gravatar imageTawha ( 2015-06-14 05:46:35 -0600 )edit

2 Answers

Sort by » oldest newest most voted

answered 2015-06-14 18:47:39 -0600

cbarbour gravatar image

What you're trying to do breaks Puppet's declarative model. It would be much better to use the registry type and provider from the Puppet forge to manage registry entries in a declarative way.

Ideally, Puppet should manage the application, and only add the registry entries if the application is assigned to the node. However, if that's not viable in your environment, you can use a custom fact. It would be a pretty trivial exec statement.

To answer your main question...

onlyif and exec accept array inputs. You could even do something like this for clarity:

$app_install_test = 'C:\myapp\myapp.exe /?'
$reg_test = 'C:\Windows\System32\reg.exe query HKLM\SOFTWARE\application /v'
$reg_add = 'C:\Windows\System32\reg.exe add HKLM\SOFTWARE\\application /v'

exec { "reg1":
    command => "${reg_add} Data1 /t REG_BINARY /d test1",
    unless => [
      "${reg_test} Data1 | findstr test1",

Note that you don't need to escape backslashes in single quotes. With windows, intelligent use of single quotes and variable interpolation dramatically improves readability.

edit flag offensive delete link more


Many thanks, this is exactly what I was looking for. I did not know that unless could accept arrays.

Yeayu gravatar imageYeayu ( 2015-06-15 12:23:51 -0600 )edit

BTW... You might need to use onlyif for all install test. I don't recall off the top of my head, but I think unless statements may be or-ed together. Check the docs.

cbarbour gravatar imagecbarbour ( 2015-06-16 10:16:30 -0600 )edit

answered 2015-11-25 23:52:49 -0600

exec { “execute_muliple”: command => “cp /tmp/a.txt /etc/puppet/modules ; rm /tmp/a.txt” } multiple commands can be separated by ;

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-06-13 08:08:34 -0600

Seen: 4,494 times

Last updated: Nov 25 '15