Ask Your Question
0

set class variables on command line with puppet agent/apply

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

CBR gravatar image

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

Hello,

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

Comments

1

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 -0500 )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 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

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

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

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:

https://puppet.com/docs/puppet/5.0/langfactsandbuiltinvars.html

edit flag offensive delete link more

Comments

2

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 -0500 )edit

Yes. Absolutely correct.

luksi1 gravatar imageluksi1 ( 2017-12-20 01:44:23 -0500 )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 -0500 )edit

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

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

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

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

Comments

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 -0500 )edit
1

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 -0500 )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 -0500 )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 -0500 )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 -0500 )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

Stats

Asked: 2017-12-18 09:33:42 -0500

Seen: 160 times

Last updated: Dec 20 '17