运行Ansible Playbook遇re.error错误,请求协助排查与修复
Ansible Playbook正则表达式错误排查与修复
错误信息
2023-03-31 11:47:39,902 p=57332 u=NI40153964 n=ansible | 任务执行期间发生异常。
如需查看完整回溯信息,请使用-vvv参数。错误信息:re.error: global flags not at the start of the expression at position 1
2023-03-31 11:47:39,903 p=57332
环境版本
- Python 3.11.2
- Ansible [core 2.14.3]
问题原因
错误出在set_fact任务的regex_search过滤器里。你写的正则表达式中,单行模式修饰符(?s)被包裹在额外的外层括号中,没有放在表达式的最起始位置。而Python的re模块要求全局修饰符必须位于表达式开头,因此触发了global flags not at the start错误。
修复方案
把(?s)移到正则表达式最开头,同时移除多余的外层括号,修改后的set_fact任务代码如下:
- 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
说明
(?s)作为单行模式修饰符,放在表达式开头后,re模块能正确识别该规则,此时.会匹配包括换行符在内的所有字符,确保可以正确提取跨多行的日志片段。
内容的提问来源于stack exchange,提问作者Varun K M
相关产品推荐
相关产品推荐

