The error was: re.error: global flags not at the start of the expression at position 1 while running the ansible playbook
Asked Answered
H

1

6

Can someone please assist how to fix this issue, why i am getting this error while running the Ansible playbook

2023-03-31 11:47:39,902 p=57332 u=NI40153964 n=ansible | An exception occurred during task execution. To see the full traceback, use -vvv. The error was: re.error: global flags not at the start of the expression at position 1 2023-03-31 11:47:39,903 p=57332

Python & Ansible versions:

  • Python 3.11.2
  • Ansible [core 2.14.3]

The YML file which I am using as

    - name: rename log file to user specific
      delegate_to: localhost
      command: mv ~/.ansible/wmDeployment.log ~/.ansible/wmDeployment_{{_user}}.log
      register: logfile_rename_output
      run_once: true
      ignore_errors: yes

    - name: set the fact for email
      set_fact:
        package_list: "{{hostvars['localhost']['package_list']}}"
        log_output: "{{log_data| regex_search('((?s)(?<=This task is to install package in target servers).*?(?=This task is to enable kafka consumers and send mail of playbook log))')}}"
        emailkey: code
        cacheable: true
Hagerman answered 31/3, 2023 at 6:27 Comment(1)
For your next question, please consider adding a more complete output where users can see the name of the task that is failing. Thanks.Reactant
A
8

The error message is saying you need to put the (?s) at the very beginning of the regex. Alternatively, try something like

    log_output: "{{log_data| regex_search('(?s:(?<=This task is to install package in target servers)(.*?)(?=This task is to enable kafka consumers and send mail of playbook log))"

In some more detail, (?s) says "I want the s flag for everything here" or, in other words, a global flag. Python requires this to go at the very beginning of the regex, presumably to avoid weird and hard-to-debug problems which could happen if you had (?s) embedded somewhere in the middle of a long and complex regex. In fact, you could even argue that it's more likely a programming mistake than a considered, conscious decision.

By contrast, (?s:...) says "I want to apply the s flag to the following subexpression," up to just before the closing parenthesis; this is obviously local to just the parenthesized subexpression, and could not be expressed by any other means.

In this concrete example, ((?s)foo) is invalid, but equivalent to either (?s)(foo) or (?s:(foo)) (or, in practice, ((?s:foo)); or, if you don't actually care about the capturing group, simply (?s:foo)).

Anaclinal answered 31/3, 2023 at 6:35 Comment(1)
To add a little detail, this seems to be a new exception as of python 3.11; in 3.9 and 3.10 it raised a DeprecationWarningSaccharin

© 2022 - 2024 — McMap. All rights reserved.