Re: Template validation

Markus Rexhepi-Lindberg <[email protected]> Mon, 18 Mar 2024 05:36:34 -0700 (PDT)
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Hi Lars,

Thanks for the example! Much more elegant than my implementation :-)

If the content of the `$(temp_file)` would change would it then trigger the 
other file(s) to be updated as well (if the unless is *still* false)? If 
that's the case then I assume the `$(temp_file)` can be templated using 
something like the `edit_template` attribute.
On Friday, March 15, 2024 at 3:25:57 PM UTC+1 [email protected] wrote:

> Hi Markus,
>
> We don't have an attribute like `validate` in CFEngine as far as I know. 
> However, the same functionality can be achieved by splitting up the 
> promise. One promise for rendering a temporary file, another for copying 
> that file into the final destination. Here is an example:
>
> ```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)"
>       content => "Hello CFEngine";
>
>     "$(dest_file_1)" # This file will pass validation
>       copy_from => cp("$(temp_file)"),
>       if => fileexists($(temp_file)),
>       unless => not(returnszero("/usr/bin/grep --quiet 'Hello CFEngine' 
> $(temp_file)", noshell));
>
>     "$(dest_file_2)" # This file will not pass validation
>       copy_from => cp("$(temp_file)"),
>       if => fileexists($(temp_file)),
>       unless => not(returnszero("/usr/bin/grep --quiet 'Hello World' 
> $(temp_file)", noshell));
> }
> ```
>
> I used the `if` attribute to make sure the file is created before I 
> running the command in the `unless` attribute. I use them in this order, 
> because `if` is executed before `unless`.
>
> By running the example, we can see that only '/tmp/file-1.txt' was created 
> (with the exception of '/tmp/TEMP.cfengine' ofc.)
>
> ```
> # cf-agent -KIf ~/example.cf 
>     info: Created file '/tmp/TEMP.cfengine', mode 0600
>     info: Updated file '/tmp/TEMP.cfengine' with content 'Hello 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'
> ```
>
> However, this is quite a bit of code which compared to Ansibles one liner. 
> The validate attribute sounds like a very nice feature, thus I created a 
> ticket (see https://northerntech.atlassian.net/browse/CFE-4356) to add it 
> to CFEngine as well. If you plan on using this multiple places, you can 
> consider putting it into a bundle for reusability. 
>
> On Friday, March 15, 2024 at 9:37:29 AM UTC+1 Markus Rexhepi-Lindberg 
> wrote:
>
>> Hi,
>>
>> Is there something equivalent to the validate function that is used for 
>> the template module in Ansible [1] for CFEngine's template engine? I have 
>> built some logic to validate the `/etc/ssh/sshd_config` config file for the 
>> sshd application before writing to it in one of my CFEngine policies but it 
>> has some drawbacks and it is not really an elegant solution.
>>
>> [1] 
>> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html#parameter-validate
>>
>

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/e0fb4b0c-ac42-4d5c-95af-78645b92f08fn%40googlegroups.com.