Ask Your Question
0

Lookup destination path of a file in a template.

asked 2015-04-06 18:30:52 -0500

updated 2015-04-08 15:46:30 -0500

Is it possible to determine the destination path while a template's being rendered?

file { 'my_cool_file':
  path    => '/etc/somewhere',
  content => template('coolmodule/coolfile.erb'),
}

file { 'another_file':
  path    => '/root/another_file',
  content => template('coolmodule/coolfile.erb')
}
<%-# this is coolmodule/coolfile.erb -%>
# this file's placed at <%= scope.lookupvar(' XXX magic to come here ') %>

So the file placed at /etc/somewhere states # this file's placed at /etc/somewhere and the one at /root/another_file says /root/another_file accordingly?

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted
2

answered 2016-09-05 18:33:03 -0500

edit flag offensive delete link more

Comments

Finally a clean solution. I never liked it when I had to use $title or $name in templates.

pascal gravatar imagepascal ( 2016-09-06 03:31:25 -0500 )edit
0

answered 2015-09-20 16:48:45 -0500

I hope this helps! Please provide Feedback. If anything is not clear tell us, we can make it. If this is a solution for your question please mark the answer as solving solution.

Create a defined type which creates the file-resource in usage of your reuseable template. From your Node Manifest node.pp you call your defined type with or without the parameter absolute_path_with_filename (See the code).

Example:

In your Node Manifest File you call the your defined type:

File: node.pp

node default {
    myModule::reuseableTemplate {'/etc/somewhere':}
    myModule::reuseableTemplate {'/root/another_file':}
}

In order to clarify it, an additional example node which makes usage of the parameter of our defined type.

File: node2.pp

node default {
    myModule::reuseableTemplate {'my_cool_file':
        absolute_path_with_filename => '/etc/somewhere',
    }
    myModule::reuseableTemplate {'another_file':
        absolute_path_with_filename => '/root/another_file',
    }
}
  1. create a puppet module: puppet mopdule generate ForgeUsername-myModule.
  2. create the defined type manifest myModule/manifests/reuseableTemplate.pp.
  3. create the template file myModule/templates/reuseableTemplate.erb.

File: myModule/manifests/reuseableTemplate.pp

define myModule::reuseableTemplate(
  $absolute_path_with_filename = $name,
) {
    $moduleName = "myModule" # hardcoded, because it's inside your Puppet Module with name "myModule"
    $templateFilename = "reuseableTemplate.erb"
    file { $absolute_path_with_filename:
      content => template("${moduleName}/${templateFilename}"),
    }
}

File: myModule/templates/reuseableTemplate.erb

# this is <%= @moduleName %>/<%= @templateFilename %>
# this file has parameter $name=<%= @name %>
# this file has parameter $absolute_path_with_filename=<%= @absolute_path_with_filename %> setuped.

Expected Output:

  • Node: node.pp

File: /etc/somewhere

# this is myModule/reuseableTemplate.erb
# this file has parameter $name=/etc/somewhere
# this file has parameter $absolute_path_with_filename=/etc/somewhere setuped.

File: /root/another_file

# this is myModule/reuseableTemplate.erb
# this file has parameter $name=/root/another_file
# this file has parameter $absolute_path_with_filename=/root/another_file setuped.
  • Node: node2.pp

File: /etc/somewhere

# this is myModule/reuseableTemplate.erb
# this file has parameter $name=my_cool_file
# this file has parameter $absolute_path_with_filename=/etc/somewhere setuped.

File: /root/another_file

# this is myModule/reuseableTemplate.erb
# this file has parameter $name=another_file
# this file has parameter $absolute_path_with_filename=/root/another_file setuped.

Explaining:

Further Informations:

edit flag offensive delete link more
0

answered 2015-04-09 04:34:08 -0500

pascal gravatar image

This is untested by me and relies on behaviour that is not well documented (actually I could not find it in the 3.7 documentation any more, so be warned):

Inside the body of a resource the var $title reveres to the resource title. If the title is your path, you could use it inside the template. If you do this, make sure to document it as use of an undocumented feature, so you know it can break without notice during updates.

edit flag offensive delete link more

Comments

Huh, though it may work – as you already considered – this is only a sub-optimal solution. I'm afraid there won't be any way to do what I'm looking for.

Kai Burghardt gravatar imageKai Burghardt ( 2015-04-09 19:16:29 -0500 )edit
-2

answered 2015-04-07 14:33:50 -0500

WhatsARanjit gravatar image

Easiest way would be to use a variable.

# code.pp
$mypath = '/some/path'
file { $mypath:
  content => template("${modulepath}/template.erb"),
}

# template.erb
<%= @mypath %>
edit flag offensive delete link more

Comments

Yeah, that's what I already do for one file → one template. But as you see above I wanna reuse the same template at multiple places.

Kai Burghardt gravatar imageKai Burghardt ( 2015-04-07 16:15:31 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-04-06 18:30:52 -0500

Seen: 682 times

Last updated: Sep 05 '16