How do i use regex_findall filter to display partial output of 'sh clock' command?

359 views
Skip to first unread message

Vikram S

unread,
May 10, 2021, 3:55:38 PM5/10/21
to Ansible Project
In below output, i want only the 'May 11 2021' to be displayed on screen.
Switch#sh clock
02:42:19.622 Sng Tue May 11 2021

Below is a sample script and used 'regex' filter as an example. How do i modify the script/ incorporate other any other filter such that my above objective is achieved?

- name: GATHER SHOW VERSION
  ios_command: 
    commands: "show clock"

  register: show_clock

- debug: 
    msg: 
    - "{{ show_clock.stdout[0] | regex_findall('.....)"

Thanks,
Vikram

Andrew Cobb

unread,
May 10, 2021, 10:13:29 PM5/10/21
to Ansible Project
something like   - "{{ show_clock.stdout[0] | regex_findall('[A-Z][a-z]{2}\s[0-9]{2}\s[0-9]{4}')" should work although I am no expert so there may be a better way to do it.   
I found  https://regexone.com/  to be helpful to learn the basics  and https://regex101.com/ for testing.

Vikram S

unread,
May 15, 2021, 2:12:00 PM5/15/21
to Ansible Project
Hi,

I suspect your script may show ewverything in 'sh clock' output and not the partial output i wanted.  Thanks for the links, will check them.

Regards,
Vikram

Dick Visser

unread,
May 16, 2021, 5:19:31 AM5/16/21
to ansible...@googlegroups.com
On Sun, 16 May 2021 at 10:38, Vikram S <vikrams...@gmail.com> wrote:
>
> Hi,
>
> I suspect your script may show ewverything in 'sh clock' output and not the partial output i wanted. Thanks for the links, will check them.

You expect this based on? First check.

If you must parse dates like that, it requires more data. Is the first
line ('Switch#sh clock') also part of the output? Let's assume so.
And is the month abbreviated or not? From just 'May' we can't tell.
Assuming it is, this works, in this century:

"{{ out | regex_replace(
'(?s).*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s+(\\d{1,2})\\s+(20\\d\\d)$',
'\\2 \\3 \\4'
) }}"
Reply all
Reply to author
Forward
0 new messages