Unless conditional statement not working [closed]

I installed several applications using exec, include and class. Then when puppet agent runs every certain amount of time it installs them again. To avoid this I created a file in a directory every time I installed an application, so if the file exists it doesn't get installed again. It works for the installations where I use exec using the onlyif clause. For the instances where I use include or class I tried the unless conditional statement but is not working for me. Can someone please let me know what am I doing wrong or another way to make sure the installations only occur once? I am new to puppet and not familiar with custom facts. Here is a code with class:

The conditional unless has the opposite meaning of if. Unless will only run the associated block if the condition is false. So the code in the block will never be run.

Unless, if, class names, setting variables, running functions and resolving definitions happen a compile time, usually on a Master. Obtaining facts, executing resources and reporting results happen at run-time on an agent.

If the purpose of this code is to determine a property of an agent then there are only three methods of using this at compile time.

Function running on the Puppet Master

Fact running on the Puppet agent

Exec special attributes running on the Puppet agent

Functions are called by the master only at compile time. The agent never sees any functions.

If a file is being created on the master then a Ruby function can be written and called to check for this file on the master. This also works in master-less setups since every agent is also its own master in that model.

Functions are Ruby code. Ruby code is added and updated by agents during Puppet runs. So the custom function is only available after the Master runs its agent once the code is published to the Master.

If a file is being created on the agent then a custom fact can be written to check for the file. This means that the information on the file will only be available on the next Puppet agent run after the file exists. This also means that you have to write Ruby code to test for the file.

Lastly the special Exec resource has agent-run-time only attributes as you have seen. These can run arbitrary system commands during the agent run to block the operation of the exec.

As an aside, Exec resources are considered poor form. Use of Exec moves the control from the master to the agent. Their command attributes can contain side-effects.

Unfortunately the alternative to an exec resource is to re-write the simple exec into a native type and provider. Writing native types and providers involves Ruby programming much more complicated than a short fact script or small function definition.

Comments

Thank you for the explanation! I suspected it was taking the '/usr/bin/test...' as a string instead of evaluating it. I guess a solution would be to use a custom fact to evaluate if the file exists, then the unless statement should work. Not familiar with them but always wiling to learn.