Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
a puppet function which lookup for variables externally
Ruby Shell
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


A simple function which read yaml files inside puppet modules.
based on rip work on extlookup.

The ylookup.rb code should be copied into /usr/lib/ruby/site_ruby/1.8/puppet/parser/functions/ylookup.rb

Your YAML files should be located inside your modules inside a data directory, e.g: 
Given your module name is common common/data directory should exists.
Then, you can define a default search order in your site.pp, e.g.:

lookup_order = [

Where everything inside '%{..}' will be replaced with another variable content (e.g. facts).

Then, in your manifest, just do:

$myvar = lookup("myvar")

If you would like to use a different order, you may provide it as a second argument.

$myvar = lookup("myvar", ["lookhere/data/%{environment}","lookthere/data/default"]

To help those starting out with YAML and puppet, here are a few examples.

*** Start of YAML example data file ***

singlevalue : legs

examplestring :
  Here is a line of 
  text which spans
  multiple lines

 - cat,dog,hampster
 - pig,goat,sheep
 - duck,goose,swan
 - ant,beetle,millepede

 subhash_key : subhash1_value
 subhash_key : subhash2_value 
 - hash_key_A : value_A
   hash_key_B : value_B
 - hash_key_A : value_C
   hash_key_B : value_D

# recursive variable example
my_app_config_dir : /etc/my_app
my_app_conf : %{my_app_config_dir}/my_app.conf

*** End of YAML example data file ***


In a Manifest (.pp):

ylookup("singlevalue") will return "legs" 
ylookup("examplestring") will return "Here is a line of text which spans multiple lines" 
ylookup("examplelist") will return ["cat,dog,hampster","pig,goat,sheep","duck,goose,swan","ant,beetle,millepede"] 

In a Template (.erb):

<%= scope.function_ylookup(["singlevalue","default"]) %>

<%= scope.function_lookup(["array_of_hashes"]).each do |hash| -%>
  'hash_key_A' is set to value <%= hash['hash_key_A'] %>
<% end -%>

Sample puppet class:

Here is a quick example of looking up an array of string data (which just happens to be comma seperated) and splitting to extract each value in turn. This technique  can be useful in many ways (for example, maintaining a list of shares, mountpoints and mount_options which puppet can then ensure are mounted )

*** Start of puppet example ***

class example_yaml_lookup {

$examplelist = ylookup("examplelist")

define do_stuff () {
        $param0=inline_template('<%= name.split(",")[0] %>')
        $param1=inline_template('<%= name.split(",")[1] %>')
        $param2=inline_template('<%= name.split(",")[2] %>')
        notify{"parma0: ${param0} - param1:  ${param1} - param2: ${param2}":}

do_stuff { $examplelist: }

*** End of puppet example code ***

Here is the expected output on a puppet client.

*** Expected output when puppet runs ***

[root@rhel6-puppettest puppet-lookup]# puppetd -t
info: Caching catalog for <computer_name>
info: Applying configuration version '1300797371'
notice: parma0: cat - param1:  dog - param2: hampster
notice: /Stage[main]/Example_yaml_lookup/example_yaml_lookup::Do_stuff[cat,dog,hampster]/Notify[parma0: cat - param1:  dog - param2: hampster]/message: defined 'message' as 'parma0: cat - param1:  dog - param2: hampster'
notice: parma0: pig - param1:  goat - param2: sheep
notice: /Stage[main]/Example_yaml_lookup/Example_yaml_lookup::Do_stuff[pig,goat,sheep]/Notify[parma0: pig - param1:  goat - param2: sheep]/message: defined 'message' as 'parma0: pig - param1:  goat - param2: sheep'
notice: parma0: duck - param1:  goose - param2: swan
notice: /Stage[main]/Example_yaml_lookup/Example_yaml_lookup::Do_stuff[duck,goose,swan]/Notify[parma0: duck - param1:  goose - param2: swan]/message: defined 'message' as 'parma0: duck - param1:  goose - param2: swan'
notice: parma0: ant - param1:  beetle - param2: millepede
notice: /Stage[main]/Example_yaml_lookup/Example_yaml_lookup::Do_stuff[ant,beetle,millepede]/Notify[parma0: ant - param1:  beetle - param2: millepede]/message: defined 'message' as 'parma0: ant - param1:  beetle - param2: millepede'
notice: Finished catalog run in 2.05 seconds

*** End of expected output when puppet runs ***

*** Debugging ***

You can debug the behavior of ylookup() using several variables in your manifest (generally site.pp):

$lookup_debug = true		# General debugging (all debugging is enabled)
$lookup_file_debug = true	# File lookup debugging only - print what files are being read
$lookup_hash_debug = true	# Debug hashes - print their keys and their values
$lookup_type_debug = true	# Debug return types - print the type (scalar, array, hash, number) of the value being returned.

You must run your puppetmaster in debug mode to view the debug output. E.g.

  puppet master --debug --no-daemonize

*** End of Debugging ***

*** Known issues ***

* Debug output for arrays and hashes is flattened. E.g. 'element1element2' or 'keyvalue'
* Numbers are returned as integers or floating point, not strings. Work around: surround value in YAML with quotes (' or ").
* Use of common Ruby idioms are likely lacking
* Debugging variables could/should be coalesced into something more expressive.
* var_to_fact() debug variable is local, not global.  

*** End of Known issues ***

Something went wrong with that request. Please try again.