Skip to content

Added replace module, for multiple regexp match + replace against entire file's contents#4889

Merged
jimi-c merged 1 commit intoansible:develfrom
EvanK:devel
Mar 25, 2014
Merged

Added replace module, for multiple regexp match + replace against entire file's contents#4889
jimi-c merged 1 commit intoansible:develfrom
EvanK:devel

Conversation

@EvanK
Copy link
Copy Markdown
Contributor

@EvanK EvanK commented Nov 13, 2013

This implements a multiline regexp replacement against the entire contents of a file. Unlike the lineinfile module, this would allow 1) matching more than one line of said file and 2) replacing multiple matches. Currently, someone would have to implement this with a shell task using sed or perl.

The specific use case I built this for was to replace the entire mpm_prefork section from ubuntu's default apache config, while leaving the rest of the file in tact; this removes the section in question and replaces it with an include (to a file that I then created from a template):

- replace: >
  dest=/etc/apache2/apache2.conf
  regexp='^(?:#[^\n]+\n)*<IfModule mpm_prefork_module>[^<]+?<\/IfModule>'
  replace='Include prefork.conf'
  backup=yes
  validate='/usr/sbin/apache2ctl -f %s -t'

I was also considering adding an all boolean or count numeric parameter to control how many replacements to perform against the file -- it's currently just replacing all matches.

EvanK pushed a commit to evolution/genesis-wordpress that referenced this pull request Nov 13, 2013
…manual overrides as global vars

* if no manual override, calculating php memory_limit via `shell` task
* replacing apache default prefork config with include to separate file
* generating prefork.conf from template (incl calculated directive values)
* using custom `replace` module, see ansible/ansible#4889

Fixes #56
EvanK pushed a commit to evolution/genesis-wordpress that referenced this pull request Nov 13, 2013
Both modules should be (hopefully) in ansible's 1.4 release, until then providing them here as custom lib modules:

* hostname module, see ansible/ansible#3940
* replace module, see ansible/ansible#4889
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to mention here that it defaults to using a multiline search

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

EvanK pushed a commit to evolution/genesis-wordpress that referenced this pull request Nov 13, 2013
Both modules should be (hopefully) in ansible's 1.4 release, until then providing them here as custom lib modules:

* hostname module, see ansible/ansible#3940
* replace module, see ansible/ansible#4889
@mpdehaan
Copy link
Copy Markdown
Contributor

Can you please resubmit this as a single commit so it is easier to review?

Thanks!

Heavily based on existing lineinfile module, but where it literally tests a regexp against
*each individual line* of a file, this replace module is more analogous to common uses of
a `sed` or `perl` match + replacement of all instances of a pattern anywhere in the file.

Was debating adding `all` boolean or `count` numeric options to control how many
replacements to make in the destfile (vs currently replacing all instances)

Noted use of MULTILINE mode in docs, per suggestion from @jarv
@EvanK
Copy link
Copy Markdown
Contributor Author

EvanK commented Nov 16, 2013

Okay, squashed down to e88ab43

@jirutka
Copy link
Copy Markdown
Contributor

jirutka commented Jan 18, 2014

+1

1 similar comment
@gregbuehler
Copy link
Copy Markdown

+1

@mpdehaan mpdehaan added the P2 label Mar 19, 2014
@jimi-c jimi-c merged commit e88ab43 into ansible:devel Mar 25, 2014
@jimi-c
Copy link
Copy Markdown
Member

jimi-c commented Mar 25, 2014

Merged, thanks!

jimi-c pushed a commit that referenced this pull request Dec 6, 2016
Run the same tests as used on Travis.
@ansibot ansibot added feature This issue/PR relates to a feature request. and removed feature_pull_request labels Mar 4, 2018
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature This issue/PR relates to a feature request. P2 Priority 2 - Issue Blocks Release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants