Ask Your Question

Is there an onlyif condition that can apply to a Puppet class or to a "file" command in a manifest?

asked 2016-07-21 19:03:29 -0600

Conventional gravatar image

I am using CentOS 7 with Puppet Enterprise 4.4.x for my Puppet Master. I am using Windows Server 2012 with Puppet Enterprise 4.x for my Puppet Agents.

My goal is to have a class in a Puppet Manifest apply conditionally to each Windows Server depending on whether or not a file is present.

Is there an onlyif condition that can apply to a Puppet class or to a file command?

Here is what I am trying:

I know how to use generate in a Puppet manifest when the Puppet Agent node is Linux. I do not know how to use it with Windows. When I try it, with either batch commands or PowerShell commands to test the existence of a file, I get an error with the Puppet Agent runs on the Windows Server. The error says "Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '=>' at ...line 86..."

I have tried a variety of different syntax with the generate command. I want to assign a variable to the output of a command that would run on the Puppet Agent node.

$vari = generate('C:\\WindowsPowerShell\\powershell.exe if(test-path ...)')

I have tried to not use the generate command and assign a variable too.

$vari = if(test-path ...)

Nothing seems to work. It seems that my conditional logic will have to be applied to every file command in the manifest. The exec commands can use the onlyif clause that I created. It would be more maintainable to have the class that has a variety of different commands execute based on the Puppet Agent node having a specific file.

I have tried to get a variable from reading a file on the Puppet Agent node. This failed for different reasons. I don't know how to make a file command (or a class of different files) in Puppet be activate conditionally. Also, I want the Puppet manifest with the class (of exec and file commands) to remain in one file. What should I do?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-07-22 08:54:03 -0600

DarylW gravatar image

updated 2016-07-22 08:54:18 -0600

Puppet is a declarative language, and it should be used to describe 'this should be like this'. Other than execs (which are given the onlyif/unless/creates option to help you make them idempotent, which has the side effect of being able to conditionally do things on the system) your only option is to either create your own custom resource, or use a custom facter fact.

The generate function runs on your puppet master, not on the agent. That is only a possible 'workaround' if you are running 'masterless' puppet, with puppet apply on your nodes.

If you need to react to how something is, the best course of action is to create a custom fact that checks for the existence of the file, and use that value in a conditional to wrap whatever other resource you do or don't want to apply.

something similar to the below like this

# yourmodulename/lib/facter/mycustomfile.rb
require 'facter'

if File.exist? '/path/to/your/file' #I'm not sure what the ruby syntax is on windows, try it out in irb first
  Facter.add(:powershell_exists) do
    setcode do

Also, I don't know if this will help you, but have you looked at ?

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

1 follower


Asked: 2016-07-21 19:03:29 -0600

Seen: 7,287 times

Last updated: Jul 22 '16