replace - Replace all instances of a particular string in a file using a back-referenced regular expression.

Author:Evan Kaufman

Synopsis

New in version 1.6.

This module will replace all instances of a pattern within a file. It is up to the user to maintain idempotence by ensuring that the same pattern would never match any replacements made.

Options

parameter required default choices comments
backup no no
  • yes
  • no
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
dest yes
    The file to modify.
    others no
      All arguments accepted by the file module also work here.
      regexp yes
        The regular expression to look for in the contents of the file. Uses Python regular expressions; see http://docs.python.org/2/library/re.html. Uses multiline mode, which means ^ and $ match the beginning and end respectively of each line of the file.
        replace no
          The string to replace regexp matches. May contain backreferences that will get expanded with the regexp capture groups if the regexp matches. If not set, matches are removed entirely.
          validate no None
            validation to run before copying into place

            Examples


            - replace: dest=/etc/hosts regexp='(\s+)old\.host\.name(\s+.*)?$' replace='\1new.host.name\2' backup=yes
            
            - replace: dest=/home/jdoe/.ssh/known_hosts regexp='^old\.host\.name[^\n]*\n' owner=jdoe group=jdoe mode=644
            
            - replace: dest=/etc/apache/ports regexp='^(NameVirtualHost|Listen)\s+80\s*$' replace='\1 127.0.0.1:8080' validate='/usr/sbin/apache2ctl -f %s -t'