terraform-provider-template icon indicating copy to clipboard operation
terraform-provider-template copied to clipboard

template_dir deletes foreign files

Open hashibot opened this issue 8 years ago • 1 comments

This issue was originally opened by @mandelsoft as hashicorp/terraform#15365. It was migrated here as part of the provider split. The original body of the issue is below.


Terraform Version

0.9.8

Problem

I want to process a bunch of templates into a given target directory. There are two groups of templates, wheras the processing of the second group is optional (meaning it depends on a condition used to set the count attribute). The second one might also be one or more template_file resources.

Basically template_dir looks like the ideal solution for this problem. Unfortunately it isn't if the target directory should be the same.

Actual Behavior

This resource is implemented in a way that treats the complete target directory as resulting resource. The effect is that all additional files not described by the actual template source files are deleted, regardless whether they have been generated by a former execution of the template_dir resource or not.

As a result the content of the directory flickers, the first time terraform executes the folder is empty everything is fine, both resources generate their files into the commonly used target directory. But for every second execution, the folder already contains files not known by the two resources, Therefore they schedule the deletion of the files. If the second one, for example, is a template_file it sees the correct target content during the planning phase, and schedules no action.

So, every second execution this file vanishes and appears again. Similar strange things happen, if both resources are template_dirs.

Not working workarrounds

I tried to use separate target folders and copy the files to a common folder later on. Unfortunately there is no trigger for the null_resource based on the target folder that triggers the copy step if the target folder is deleted. (for example a directory hash)

Expected Behavior

There are for sure scenarios where the actual behaviour is completely fine. But it would be a great improvement if there would be an option to tell the resource to handle only files covered by the resource and not the complete directory. This means, it should delete only files, if they were generated by a former execution of the template_dir resource.

hashibot avatar Jun 23 '17 20:06 hashibot

My target directory was ${path.cwd} - so this just wiped everything, including .git/. Not happy. 😞

This should come with a huge warning such as:

Destination directory is a resource, so direct modifications or externally added files will be lost.

OJFord avatar Nov 05 '17 19:11 OJFord