Here's what I use which has been pulled from various answers in the forum.
Bascially there's a function for deleting a line, appending a line if not
already there and near the bottom one for editing a line.  So the way this
works is that this file gets run as part of the base_class to set up the
definition statements and then whenever you need to make a mod such as
replacing a line just include the commented bit at the end (uncommenting it
first, of course)

#
# edit_file.pp

case $operatingsystem {
  RedHat,suse: {  $sedcommand="/bin/sed" }
  solaris: { $sedcommand="/opt/csw/bin/gsed"}
}


define delete_line($file, $pattern) {
   exec { "$sedcommand -r -i '/$pattern/d' $file":
      onlyif => "/bin/grep  '$pattern' '$file'",
   }

}
# call the above somewhere else using the following:-
#    import "edit_file.pp"
#      delete_line { title:
#      file => "/path/to/file",
#      pattern => "InsertRegExpHere",
#   }

define append_if_no_such_line($file, $line, $refreshonly = 'false') {
   exec { "/bin/echo '$line' >> '$file'":
      #unless      => "/bin/grep -Fxqe '$line' '$file'",
      unless      => "/bin/grep '$line' '$file'",
      path        => "/bin",
      refreshonly => $refreshonly,
   }
}

# call the above somewhere else using the following:-
#  append_if_no_such_line{ motd:
#          file => "/etc/motd",
#          line => "Configured with Puppet!"}


define replace_line($file, $old_pattern, $new_pattern) {
   exec { "$sedcommand -r  -i 's/$old_pattern/$new_pattern/' $file":
      onlyif => "/bin/grep  '$old_pattern' '$file'",
   }

}
# call the above somewhere else using the following:-
#    import "edit_file.pp"
#      replace_line { title:
#      file => "/path/to/file",
#      old_pattern => "InsertRegExpHere",
#      new_pattern => "InsertRegExpHere",
#      }


Rgds
Paul

2009/1/15 Robin Lee Powell <rlpow...@digitalkingdom.org>

>
> On Wed, Jan 14, 2009 at 09:38:42PM -0800, chakkerz wrote:
> >
> > which deals with the subject of replacing lines in a file,
>
> You might want to look at Augeas.  It's pretty neat, and the latest
> puppet version has it built in.
>
> -Robin
>
>
> --
> They say:  "The first AIs will be built by the military as weapons."
> And I'm thinking:  "Does it even occur to you to try for something
> other than the default outcome?" -- http://shorl.com/tydruhedufogre
> http://www.digitalkingdom.org/~rlpowell/<http://www.digitalkingdom.org/%7Erlpowell/>***
> http://www.lojban.org/
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to