Hi again Markus,
If I understand the question correctly, then the answer is yes. If we modify the example a bit, we can see this in action:
```cf3
body copy_from cp(source) {
source => "$(source)";
}
bundle agent main {
vars:
"temp_file"
string => "/tmp/TEMP.cfengine";
"dest_file_1"
string => "/tmp/file-1.txt";
"dest_file_2"
string => "/tmp/file-2.txt";
files:
"$(temp_file)"
create => "true",
edit_template => "/tmp/my_template.tmpl",
template_method => "cfengine";
"$(dest_file_1)"
copy_from => cp("$(temp_file)"),
if => fileexists("$(temp_file)"),
unless => not(returnszero("/usr/bin/grep --quiet 'Hello CFEngine' $(temp_file)", noshell));
"$(dest_file_2)"
copy_from => cp("$(temp_file)"),
if => fileexists("$(temp_file)"),
unless => not(returnszero("/usr/bin/grep --quiet 'Hello World' $(temp_file)", noshell));
}
```
Let's create our template file:
```
# echo "Hello CFEngine" > /tmp/my_template.tmpl
```
Now we run the agent and see that `/tmp/file-1.txt` is created:
```
# cf-agent -KIf ~/
example.cf info: Created file '/tmp/TEMP.cfengine', mode 0600
info: Inserted the promised line 'Hello CFEngine' into '/tmp/TEMP.cfengine' after locator
info: insert_lines promise 'Hello CFEngine' repaired
info: Edited file '/tmp/TEMP.cfengine'
info: Copied file '/tmp/TEMP.cfengine' to '/tmp/file-1.txt.cfnew' (mode '600')
info: Moved '/tmp/file-1.txt.cfnew' to '/tmp/file-1.txt'
info: Updated file '/tmp/file-1.txt' from 'localhost:/tmp/TEMP.cfengine'
```
Let's modify our template and run it again:
```
# echo "Hello World" > /tmp/my_template.tmpl
# cf-agent -KIf ~/
example.cf info: Inserted the promised line 'Hello World' into '/tmp/TEMP.cfengine' after locator
info: insert_lines promise 'Hello World' repaired
info: Edited file '/tmp/TEMP.cfengine'
info: Copied file '/tmp/TEMP.cfengine' to '/tmp/file-1.txt.cfnew' (mode '600')
info: Backed up '/tmp/file-1.txt' as '/tmp/file-1.txt.cfsaved'
info: Moved '/tmp/file-1.txt.cfnew' to '/tmp/file-1.txt'
info: Updated '/tmp/file-1.txt' from source '/tmp/TEMP.cfengine' on 'localhost'
```
Here something unexpected happens... Due to pre-evaluation (see
https://docs.cfengine.com/docs/3.21/reference-language-concepts-normal-ordering.html#top), the functions in the `if` and `unless` attributes are evaluated and cached before the promises are executed. Thus, the conditions are initially correct for `/tmp/file-1.txt`, but not for `/tmp/file-2.txt`. Then, `/tmp/TEMP.cfengine` is modified, and the wrong file gets updated. If we run the agent again, we can see that the correct file gets updated, but now the other file still contains the wrong content.
```
# cf-agent -KIf ~/
example.cf info: Copied file '/tmp/TEMP.cfengine' to '/tmp/file-2.txt.cfnew' (mode '600')
info: Moved '/tmp/file-2.txt.cfnew' to '/tmp/file-2.txt'
info: Updated file '/tmp/file-2.txt' from 'localhost:/tmp/TEMP.cfengine'
```
We can fix this by making sure the two other `files` promises are out of context until after the rendering the temporary file `/tmp/TEMP.cfengine`. We can do this by using the classes attribute as shown below:
```
body copy_from cp(source) {
source => "$(source)";
}
body classes check_if_changed
{
promise_repaired => { "it_was_changed" };
}
bundle agent main {
vars:
"temp_file"
string => "/tmp/TEMP.cfengine";
"dest_file_1"
string => "/tmp/file-1.txt";
"dest_file_2"
string => "/tmp/file-2.txt";
files:
"$(temp_file)"
create => "true",
edit_template => "/tmp/my_template.tmpl",
template_method => "cfengine",
classes => check_if_changed;
it_was_changed::
"$(dest_file_1)"
copy_from => cp("$(temp_file)"),
if => fileexists("$(temp_file)"),
unless => not(returnszero("/usr/bin/grep --quiet 'Hello CFEngine' $(temp_file)", noshell));
"$(dest_file_2)"
copy_from => cp("$(temp_file)"),
if => fileexists("$(temp_file)"),
unless => not(returnszero("/usr/bin/grep --quiet 'Hello World' $(temp_file)", noshell));
}
```
Now the promises for `/tmp/file-1.txt` and `/tmp/file-2.txt` are executed if and only if the template `/tmp/TEMP.cfengine` changed.
```
# echo "Hello CFEngine" > /tmp/my_template.tmpl
# cf-agent -KIf ~/
example.cf info: Inserted the promised line 'Hello CFEngine' into '/tmp/TEMP.cfengine' after locator
info: insert_lines promise 'Hello CFEngine' repaired
info: Edited file '/tmp/TEMP.cfengine'
info: Copied file '/tmp/TEMP.cfengine' to '/tmp/file-1.txt.cfnew' (mode '600')
info: Moved '/tmp/file-1.txt.cfnew' to '/tmp/file-1.txt'
info: Updated file '/tmp/file-1.txt' from 'localhost:/tmp/TEMP.cfengine'
# echo "Hello World" > /tmp/my_template.tmpl
# cf-agent -KIf ~/
example.cf info: Inserted the promised line 'Hello World' into '/tmp/TEMP.cfengine' after locator
info: insert_lines promise 'Hello World' repaired
info: Edited file '/tmp/TEMP.cfengine'
info: Copied file '/tmp/TEMP.cfengine' to '/tmp/file-2.txt.cfnew' (mode '600')
info: Moved '/tmp/file-2.txt.cfnew' to '/tmp/file-2.txt'
info: Updated file '/tmp/file-2.txt' from 'localhost:/tmp/TEMP.cfengine'
```
I hope this helps!