create_resources and file

478 views
Skip to first unread message

erkan yanar

unread,
Nov 18, 2013, 9:33:55 AM11/18/13
to puppet...@googlegroups.com
Doing create_resources('file',hiera_hash('input'))
works great for some hiera like
input:
/tmp/a.txt:
owner: root
ensure: file
...

Im not able to put any template() stuff in there
input:
/tmp/a.txt:
owner: root
ensure: file
content: template("create_re/aha.erb")

Instead of doing the template lookup. The content of the file is
'template("create_re/aha.erb")'

Any way to dynamically (hiera) define the templates?


Regards
Erkan



--
�ber den grenzen mu� die freiheit wohl wolkenlos sein

Felix Frank

unread,
Nov 21, 2013, 6:01:14 AM11/21/13
to puppet...@googlegroups.com
Hi,

tough call. You may have to replicate the file type in a defined type
like so:

define my_file($owner="root",$mode="644",...,$template="") {

if $template { File[$name] { content => template($template) } }

file { $name: owner => $owner, ... }
}

Then you can create_resource('my_file',hiera('input')) and pass an
actual "template" parameter to your define.

Lots of work, but I feel that at some points, many if not most users
likely feel the need to wrap the file type this way.

HTH,
Felix

erkan yanar

unread,
Nov 22, 2013, 7:50:27 PM11/22/13
to puppet...@googlegroups.com
Ahoi,

On Thu, Nov 21, 2013 at 12:01:14PM +0100, Felix Frank wrote:
> Hi,
>
> tough call. You may have to replicate the file type in a defined type
> like so:
>
> define my_file($owner="root",$mode="644",...,$template="") {
>
> if $template { File[$name] { content => template($template) } }
>
> file { $name: owner => $owner, ... }
> }
>
> Then you can create_resource('my_file',hiera('input')) and pass an
> actual "template" parameter to your define.
>

Damn thats look quite a *little* bit more complicated.
It evens look some kind of hack. Thats ok with me :)
Is there a place to do RTFM about when things are evaluated?
At least so I can understand why I need File[$name]?

thx
Erkan

jcbollinger

unread,
Nov 25, 2013, 11:03:05 AM11/25/13
to puppet...@googlegroups.com


The manual is here: http://docs.puppetlabs.com/puppet/3/reference/.  I recommend you at least read the entire "The Puppet Language" section, and that you at least skim the "Resource Types" and "Functions" subsections of the "Generated References" section.  As far as the code Felix proposed, it uses a resource parameter override.  You can read about that, specifically, here: http://docs.puppetlabs.com/puppet/3/reference/lang_resources.html#amending-attributes-with-a-collector.

And yes, resource parameter overrides are somewhat of a hack, though useful at times.  In this case, however, I think they are unnecessary.  If you are going to add a defined type to hang the behavior on anyway, then why not just make it unconditionally define the content via a template?  Then you can achieve the same thing a bit more naturally via a definition such as this one:

define mymodule::templated_file(
    $owner = 'root',
    $mode='0644',
    ... <no $content parameter> ...
    $template) {
  file { $title:
    owner => $owner,
    mode => $mode,
    # ...
    content => template($template)
  }
}


John

Reply all
Reply to author
Forward
0 new messages