Forum: CFEngine Help
Subject: Re: How-to insert multi line string at the top of file ?
Author: neilhwatson
Link to topic: https://cfengine.com/forum/read.php?3,27226,27245#msg-27245
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}");
}
# Run the agent:
-> Edited file /tmp/nhw_test
!! Method invoked repairs
-> Edited file /tmp/nhw_test
!! Method invoked repairs
Final file:
#### one
#### two
#### three
This is the old first line.
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine