Request assistance to write script using Regex

25 views
Skip to first unread message

Vikram S

unread,
Jul 13, 2021, 1:27:04 PM7/13/21
to Ansible Project
Hi Team,

From the below output, i need to extract only the IP 10.1.1.10 (it is ok if the entire line starting from asterix is extracted). 

I wrote below script and it seemed to work when i tested in regex101.com but failing when i used it in playbook. What am i doing wrong? How can it be improved?

SWITCH COMMAND:

    CORE-SWITCH#sh ip route 0.0.0.0
    Routing entry for 0.0.0.0/0, supernet
    Known via "static", distance 1, metric 0, candidate default path
    Redistributing via eigrp 500
    Advertised by eigrp 500
    Routing Descriptor Blocks:
    * 10.1.1.10
      Route metric is 0, traffic share count is 1

SCRIPT:

    tasks:
    - name: GATHER DEFAULT ROUTE INFO
      ios_command:
        commands: "show ip route 0.0.0.0"

      register: default_route

    - debug:
        msg: "{{ default_route.stdout[0] | regex_findall('\* (.*)') }}"


OUTPUT:

    ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
    JSON: Expecting value: line 1 column 1 (char 0)

    Syntax Error while loading YAML.
    found unknown escape character

    The error appears to be in '/root/sample12.yaml': line 19, column 59, but may be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

    - debug:
        msg: "{{ default_route.stdout[0] | regex_findall('\* (.*)') }}"
                                                          ^ here
    We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:

    with_items:
      - {{ foo }}

    Should be written as:

    with_items:
      - "{{ foo }}"

Thanks,
Vikram

Dick Visser

unread,
Jul 13, 2021, 1:36:33 PM7/13/21
to ansible...@googlegroups.com
Well, it's says it right there:

On Tue, 13 Jul 2021 at 19:27, Vikram S <vikrams...@gmail.com> wrote:

> Syntax Error while loading YAML.
> found unknown escape character

You escape something but incorrect.
Try with 2 backslashes:

msg: "{{ default_route.stdout[0] | regex_findall('\\* (.*)') }}"



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT
Reply all
Reply to author
Forward
0 new messages