How to Use Conditional Logic With A Puppet Manifest For Executing a Command

Scenario:  You want an exec command in a manifest to be invoked only if a certain file is on the Windows Server.  You do not want the execution of a command to happen if a file does not exist in a certain location.
Solution:  Incorporate the following lines into your manifest (.pp file).
exec { 'cont_int":
    command => "echo continual > contint.txt",
    provider => powershell,
    path => 'c:\\temp\\',
    onlyif => 'if(test-path c:\\temp\\contualint.txt) {exit 0;} else {exit 1;}',

This is an example of a command that will only execute on a Puppet Agent node if c:\temp\continualint.txt is present.  If it is not found, then it will not run.

Leave a comment

Your email address will not be published. Required fields are marked *