I AM A FUCKING SYSADMIN

  • Archive
  • RSS
  • Ask me a thing.

Defining variables on the fly in Puppet

Pro Puppet Lies!

Ok, a bit of hyperbole there, but it is out of date.

Lately, in an effort to become a better SysAdmin, I’ve been teaching myself Ruby and Puppet in paralel. Fortunately, I’ve got a handy dandy subscription to O’Reilly’s Safari Books Online through work, which means I get free access to Pro Puppet, the quasi-official puppet book!

Unfortunately (in this context), the folks over at Puppet Labs iterate really quickly, and some of the instructions in Pro Puppet are just plain invalid for the latest release, which is 2.7.x at the time of my writing this post, but was 2.6.x when the book was written.

One of the most glaring errors is the way the book instructs us to use a params.pp in each module we write, to store all the variables and mutable parameters we use throughout our modules. This unto itself is a great idea, assuming we’re not using Hiera, because it means that if we want to change a parameter we use throughout a module, we only have to edit it once, in the params.pp file. It ALSO means that we can define a variable in a params.pp file, and then override it locally in a specific class or manifest when necessary.

However, Pro Puppet’s example of how to do this is broken.

Luckily, I’ve figured out how to make it work.

The example params.pp file, more or less:

# .../modulename/manifests/params.pp

class modulename::params {
  $somevar = "some string goes here"
}

Now we attempt to make use of the parameters, as Pro Puppet tells us to:

# .../modulename/manifests/somemanifest.pp

include modulename::params

class modulename::someclass {
  file { '/some/path/to/a/file.conf':
    ensure  => present,
    content => "$somevar",
    owner   => 'root',
    group   => 'root',
  }
}

But wait! This doesn’t work! When you try to apply this manifest in some way, Puppet will throw up warnings about an undefined variable or some-such. Why is this? Why isn’t the “include modulename::params” statement enough? I don’t know for certain, but I suspect it has something to do with Puppet’s effort to remove dynamic scoping from variables in the next release. Although since that page claims that dynamic scoping is merely deprecated and not removed in the 2.7.x release, I’m probably wrong about that.

Anyway, how DO we use a params.pp file in our manifests? Well, it’s a bit more complicated now, but still doable. After much emailing back and forth on the Puppet Users Google Group, I figured out how it’s done.

The mere “include modulename::params” statement is no longer sufficient; we must actually define the variable in the local scope of the class using it as having the value of the variable we intend to use from the modulename::params class we defined - like so:

# .../modulename/manifests/somemanifest.pp

class modulename::someclass {
  include modulename::params ##LOOK AT ME
  $somevar = $modulename::params::somevar ## LOOK AT ME ALSO
  file { '/some/path/to/a/file.conf':
    ensure  => present,
    content => "$somevar",
    owner   => 'root',
    group   => 'root',
  }
}

So what’s the difference?

  • I’ve included the include modulename::params statement inside the actual class “modulename::someclass”, rather than just including it in the manifest somemanifest.pp, outside the scope of the class.
  • I’ve set the variable $somevar equal to the specific variable I want to use in the modulename::params class.

I actually like this for a few reasons:

  1. No weird/unexpected behavior from dynamic scoping of variables, where you try to define it as one thing and it ends up evaluating to something you didn’t expect.
  2. By explicitly defining the local-scope variable as equal to the variable in the params class/manifest, you’ve put the skeleton-code in place for potentially redefining that variable locally in the future.
  3. Other technical reasons that real programmers could probably explain.

For those wondering, the broken code is based on section 2.7 of Pro Puppet, Managing Puppet with the Puppet Module. They actually use the variable in a template in that section, but that was too long of an example to explain here, and the concept is basically the same.

I apologize in advance for a post that I’m certain contains myriad inaccurate statements. I’m just learning. 

Google Groups Discussion

    • #puppet
    • #devops
    • #sysadmin
    • #systems administration
    • #puppet labs
    • #automation
    • #programming
  • 9 months ago
  • 3
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

3 Notes/ Hide

  1. rockettocloud likes this
  2. zacharyalexstern reblogged this from iamafuckingsysadmin and added:
    plug into electrical sockets, now.
  3. iamafuckingsysadmin posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →
Sex, drugs, and DevOps.

Read my non-tech blog here.
  • whoami
  • RSS
  • Random
  • Archive
  • Ask me a thing.
  • Mobile

Copyright - Zachary Alex Stern.

Effector Theme by Pixel Union