Ask Your Question

set class variables on command line with puppet agent/apply

asked 2017-12-18 09:33:42 -0600

CBR gravatar image

updated 2017-12-18 09:59:52 -0600


I know that a variable can be set from command line using Facter_is_myvar=true puppet agent -t but what I'm looking for is something like this. Let's say that the variable I want to set is in this class tomcat::profile::install my_var is the class variable in tomcat::profile::install.

How can I set it from command line using Facter_ ?

Thank you.

edit retag flag offensive close merge delete



No, you can not. Class variables are evaluated by Puppet's parser on the master. Each `class { … }` creates a new scope for variables you can't override/edit (except in inheriting classes). If a class variable doesn't already depend on $facts['is_myvar'] you can't manipulate it that way.

Kai Burghardt gravatar imageKai Burghardt ( 2017-12-19 15:48:56 -0600 )edit

thanks Kai. I will take your comment as an answer as this is not possible.

CBR gravatar imageCBR ( 2017-12-20 08:33:19 -0600 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-12-19 15:19:26 -0600

luksi1 gravatar image

updated 2017-12-20 02:51:59 -0600

You can use custom command-line facts in your manifest just like any other facts by specifying the variable $facts['mycustomfact']

For instance:

export FACTER_bar='foo'

class foo { notice($::bar) } 
include ::foo

Or if you need to set a default in your manifest and then override it from a fact, either data-in-modules with Hiera, or a params pattern should fit your needs.

class foo ( 
   $bar = $::foo::param::bar
) inherits ::foo::param { notice($bar) }

class foo::param {  
  if ($facts['bar']) {
    $bar = $facts['bar']   
  } else {
    $bar = 'this is my default'  

include ::foo

Take a look here for more information on using facts in your manifests:

edit flag offensive delete link more



Facter variables appear in the $facts hash. That they're accessible via the top-scope is a legacy “feature”.

Kai Burghardt gravatar imageKai Burghardt ( 2017-12-19 15:43:38 -0600 )edit

Yes. Absolutely correct.

luksi1 gravatar imageluksi1 ( 2017-12-20 01:44:23 -0600 )edit

I've updated my answer with a facts hash (removing the legacy version), as well as how you might use a default value as a parameter and overide that value with a custom command-line fact.

luksi1 gravatar imageluksi1 ( 2017-12-20 02:54:10 -0600 )edit

this is the solution I'm going with. Thank you @luksi1

CBR gravatar imageCBR ( 2018-01-11 11:37:33 -0600 )edit

answered 2017-12-19 08:54:20 -0600

DarylW gravatar image

The only way to set/override paramters would be either through hiera, or through calling your own manifest which has the values explicity defined..

I don't believe that you can override hiera calls on the command line.

What are you trying to accomplish here? If you are trying to 'test', you would have to modify the class (or have a wrapper arond it) to pass the value of a specific fact in for the parameter to the class in question. Your other option is to set some override values in hiera, and then have your hierachy trigger based on a fact if you only want to trigger it in testing witout breaking' the rest of the environment', or better yet, put your 'debug code' in an isolated puppet environment, and switch the environment on just that node to point to your 'debug' environment.

edit flag offensive delete link more


You can certainly overide a variable with a custom fact, which is what the questioner was asking.

luksi1 gravatar imageluksi1 ( 2017-12-19 15:14:27 -0600 )edit

You can’t arbitrarily set $tomcat::install::foo by setting a fact, that’s not how variables work... if you have an uninitialized variable, you can use the legacy method of accessing a fact as if it was a variable, but you cannot override an existing value... as Kai mentioned above..

DarylW gravatar imageDarylW ( 2017-12-19 16:21:34 -0600 )edit

I think I'm confused about what the questioner is asking. If he is trying to override a variable, it's a no-go. If he's trying to set a variable that will be used in a manifest, which is how I interpreted his question, command-line or external facts should be an option.

luksi1 gravatar imageluksi1 ( 2017-12-20 01:40:41 -0600 )edit

The main difference is that you can't 'override' the value of $tomcat::profile::install::myvar if it is a parameter, but if it is a default parameter that would otherwise pull from a fact, you could do that, but not 'after the fact'. if you have a loose variable within a class, I believe that ...

DarylW gravatar imageDarylW ( 2017-12-20 10:19:48 -0600 )edit

...the legacy facter way of accessing it won't override the value that is assigned into it, but if the variable didn't exist and you were using it (which was possible in old puppet, but I don't know if it is in new puppet) then you could set a value

DarylW gravatar imageDarylW ( 2017-12-20 10:20:34 -0600 )edit

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: 2017-12-18 09:33:42 -0600

Seen: 520 times

Last updated: Dec 20 '17