Search code examples
chef-infrachef-recipe

Chef attributes in files


I have a file under

cookbooks/recipe/files/default/file

where I need to add the default attribute <%= node['hostname'] %>.

I know I could put the file below templates/, however I need it to be under files/.

I am having the attribute defined above to work, Chef doesn't parse the file it looks like. Since on the node the files comes in raw text.

How can this be solved? Since the same variable worked with files under templates/, what is the difference?


Solution

  • tl;dr You can't.

    The difference is exactly what you encountered. The cookbook_file resource copies the original file from the cookbook, while the template passes it through the Erubis templating engine.

    You can't do that with the file resource. Just use a template resource instead (and move it to templates/). This procedure is totally normal, when you decide that you have to make a file parametrized. I don't believe your argument "I need it to be under files/". Just move it.