Coder Social home page Coder Social logo

puppet-lookup's Introduction

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

usage:
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 = [
    "%{sitemodulename}/data/hostname/%{hostname}",
    "%{sitemodulename}/data/%{hosttype}/%{hostmode}",
    "%{sitemodulename}/data/%{hosttype}/default",
    "%{sitemodulename}/data/%{hostmode}",
    "%{sitemodulename}/data/default",
    "%{modulename}/data/%{company}",
    "%{modulename}/data/default"
]

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"]

Examples: 
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

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

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

Usage: 

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"] 

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 ***

puppet-lookup's People

Contributors

ohadlevy avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.