Ansible 从多行字符串中提取单词

Ansible 从多行字符串中提取单词,ansible,Ansible,我有一个多行字符串,我必须从中提取一个单词 示例字符串: `Locale LANG set to the following: "en" Scheduled for (Exp) 02/12/20 (#4662) on CSC2CXN00002555. Batchman LIVES. Limit: 80, Fence: 0, Audit Level: 1` 我必须只提取单词csc2cxn0000255。我尝试使用regex_搜索ansible模块 {{ input_string | rege

我有一个多行字符串,我必须从中提取一个单词

示例字符串:

`Locale LANG set to the following: "en"
Scheduled for (Exp) 02/12/20 (#4662) on CSC2CXN00002555.  Batchman LIVES.  Limit: 80, Fence: 0, Audit Level: 1`
我必须只提取单词csc2cxn0000255。我尝试使用regex_搜索ansible模块

{{ input_string | regex_search('on(.*).  Batchman', multiline=True) }}
但我将整个模式作为输出:

{
"msg": "on CSC2CXN00002555.  Batchman"
}

有没有办法做到这一点?

尝试使用这个正则表达式-
CS(.*)5

{{ input_string | regex_search('CS(.*)5', multiline=True) }}

如果只想返回正则表达式的一小部分,则应使用
regex\u replace

可以通过在换行符上拆分来处理多行

---
- hosts: localhost
  gather_facts: no

  vars:
    input_string: |
        Locale LANG set to the following: "en"
        Scheduled for (Exp) 02/12/20 (#4662) on CSC2CXN00002555.  Batchman LIVES.  Limit: 80, Fence: 0, Audit Level: 1

  tasks:

    - name: "it will return 'CSC2CXN00002555'"
      debug:
        msg: "{{ input_string.split('\n') | regex_replace('^.+ on (\\w+)\\.  Batchman .+', '\\1') }}"
为了更好地理解正则表达式,请使用

此处解释正则表达式: