首页 > 解决方案 > 在 sdout 中匹配通配符时如何使 Ansible 失败?

问题描述

我正在使用 shell 命令运行 SQL,输出为:

SQL*Plus: Release 10.1.0.4.0 - Production on Thu Sep 6 15:44:35 2007

Copyright (c) 1982, 2005, Oracle.  All rights reserved.
<b>
ERROR:
ORA-12560: TNS:protocol adapter error

</b>

我正在使用以下方法检查 std 中的错误:

- hosts: localhost
  tasks:
    - name: error-codes
      shell: "cat {{ item }}"
      with_items:
        - file1.txt
        - file2.txt
      register: result
      failed_when: "'ERROR' in result.stdout"

但是,我需要匹配多行,例如

ERROR:
ORA-

标准输出是单行,所以我尝试使用通配符进行匹配,但它似乎不匹配

failed_when: "'ERROR.*ORA' in result.stdout"

使用通配符时如何使其失败?

标签: ansible

解决方案


result.stdout不是单行内容。它是一个多行内容,它显示在一行中。

这里(?s)regex_search整个记录视为一行。

result.stdout| regex_search("(?s)ERROR.*ORA")

推荐阅读