Daniel,

I have been working on a similar problem, and came up with the
following two bundles for editing files. I think both of these could
be used to edit an /etc/hosts file, one by passing the pattern, e.g.

edit_line => replace_or_add(".*\s+hostname", "1.2.3.4     hostname")
# 1.2.3.4 is the new IP address

The second one can't be used as-is since the value you want to change
(the IP address) is in the first field, I think the necessary change
would be to change the "2" to "1" in the second call to col(), and
maybe some other changes, I think this could certainly be used as a
starting point.

Cheers,
--Diego


# Replace a pattern in a file with a single line.
# If the pattern is not found, add the line to the file.
# The pattern must match the whole line (it is automatically
# anchored to the start and end of the line) to avoid
# ambiguity.
bundle edit_line replace_or_add(pattern,line)
{
replace_patterns:
  "^${pattern}$"
  replace_with => value("${line}"),
  classes => always("replace_done");

insert_lines:
  replace_done::
  "${line}";
}

# Sets the RHS of configuration items in the file of the form
#   LHS RHS
# If the line is commented out with #, it gets uncommented first
# Adds a new line if non exists.
# The argument is an associative array containint v[LHS]="rhs"

# Based on set_variable_values from cfengine_stdlib.cf, modified
# by Diego Zamboni to use whitespace as separator, and to handle
# commented-out lines.
bundle edit_line set_config_values(v)
{
vars:
  "index" slist => getindices("$(v)");

  # Be careful if the index string contains funny chars
  "cindex[$(index)]" string => canonify("$(index)");

field_edits:

  # If the line is there, but commented out, first uncomment it
  "#+$(index)\s+.*"
     edit_field => col("\s+","1","$(index)","set");

  # match a line starting like the key something
  "$(index)\s+.*"
     edit_field => col("\s+","2","$($(v)[$(index)])","set"),
        classes => if_ok("not_$(cindex[$(index)])");

insert_lines:

  "$(index) $($(v)[$(index)])",
      ifvarclass => "!not_$(cindex[$(index)])";
}



On Wed, Mar 17, 2010 at 11:43 AM, daniel parkes <liquidsm...@gmail.com> wrote:
> Hi,
>
> We have a very large /etc/hosts(300 hosts) files in our servers that we have
> to mantain for certain acient applications, as of now what I have configured
> in cfengine 3.0.2 is an edit line bundle that reads from a master host file
> and if the host isn't in the file it adds it.
>
> The problem is just and add line, if I modify one of the hosts ip address it
> won't replace it with the new one, it just adds the line at the end.
>
> I have been triying to get it working with replace_with and regex
> expresions, but I really can't find a way to do it easily, any of you doing
> something like this could get me started ??
>
> I'cant paste the code I am using because i'm not at work, but I will get in
> tomorrow.
>
> Thnx for the help
>
> _______________________________________________
> Help-cfengine mailing list
> Help-cfengine@cfengine.org
> https://cfengine.org/mailman/listinfo/help-cfengine
_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to