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,27241#msg-27241
I understand your arguments and documentation also prove it. But I still need
to solve this issue and so come up with 2 possible (not very elegant and
probably, in some cases, buggy) solutions. Plus the one you've recommended -
use template (which is probably the best one).
And so the first one is the obvious and straight forward way to solve issue -
simply reverse the order of multi line string.
# cat promises.cf
body common control
{
bundlesequence => {
"multiline1",
"multiline2",
};
inputs => {
"cfengine_stdlib.cf",
};
}
bundle agent multiline1 {
vars:
"banner" string => "1 line
2 line";
files:
"/root/test1"
edit_line => multiline_edit("$(banner)");
}
bundle agent multiline2 {
vars:
"banner" string => "2 line
1 line";
files:
"/root/test2"
edit_line => multiline_edit("$(banner)");
}
bundle edit_line multiline_edit(string)
{
insert_lines:
"$(string)" location => myloc("1");
}
body location myloc(linenum)
{
before_after => "before";
select_line_number => "$(linenum)";
}
# cf-agent -f /root/cfpol/promises.cf
# cat /root/test1
2 line
1 line
test data
test data
test data
test data
# cat /root/test2
1 line
2 line
test data
test data
test data
test data
#
The second approach is to put marker inside of the file and use this marker as
a starting point to insert multiline string after the marker:
# cat promises.cf
body common control
{
bundlesequence => {
"multiline1",
};
inputs => {
"cfengine_stdlib.cf",
};
}
bundle agent multiline1 {
vars:
"marker" string => "###marker###";
"banner" string => "1 line$(const.n)2 line";
files:
"/root/test1"
edit_line => insert_single_line("$(marker)"),
classes => if_ok("signal_class");
signal_class::
"/root/test1"
edit_line => multiline_edit("$(banner)","$(marker)");
}
bundle edit_line insert_single_line(string)
{
insert_lines:
"$(string)" location => myloc("1");
}
bundle edit_line multiline_edit(string,marker)
{
insert_lines:
"$(string)" location => myloc1($(marker));
}
body location myloc(linenum)
{
before_after => "before";
select_line_number => "$(linenum)";
}
body location myloc1(marker)
{
select_line_matching => "$(marker)";
}
# cf-agent -f /root/cfpol/promises.cf
# cat /root/test1
###marker###
1 line
2 line
test data
test data
test data
test data
#
Maybe this will help somebody, but I still need to point to documentation
https://cfengine.com/manuals/cf3-Reference#insert_005ftype-in-insert_005flines
and warn about the possible problems Neilh have mentioned.
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine