Template file name

35 views
Skip to first unread message

Mark McWhinney

unread,
Dec 10, 2013, 2:44:10 PM12/10/13
to puppet...@googlegroups.com
I have a module that needs to create several, similar config files, so I am using a template.

file { "foo" :
    ensure  => file,
    content => template("MyTempalte.erb"),
  }
file { "bar" :
    ensure  => file,
    content => template("MyTempalte.erb"),
  }

So far, so good. The issue is that I need to put the name of the file into the config file that is created from the template.  I tried <%= file %>, but that gives the name of the template file, MyTemplate.erb  What I need is the name of the config file e.g. foo or bar.  I poked around the documentation but could not the right incantation.  Any ideas?


Felix Frank

unread,
Dec 11, 2013, 6:07:15 AM12/11/13
to puppet...@googlegroups.com
Easiest way is wrapping it in a defined type.

define my_config() {
file { $name: ensure => file,
content => template(...),
}
}

Inside the template, you can use <%= @name %> or <%= @title %>, because
that is the name of the file being managed in this context.

Use the type like

my_config { [ "foo", "bar" ]: }

It is good practice to pass all non-fact non-hiera variables that are
relevant for the template as parameters to your defined type.

HTH,
Felix

Mark McWhinney

unread,
Dec 11, 2013, 7:57:06 PM12/11/13
to puppet...@googlegroups.com
Cool. That worked.  Thank you!
Reply all
Reply to author
Forward
0 new messages