On 8/3/11 3:59 PM, [email protected] wrote:
> Forum: CFEngine Help
> Subject: How to add to line with field_edits?
> Author: marc
> Link to topic: https://cfengine.com/forum/read.php?3,22981,22981#msg-22981
> 
> Hi List
> 
> I have a file with a line like this:
> 
> uri server1 server2 server3
> 
> 
> What I wanna do is, to check if the line is there. And if it is, check that 
> is has the right fields. 
> 
You probably want a combination of these two bundles. For a usage
example see below... Looking at these I can see they can use some
tweaking, but it should get you started.


# Appends a line if the regex supplied did not match.
#    files:
#        "myfile.txt"
#               edit_line       =>
append_line_if_no_match(".*foo.*","foobar bas");
bundle edit_line append_line_if_no_match(regex,line) {
    classes:
        "regex_matched" expression =>
regline("^$(regex)$","$(edit.filename)");

    insert_lines:
        !regex_matched::
            "$(line)"
                comment => "Insert line if regex did not match";
}

# Special bundle that appends a subfield on the righthandside (rhs)
# for a specific lhs value (used in ncc.yum.cf a.o.)
bundle edit_line append_subfield(delim1,delim2,lhs,field,values) {
vars:
    "vals"
        comment => "List of subvalues for the rhs",
        slist => { @(values) };

    field_edits:
        "$(lhs).*"
            comment => "Append values to 'multi value rhs lists' (name =
val1:val2)",
            edit_field =>
subcol("$(delim1)","$(delim2)","$(field)","$(vals)","alphanum");
}


###################
To use these do something along these lines:
"/etc/rpm/macros.nfs"
    comment         => "Yum needs to know about nfs mounts.",
    classes         => if_repaired("rpm_macros_fixed"),
    create          => "true",
    edit_line       =>
append_line_if_no_match(".*netsharedpath.*",'%_netsharedpath /ncc:/srv');

"/etc/rpm/macros.nfs"
    comment => "Add some more nfs mounts to existing line",
    classes         => if_repaired("rpm_macros_fixed"),
    edit_line => append_subfield(" ",":","%_netsharedpath ","2","/var/ftp");

-- 
Sjoerd Oostdijck                  RIPE Network Coordination Centre
RIPE NCC, IT Department                  Singel 258, Amsterdam, NL
http://www.ripe.net                                +31 20 535 4444

_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to