[colug-432] NIH v Ansible Custom Modules; Comparing Ansible lineinfile and replace Modules

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Thu Nov 19 13:44:18 EST 2015

On Thu, 19 Nov 2015 09:18:16 -0800, Jon Miller <jonebird at gmail.com> wrote:

> Looks like that [replace] module is much like the 'lineinfile' 
> module in that it is operating on a single line. 

Almost. There are nuances.

The replace and lineinfile Ansible modules are certainly similar.
The regular behavior is for lineinfile to replace the _last_ match,
whereas the replace module replaces all matches.
The following did as its name suggests.

    - name: Require all spaces in all lines with periods.
      replace: dest=/etc/httpd/conf/httpd.conf regexp=' ' replace='.' backup=yes

The important difference I noted for the regexp parameter for my use
is that it uses the multiline mode in the replace module, 
(re.MULTILINE), but not in the lineinfile modules.

> Reason I think the custom module is the correct route to go is because you
> need to have a partial state machine in your parser to ensure you're editing
> 'Require' lines that only exist within the '<Directory />' sections. 

Using the patch module accomplished that 
and did so in a much simpler way than with regexes, 
if it is even possible with regexes.

> My thought would be to write a custom module to be used by Ansible. 

I try to avoid NIH[1]. This is a familiar theme[2].

> You can have a simple 'in_directory' boolean and then only 
> edit 'Require' lines when that is true.

That is the kind of approach I would have used in awk.
I think it would be trickier in sed.

[1] https://en.wikipedia.org/wiki/Not_invented_here
[2] [colug-432] Nice Plain Text Columns and Avoiding NIH

More information about the colug-432 mailing list