正则表达式无法检查具有相同前缀的多行

时间:2019-06-26 13:05:53

标签: regex ansible

我正在使用ansible检查文件中是否存在几行。这些行有一个通用前缀,如果文件中所有行都存在,我将打印“是”,否则打印“否”

---
- hosts: localhost
  connection: local

  vars:
    output: "no"

  tasks:
    - name: Reading the contents of the file
      command: "more datafile.txt"
      register: filecontents

    - name: Checking if all the lines are present in the file or not
      set_fact:
        output: "yes"
      when:
        - filecontents.stdout | regex_search('abc def tyu', multiline = true)
        - filecontents.stdout | regex_search('abc def jyu', multiline = true)
        - filecontents.stdout | regex_search('abc def ieye', multiline = true)
        - filecontents.stdout | regex_search('abc def dkhfy', multiline = true)
        - filecontents.stdout | regex_search('abc def dkerj', multiline = true)
        - filecontents.stdout | regex_search('abc def hiu', multiline = true)
        - filecontents.stdout | regex_search('abc def ert', multiline = true)
        - filecontents.stdout | regex_search('abc def opu', multiline = true)

    - debug:
        var: output

使用上述代码,我能够获得正确的答案。但是我要避免编写所有这些regex_search,因为它们的前缀是相同的。

我想用一个regex_search语句替换所有这些regex_search语句。我被困在这里。

我不知道这是否可能。如果可能的话,该怎么做。

0 个答案:

没有答案