Dynamic / nested / setting a varible with / from a variable in puppet.

787 views
Skip to first unread message

omfg9899

unread,
Jan 21, 2015, 5:24:19 PM1/21/15
to puppet...@googlegroups.com
  Hello all and thanx in advance to anyone that replies to my endless headache here.

  I am trying to do a couple of things, and failing miserably. I will touch on the most important one for the moment.

 I am trying to set a variable based on a variable.  Effectively a dynamic variable.  I can do this in bash all day long. :/ 

class webapps_dt($envir = "${which_envir}", $envir = upcase($envir), $agent_name = "WEBAPP_EC2EAST_$envir_SERVICE, $server = "collector-somehost.atsome-domain.com", $version="5.5.0") {


  The problem is, I can't figure out for the life of me how to nest or embed that variable inside that variable.  I can nest or embed a fact, but not another variable.  In case anyone is wondering, the second thing I am trying to solve is upper casing the original fact.  What I am searching for is the environment in which the machines are built. ( dev / cert / prod ), but I want them to be uppercase like the rest of the string.  That has of course failed too.  So assuming that envir = DEV, I can't get this to work..

 Any ideas?  Thought?  Miracles?


Thanks,

J

Hristo Mohamed

unread,
Jan 22, 2015, 5:06:22 AM1/22/15
to puppet...@googlegroups.com
$var="blabla $var1" is the way so assign it.
But in puppet, you cant reassign variables values in the same scope , so once you do:
$envir = "${which_envir}",
You cant upcase in the same variable name
$envir = upcase($envir).

Try
$envir_upcase  = upcase($evir)



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/8810c5ab-9342-4ccf-b467-f729e488f516%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jan 22, 2015, 9:28:42 AM1/22/15
to puppet...@googlegroups.com

You cannot specify the same parameter name twice in one class's declaration.  It simply doesn't make sense -- they conflict.

You also cannot interpolate a class parameter's value into its own default value.  This stands to reason because the default is relevant only when no value is specified for the parameter.  Moreover, you cannot modify a class parameter's (or any other Puppet variable's) value once it is assigned.  You can, however, create a separate class variable that contains the transformed value you want.

As a separate matter, you cannot reliably interpolate the value of one class parameter into the default value of a different one, though there is an outstanding feature request for that.  In practice, doing so works for some combinations of parameters but not for others, and it is difficult to predict which will and won't work.  You have a few options here, among them:

(1) Similar to the case of transforming a parameter value, you can create a separate class variable (not parameter) in which to assemble the wanted value.  Example:

class webapps_dt($envir = "${which_envir}",  $agent_name = undef) {
  $envir_upcase
= upcase($envir)
 
if $agent_name {
    $agent_name_real
= $agent_name
 
} else {
    $agent_name_real
= "WEBAPP_EC2EAST_${envir_upcase}_SERVICE"
 
}
 
# ... use $agent_name_real

}

(Note the braces around the variable name in the interpolation, by the way.  Since you are proficient with bash scripting, I don't need to explain.)

(2) You can create a wrapper class that performs the data munging you want, and declares the true target class with the right parameters.  The easiest way to do this is just a special case of (1), though, so it's usefulness is limited.  It mainly serves cases where other classes, not themselves declared by the target class, rely on the values of the target class's parameters:

class webapps_dt_wrapper($envir = "${which_envir}",  $agent_name = undef) {
  $envir_upcase
= upcase($envir)
 
if $agent_name {
    $agent_name_real
= $agent_name
 
} else {
    $agent_name_real
= "WEBAPP_EC2EAST_${envir_upcase}_SERVICE"
 
}
 
class { 'webapps_dt':
    envir
=> $envir,
    agent_name
=> $agent_name_real
 
}
}


John

omfg9899

unread,
Jan 26, 2015, 10:27:35 AM1/26/15
to puppet...@googlegroups.com
  jcbollinger,

 Thanks for your help, it worked perfectly, although it did leave me asking two more questions.

What's up with the setting of 'undef' for some of the variables?  They do not appear to be 'required' and if the "undef" is put into "'s appears to fail. ( $agent_name = "undef")
 
 Then it appears that you test to see if the variable that was previously set to 'undef' is set, this leads me to believe that setting it to undef makes puppet ignore it as not being set.  So if your test detects anything other than 'undef', it will set the '<variable>_real' with whatever the variable was set to, except undef.??  Man I know I am making this more complicated than it needs to be but I really want to fully understand this.


  if $agent_name {
    $agent_name_real 
= $agent_name
  
} else {
    $agent_name_real 
= "WEBAPP_EC2EAST_${envir_upcase}_SERVICE"
  
}
  
# ... use $agent_name_real

}

  Thanks,

 J
On Wednesday, January 21, 2015 at 4:24:19 PM UTC-6, omfg9899 wrote:
Reply all
Reply to author
Forward
0 new messages