Forum: CFEngine Help
Subject: Re: How-to insert multi line string at the top of file ?
Author: juriskrumins
Link to topic: https://cfengine.com/forum/read.php?3,27226,27256#msg-27256

Executing the policy you've provided (no changes) using my cfengine 
installation gives following results:

# cf-agent -V

   @@@      
   @@@      cf-agent
            
 @ @@@ @    CFEngine Core 3.3.5
 @ @@@ @    
 @ @@@ @    
 @     @    
   @@@      
   @ @      
   @ @      
   @ @      

Copyright (C) CFEngine AS 2008-2012
See Licensing at http://cfengine.com/3rdpartylicenses
# ls /tmp/nhw*
ls: cannot access /tmp/nhw*: No such file or directory
# cat promises2.cf 
body common control {

        bundlesequence => {
                "main",
        };

        inputs => {
                "cfengine_stdlib.cf",
        };
}

bundle agent main {

        vars:
                "edit_file" string => "/tmp/nhw_test";
                "first_line" string => "This is the old first line.";

        methods:

        "any" usebundle => create_file("${edit_file}", "${first_line}");
        "any" usebundle => edit_file("${edit_file}", "${first_line}");

}

bundle agent create_file(file, line) {

        files:
                "${file}"
                        edit_defaults => empty,
                        create => 'true',
                        edit_line => append_if_no_line("${line}");
}

bundle agent edit_file(file, line){

        vars:
                "new_first_lines" string => 
"#### one 
#### two
#### three";

        files:
                "${file}"
                        edit_line => insert_at_top("${new_first_lines}", 
"${line}");
}

bundle edit_line insert_at_top(block, first){

        insert_lines:
                "${block}"
                insert_type => 'preserve_block',
                location => before("${first}");

}
# cf-agent -KI -f /root/cfpol/promises2.cf 
 -> Created file /tmp/nhw_test, mode = 600
 -> Edited file /tmp/nhw_test
 -> Edited file /tmp/nhw_test
# cat /tmp/nhw_test
#### three
#### two
#### one 
This is the old first line.


And this is not what I expect to see. By the way I've also tried things that by 
myself using "preserve_block" with no luck. And that's the reason I've started 
this thread.

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to