0

I am trying to set up a Puppet exec resource for a Windows Server based on a Facter variable. But, it looks like onlyif accepts only command files and not an inline expression.

Could someone please help me set this expression in onlyif (either as a conditional expression or as inline-dos command)

exec { 'C:\Users\validate.cmd validate':
  onlyif   =>  "$validate_enabled" == true,   => This not recognized as command
  provider => powershell,
}

Thanks in advance.

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
Raghav
  • 2,890
  • 7
  • 36
  • 56

1 Answers1

2

There are a couple issues here. The first is that the syntax for the onlyif is incorrect. That attribute executes a command and checks for success, so you would want to change it to this to remove Puppet string and shell syntax issues:

exec { 'C:\Users\validate.cmd validate':
  onlyif   => $validate_enabled,
  provider => powershell,
}

Note the first part of what I wrote (executes a command). You do not want to execute a command in this instance. You want to check the value of a boolean Facter fact. Therefore, you would be checking this with Puppet DSL and not a shell command.

Facter 3/Puppet 4:

# use fact hash
if $facts['validate_enabled'] {
  exec { 'C:\Users\validate.cmd validate': provider => powershell }
}

Facter 2/Puppet 3:

# specify global variable for safety
if $::validate_enabled {
  exec { 'C:\Users\validate.cmd validate': provider => powershell }
}

This will fix your conditional and give you your desired behavior.

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67