0

In my chef erb template, I want to get the path the template is being copied to. So it looks like like:

MY_PATH=<%= ###HOW DO I GET THE TEMPLATE PATH### %>

Is there a way to get that?

Jeff Storey
  • 448
  • 1
  • 7
  • 19
  • 1
    You could pass it in as a variable to the template, but I know of no way to devine it. As I understand it, Chef is actually using a library to get a `string` from the ERB and then writing that to a file. – Tejay Cardon Jun 01 '15 at 13:18
  • What @TejayCardon said. You don't want to have special logic in templates. It's like with web applications (MVC-Pattern). Because your template resource in chef needs a filename, you can easily extract the directory to a variable. – Roland Jun 01 '15 at 23:25

1 Answers1

1

Solution using a variable:

file = '/etc/whatever/app.conf'

template file do
  source 'app.conf.erb'
  variables(
    directory: ::File.dirname(file)
  )
  action :create
end

template:

 <%= @directory %>

which value will be: /etc/whatever

Roland
  • 369
  • 3
  • 9