Forum: CFEngine Help
Subject: Re: Deleting extra entries inside config that's built by edit_line(s)
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,25689,25709#msg-25709
Hi Tero,
You can use a delete_lines promise to do this. Below if the full example, but
all that I added was this:
delete_lines:
".*"
select_region => edit_in_between("^\[$(block)\]","^#\"),
delete_select =>
not_starting_with("set_config_values_w_delim_and_block.index");
and the corresponding delete_select body:
body delete_select not_starting_with(s)
{
delete_if_not_startwith_from_list => { @(s) };
}
Best regards,
--Diego
body common control
{
bundlesequence => { "block_conf" };
}
bundle agent block_conf
{
vars:
"conf" string => "/tmp/test.conf";
# List of config blocks we have.
# Value is the without []
# Key is the array variable set below.
"array_ref" string => "USER";
"array_ref" string => "HOST";
"array_ref" string => "API";
# block
"user" string => "randomuser",
policy => "free";
"user" string => "randompass",
policy => "free";
# block
"host" string => "randomhost",
policy => "free";
"host" string => "randomport",
policy => "free";
# block
"api" string => "/randomget",
policy => "free";
"api" string => "/randompost",
policy => "free";
# Open up the array_ref array.
"array_ref_keys" slist => getindices("array_ref");
files:
"$(conf)"
comment => "Set the $(conf) config values",
create => "true",
# Iterate over all available blocks and add the entries.
edit_line =>
set_config_values_w_delim_and_block("block_conf.$(array_ref_keys)","=","$(array_ref[$(array_ref_keys)])");
}
bundle edit_line set_config_values_w_delim_and_block(v,delim,block)
# Sets the RHS of configuration items in the file of the form
# LHS delim RHS _after_ the given block. Also adds the block as such before
entries:
# #
# [$(block)]
# lhs $(delim) rhs
# #
# If the line is commented out with #, it gets uncommented first.
# Adds a new line if none exists.
{
vars:
"index" slist => getindices("$(v)");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
classes:
"secondpass" expression => isvariable("index");
replace_patterns:
# If the line is there, maybe commented out, uncomment and replace with
# the correct value
"^\s*($(index)\s+(?!$(delim)\s+$($(v)[$(index)])).*|# ?$(index)\s+.*)$"
replace_with => value("$(index) $(delim) $($(v)[$(index)])"),
select_region => edit_in_between("^\[$(block)\]","^#\"),
classes => always("replace_attempted_$(cindex[$(index)])");
insert_lines:
"#";
"[$(block)]";
"#";
"$(index) $(delim) $($(v)[$(index)])"
#insert_type => "preserve_block",
select_region => edit_in_between("^\[$(block)\]","^#\"),
ifvarclass => "replace_attempted_$(cindex[$(index)])";
delete_lines:
".*"
select_region => edit_in_between("^\[$(block)\]","^#\"),
delete_select =>
not_starting_with("set_config_values_w_delim_and_block.index");
}
body delete_select not_starting_with(s)
{
delete_if_not_startwith_from_list => { @(s) };
}
body classes always(x)
# Define a class no matter what the outcome of the promise is
{
promise_repaired => { "$(x)" };
promise_kept => { "$(x)" };
repair_failed => { "$(x)" };
repair_denied => { "$(x)" };
repair_timeout => { "$(x)" };
}
body replace_with value(x)
{
replace_value => "$(x)";
occurrences => "all";
}
# Select region between start and end patterns
body select_region edit_in_between(start, end) {
select_start => "$(start)";
select_end => "$(end)";
}
body replace_with comment(c)
{
replace_value => "$(c) $(match.1)";
occurrences => "all";
}
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine