Thursday, May 16, 2013

Overriding attributes of collected exported resources

http://ttboj.wordpress.com/2013/05/14/overriding-attributes-of-collected-exported-resources/

Tuesday, May 14, 2013 3:53 AMOverriding attributes of collected exported resourcesPlanet DevOpsjamesjustjames

This post is about a particularly elegant (and crucial) feature in puppetexported resourcesattribute overriding. If you're not already familiar with exported resources, you should start there, as they are the killer feature that makes configuration management with puppet awesome. (I haven't found any explicit docs about this feature either, so feel free to comment if you know where they're hidden.)

Setup: I've got a virtual machine which exports a resource to N different nodes. I'd like to define the resource with just one exported (@@) definition on my virtual machine.

Problem: One (or more) of the attributes needs to be changed based on which node it gets collected on. To make things more complicated, I'm using the same class definition on each of those N nodes to collect the resource. I don't want to have to write N separate node definitions:

@@some::resource { 'the_name':      foo => 'bar',      #abc => 'different_on_each_node',      tag => 'magic',  }

Solution: It turns out that for exported (or virtual) resources, you can specify attributes that get set upon collection. Naturally they can depend on a variable such as $name, which is unique to where they get collected:

Some::Resource <<| tag == 'magic' |>> {      abc => "node-${name}",    # override!  }

Bonus: You can obviously use other variables throughout including in the collection (tag == 'magic') area, on both the source and the destination. Instead of a simple equality like I've used, you can actually specify a more complex expression, including other variables such as title (the $name).

Hope this takes your puppet coding to another level,

Happy hacking,

James

 

 



No comments:

Post a Comment