Forum: CFEngine Help
Subject: Re: "real" templates
Author: toddnni
Link to topic: https://cfengine.com/forum/read.php?3,23837,23882#msg-23882
Marco,
I don't exactly see what kind of help you need, but I will try describe how I
would edit files. Maybe this can be useful to someone else too. I would say
that there are three ways to edit files in cfengine
1. Simple edit_line promises like insert_file_if_no_line_matching(...)
2. Templates, I wrote about this in my previous post.
3. Complicated edit_line bundle promises.
In your case I would prefer a complicated edit_line bundle. To my understanding
the other posters have also suggested this, but I want to make myself clear and
give an example
bundle agent conf_ntp {
files:
any::
"/etc/ntp.conf"
comment => "Cfengine builds the file using build_ntp_conf, and if it is
changed then cfengine updates the file.",
edit_defaults => empty,
create => "true",
edit_line => build_ntp_conf;
}
bundle edit_line build_ntp_conf {
vars:
any::
"variable" string => "version 2";
has_servers_list|has_some_other_list::
"servers" string => "iburst";
"servers" string => "maxpoll 9";
"servers" string => "iburst maxpoll 9";
"all_servers" slist => getindices("servers");
insert_lines:
any::
"# Some header with $(variable)";
has_servers_list::
"#";
"#";
# Select_region example
"server $(all_servers) $(servers[$(all_servers)])"
comment => "Iterate over servers and use select_region",
select_region => in_between("^\",
"^[\END servers\]");
has_text_block::
"#";
"#";
"This should work
OK inside select_region."
insert_type => "preserve_block",
select_region => in_between("^\",
"^[\END block\]");
has_some_other_list::
# More tricky example (inserts after header)
"#
#"
insert_type => "preserve_block",
location => after_first("# Some header.*");
"server $(all_servers))"
select_region => in_between("^\",
"^[\END other_list\]");
any::
"Some random line";
}
body edit_defaults empty {
empty_file_before_editing => "true";
}
body select_region in_between(start, end) {
select_start => "$(start)";
select_end => "$(end)";
}
body location after_first(lineexp) {
select_line_matching => "$(lineexp)";
before_after => "after";
first_last => "first";
}
And the file will converge to
# Some header with version 2
#
server server2
server server3
server server1
#
#
server server2 maxpoll 9
server server3 iburst maxpoll 9
server server1 iburst
#
#
This should work
OK inside select_region.
#
Some random line
I use select_regions to define file structure. Inside (or outside) the regions
line order is not well defined.
It is easiest to use only one edit_line bundle. When you split the bundle you
need to consider insert_lines locations (if you want to define order) and you
cannot use edit_defaults => empty.
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine