searching multi lines using regex

38 views
Skip to first unread message

rajtheco...@gmail.com

unread,
Sep 21, 2020, 11:28:48 AM9/21/20
to Ansible Development
How to search multiple lines using regex in Ansible?

I tried below pattern and it is not working

regex: ^line con.*(?:\n^\s.*)

line console 0
 password

Bob Harold

unread,
Sep 21, 2020, 11:39:27 AM9/21/20
to rajtheco...@gmail.com, Ansible Development
I don't think you can use caret " ^ " in the middle of a regex.  It is probably trying to match a literal ^ symbol and not finding it.  Try removing the caret.

-- 
Bob Harold
 

rajtheco...@gmail.com

unread,
Sep 21, 2020, 12:01:56 PM9/21/20
to Ansible Development
Hi Bob,

here my code I removed '^' still not working

- set_fact:
        console: "{{ current_show_run[0] | map('regex_search', qry, multiline=True | select('string') | list }}"
  vars:
       qry: '^line con.*\n\spassword'

Abhijit Menon-Sen

unread,
Sep 28, 2020, 1:22:56 AM9/28/20
to rajtheco...@gmail.com, Ansible Development
On Mon, 21 Sep 2020 at 21:31, rajtheco...@gmail.com
<rajtheco...@gmail.com> wrote:
>
> Hi Bob,
>
> here my code I removed '^' still not working
>
> - set_fact:
> console: "{{ current_show_run[0] | map('regex_search', qry, multiline=True | select('string') | list }}"
> vars:
> qry: '^line con.*\n\spassword'

This list is for discussions about Ansible development. The Ansible
Project mailing list would be the right forum for your question. See
https://docs.ansible.com/ansible/latest/community/communication.html
for details.

The approach you are using works as expected in this test (caret included):

```
$ ansible localhost -m debug -a msg="{{ lines|map('regex_search',
'^line con.*\n^\s(.*)', multiline=True)|select('string')|list }}" -e
'{"lines":["foo\nbar","line console 0\n password","mine console
1\npassword","line console 2\nnospaceword","line console 3\n
otherpassword"]}'
localhost | SUCCESS => {
"msg": [
"line console 0\n password",
"line console 3\n otherpassword"
]
}
```

Either the input data you're feeding it isn't what you expect, or your
pattern doesn't match the correct input.

-- Abhijit
Reply all
Reply to author
Forward
0 new messages