Optional template files?

168 views
Skip to first unread message

GriffaA10

unread,
Jun 29, 2012, 8:35:22 AM6/29/12
to puppet...@googlegroups.com
Hi,

I'm trying get Puppet to manage my snmpd instances. I have a "base" snmpd.conf constructed from a template, but some hosts require additional config. My plan was to have something like snmpd.conf.$hostname.erb in the templates directory and concatenate this with the base template. However, for the majority of hosts this file will not be present and in those cases I will get a "Could not find template" error.

Is there a method to test for the presence of a file in the template directory, so I can build my template() command accordingly?

Thanks,

Alan

Darren Chamberlain

unread,
Jun 29, 2012, 9:04:06 AM6/29/12
to puppet...@googlegroups.com

* GriffaA10 <alan.griffiths at interoute.com> [2012/06/29 05:35]:
> I'm trying get Puppet to manage my snmpd instances. I have a
> "base" snmpd.conf constructed from a template, but some hosts
> require additional config. My plan was to have something like
> snmpd.conf.$hostname.erb in the templates directory and
> concatenate this with the base template. However, for the majority
> of hosts this file will not be present and in those cases I will
> get a "Could not find template" error.

The file() function returns the content of the first file that
exists, so you can use it with inline_template():

file {
"/etc/snmpd.conf":
ensure => "present",
content => inline_template(
file(
"puppet:///modules/snmp/snmpd.conf.$::hostname.erb",
"puppet:///modules/snmp/snmpd.conf.erb",
)
),
}

--
Darren Chamberlain <dar...@boston.com>

Dan White

unread,
Jun 29, 2012, 10:09:58 AM6/29/12
to puppet...@googlegroups.com
----- Darren Chamberlain <dar...@boston.com> wrote:
>
> The file() function returns the content of the first file that
> exists, so you can use it with inline_template():
>
> file {
> "/etc/snmpd.conf":
> ensure => "present",
> content => inline_template(
> file(
> "puppet:///modules/snmp/snmpd.conf.$::hostname.erb",
> "puppet:///modules/snmp/snmpd.conf.erb",
> )
> ),
> }
>

That is a wonderful tip, Darren.

Thanks !

GriffaA10

unread,
Jun 29, 2012, 10:37:29 AM6/29/12
to puppet...@googlegroups.com
Great, that's done the trick, thanks

Just one thing to note. file() takes an absolute file path (i.e. /etc/puppet/modules/snmp/templates/snmpd.conf.erb). Spent a few minutes trying to figure out the error I was getting before finally RTFM.

R.I.Pienaar

unread,
Jun 29, 2012, 10:45:40 AM6/29/12
to puppet...@googlegroups.com


----- Original Message -----
> From: "GriffaA10" <alan.gr...@interoute.com>
> To: puppet...@googlegroups.com
> Sent: Friday, June 29, 2012 3:37:29 PM
> Subject: Re: [Puppet Users] Optional template files?
>
> Great, that's done the trick, thanks
>
> Just one thing to note. file() takes an absolute file path (i.e.
> /etc/puppet/modules/snmp/templates/snmpd.conf.erb). Spent a few
> minutes trying to figure out the error I was getting before finally
> RTFM.

indeed, not ideal especially with environments.

Ages ago I wrote a little function to bring to template() the same behaviour
as source => [] I dont know if it still works on modern puppet but it
was handy

http://p.devco.net/27/

Reply all
Reply to author
Forward
0 new messages