Ask Your Question
0

exec onlyif not working on windows

asked 2018-07-12 08:06:51 -0600

daniel.serrao gravatar image

Hi,

I'm trying to make sure that a Splunk Forwarder instance is always running, but I don't want to execute the command to start Splunk every time that Puppet runs, so I'm using the following resource:

exec {'Start Splunk':
  command => 'splunk start',
  path    => 'C:/SplunkUniversalForwarder/bin',
  onlyif  => 'splunk.exe status | findstr /i "Stopped"',
  require => Exec['Install splunk forwarder'],
}

In the windows cmd, if I execute the command splunk.exe status | findstr /i "Stopped" I get SplunkForwarder: Stopped. Then if I execute echo Exit Code is %errorlevel% I get Exit Code is 0.

According to the documentation, the command 'splunk start' in the exec should be executed when the command inside the onlyif return exit code 0, but in this case is never executed. I'm assuming this problem or difference is related to Windows since I never had problems in using the onlyif on Linux.

Anyone knows how to solve this?

I'm using Puppet 5.5 and Windows Server 2012 R2.

Thanks for any help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-08-02 02:26:47 -0600

joshc gravatar image

Puppet expects onlyif (or unless) to be an executable and optional arguments that can be passed to CreateProcess. However, you're trying to pipe the output of splunk status to findstr. To do that you need to run the command in a shell, something like:

onlyif => 'cmd.exe /c splunk.exe status | findstr /i "Stopped"'

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

Stats

Asked: 2018-07-12 08:06:51 -0600

Seen: 103 times

Last updated: Aug 02