Can we use regex in single-line mode with ansible module like lineinfile [dot matches all, dotall, re.S, (?s)]

1,172 views
Skip to first unread message

sigml16

unread,
Apr 21, 2015, 3:35:38 PM4/21/15
to ansible...@googlegroups.com, Mike Larouche
Ansible docs point us on python re web site for regex. In this page, modifiers like ignore case, single line mode are documented, but it seems not supported when used with ansible module where regex is used (like lineinfile)

Someone find out how works with (?s) aka single-line mode?

usage: useful to insert line in the rigth place in a file like:

Input:
foo
o
o
foo
bar
a
r

regex to insert "foobar" line after "foo" followed by "bar"
(?s)(.*?foo.*?\n)(bar.*)

substitution:
\1foobar\n\2

Result:
foo
o
o
foo
foobar
bar
a
r


"...
(?iLmsux)

(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function.

..."

Ansible should support this since is build-in in python and look very easy to support.


(Note: I am pretty new with ansible, python and i am not english. So, no bashing will be apreciated.. i am looking for a solution not useless comments)

sigml16

unread,
Apr 22, 2015, 10:07:57 AM4/22/15
to ansible...@googlegroups.com, mik...@gmail.com
UPDATE: I tested the re.IGNORECASE inline modifier (?i) and this one work well. I dont know why the single line modifier (?s) don't works then.

Brian Coca

unread,
Apr 22, 2015, 10:28:07 AM4/22/15
to ansible...@googlegroups.com, mik...@gmail.com
lineinfile applies the regex to single lines already, it does not
apply it against the whole document, so adding single line parsing is
useless.



--
Brian Coca

sigml16

unread,
Apr 22, 2015, 12:50:57 PM4/22/15
to ansible...@googlegroups.com, mik...@gmail.com
[SOLVED]
ALL GOOD, i discover than "replace" module works well with DOTALL flag.


 
Reply all
Reply to author
Forward
0 new messages