is it possible to append a partial template to exsiting file? [chef cookbook]

1.3k Views Asked by At

I'm looking to add configurations to a conf file that is extracted from a tar file.

Currently the cookbook is using a template to overwrite the whole file instead I need to use the original file as a template and add to it.

Partial templates seem to do the task but I haven't been able to find an example of a partial being applied to an existing file,

is it even possible?

thank you

2

There are 2 best solutions below

1
On

It appears you may use the second method. I have tried using partial template from this example but https://github.com/danielsdeleo/partials

0
On

No, you can't really do that. If the original isn't an erb template already, then you can't use it in this manner. You really have two choices.

  1. Create a template that includes the original file as a partial within it. I think this would work, though I've never tried it, and I'm not sure chef's system will let you use a file outside the cookbook as a partial.
  2. Use the FileUtil module from chef to modify the file in place. If you do that, I'd strongly suggest you create an LWRP to wrap it, and create the new file in the chef cache, then compare against the existing file, and only "update" if there is a change.

In reality, your best bet is almost certainly to just use a custom erb template for it. If there is some sort of special data in the original that you need to re-capture, you could use a ruby_block to read that data form the original and then pass it into your template.