Hi, Is it possible to define a function in a template file? For example, I thought it might be a good idea to define my Debian systems'' /etc/apt/sources.list files based on what level of stability and freeness a system should have. So I have this puppet code: #---------- apt.pp ---------------- define apt-sources-list (the_stability=1,the_freeness=1,the_url="http://mirror.phy.bnl.gov/debian/") { $conffile = "etc/apt/sources.list" file { "$conffile": path => "/$conffile", owner => root, group => root, mode => 444, content => template("$conffile.erb") } } class apt-stable { package { "apt" : ensure => present } apt-sources-list { the_stability => 1, the_freeness => 3 } } #-------------------------- And this sources.list.erb template file: #---------- sources.list.erb ---------------- <% def sourcelines(stability=1,freeness=1,url="http://mirror.phy.bnl.gov/debian/") dists = ["sarge","etch","sid","experimental"] frees = ["main","contrib","non-free"] ret = "" for d in 0...stability ret = ret + "deb " + url + " " + dists[d] for f in 0...freeness ret = ret + " " + frees[f] end ret = ret + "\n" end return ret end %> <%= sourcelines(the_stability, the_freeness, the_url) %> #-------------------------- However, running this gives on a node with "apt-stable" class included gives: lycastus:~# puppetd --test err: Could not retrieve configuration: Failed to parse template etc/apt/sources.list.erb: bad value for range in file /etc/puppet/manifests/classes/apt.pp at line 11 Line 11 coresponds to the line in apt.pp holding: content => template("$conffile.erb") I can run this code as a stand-alone ruby program and it works. Am I doing something wrong or is this not supported? Also, is there any good documentation on ERB beyond what is here: http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ ? Thanks, -Brett.
Sorry to reply to myself. The problem was a string coming from puppet being used as an integer by the function in the template. In case anyone cares, I''ve fixed the problem below. Thanks for listening.... -Brett. Brett Viren <bv@bnl.gov> writes:> Hi, > > Is it possible to define a function in a template file? > > For example, I thought it might be a good idea to define my Debian > systems'' /etc/apt/sources.list files based on what level of stability > and freeness a system should have. So I have this puppet code: > > #---------- apt.pp ---------------- > define apt-sources-list (the_stability=1,the_freeness=1,the_url="http://mirror.phy.bnl.gov/debian/") { > $conffile = "etc/apt/sources.list" > file { "$conffile": > path => "/$conffile", > owner => root, > group => root, > mode => 444, > content => template("$conffile.erb") > } > } > class apt-stable { > package { "apt" : ensure => present } > apt-sources-list { > the_stability => 1, > the_freeness => 3 > } > } > #-------------------------- > > > And this sources.list.erb template file: > > > #---------- sources.list.erb ---------------- > <%> > def sourcelines(stability=1,freeness=1,url="http://mirror.phy.bnl.gov/debian/") > dists = ["sarge","etch","sid","experimental"] > frees = ["main","contrib","non-free"] > ret = "" > for d in 0...stability > ret = ret + "deb " + url + " " + dists[d] > for f in 0...freeness > ret = ret + " " + frees[f] > end > ret = ret + "\n" > end > return ret > end > > %> > > <%= sourcelines(the_stability.to_i(), the_freeness.to_i(), the_url) %> > #-------------------------- > > > However, running this gives on a node with "apt-stable" class > included gives: > > lycastus:~# puppetd --test > err: Could not retrieve configuration: Failed to parse template > etc/apt/sources.list.erb: bad value for range in file > /etc/puppet/manifests/classes/apt.pp at line 11 > > Line 11 coresponds to the line in apt.pp holding: > > content => template("$conffile.erb") > > I can run this code as a stand-alone ruby program and it works. Am I > doing something wrong or is this not supported? > > Also, is there any good documentation on ERB beyond what is here: > http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ ? > > Thanks, > -Brett. > > _______________________________________________ > Puppet-users mailing list > Puppet-users@madstop.com > https://mail.madstop.com/mailman/listinfo/puppet-users
Brett Viren wrote:> #---------- sources.list.erb ---------------- > <%> > def sourcelines(stability=1,freeness=1,url="http://mirror.phy.bnl.gov/debian/") > dists = ["sarge","etch","sid","experimental"] > frees = ["main","contrib","non-free"] > ret = "" > for d in 0...stability > ret = ret + "deb " + url + " " + dists[d] > for f in 0...freeness > ret = ret + " " + frees[f] > end > ret = ret + "\n" > end > return ret > end > > %>I know you''ve already fixed this problem, but you will normally want to use <% %> instead of <%= %> here -- the ''='' is used for returning a value. In this case it doesn''t matter, but I wanted it out there just in case. -- Levy''s Law: The truth is always more interesting than your preconception of what it might be. --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com