Platform neutral way to create a temporary file in Chef Recipe
Asked Answered
S

1

13

I have a chef recipe that uses a template to generate and xml document. The location of that document on the file system is not important; the location simply needs to be handed to a command line tool which will read the file and use it to configure an application. The xml file need not persist beyond the execution of the command. Therefore it seems to me that the file should be created in a platform neutral temporary files directory. C:\Temp on windows or /TMP on most nix machines. Does chef provide a means of creating temporary files, detecting the temporary file location, or otherwise accomplishing the objective?

Sleek answered 17/12, 2014 at 12:56 Comment(0)
A
15

Best bet: use Chef::Config['file_cache_path'] which is used by chef to store transient datas. I.e: template "#{Chef::Config['file_cache_path']}/myfile.xml" do

Left (rephrased) after comment as it still worth being there for general purpose:

As per comment, the execute resource will run anyway with this input to ensure the app is configured accordingly, the file has to be present only when the execute resource is run, for other cases what is below is to be kept in mind.

One of the key idea behind chef is idempotency, chef may run 100 times and do things only when they need to be done.

In this case, the template would be generated once, and never change unless there's a parameter change in attributes.

In such a case (the resulting file should be updated), chef will notice it, replace it and act accordingly with any notification defined on it.

Apprehend answered 17/12, 2014 at 13:6 Comment(2)
Yep, that works. Regarding idempotency, point understood but not pertinent to the current consideration. The command being executed is idempotent and its important that it be run whether or not the template output has changed or not. We want to ensure that the app is configured per the recipe, and overwrite any manual config changes that may have occurred. Such manual changes would not trigger a change in any files.Sleek
Ok, edited the answer, left some part as I find them relevant for generic answer.Apprehend

© 2022 - 2024 — McMap. All rights reserved.