REGEX (regex_search) 就像 Linux EGREP - 使用多模式/范围使用一个 with_items 循环显示模式匹配行(带空格)

问题描述 投票:0回答:2

Ansible / ansible-playbook 2.9.27

我有以下剧本,我只想显示那些与给定的multi“egrep”类似模式匹配的行(来自数据结构(即列表-->with_items循环)(例如:“pattern1|”)模式2|模式3withRange[0-9a-zA-Z]”)。

我怎样才能纯粹在 Ansible 中实现这一目标,可能只使用一个 with_items / 循环?

那就太好了:

  • 如果我不必使用N则不行。嵌套循环(每个模式/字符串)--或--
  • 我不必使用 shell 单行代码(egrep "string|ab.*yz|giga")。

这是我的剧本:

aks.yml

---
- hosts: localhost
  become: yes
  remote_user: root
  become_user: root
  pre_tasks:
  - name: show pattern matching lines
    #command: 'echo {{ item }} | egrep -i "r[0-9]|goga"'
    ansible.builtin.debug:
      var: "{{ item }}"
    when: ( item | regex_search('r0[0-9]|goga', ignorecase=True))
    with_items:
      - "ar00n_giga"
      - "Schnooka_goga"
      - "lorito_r01_gigaFifa"

运行剧本给了我:

$ ansible-playbook aks.yml

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [localhost]

TASK [show pattern matching lines] *******************************************************************************************************************************************************************************
ok: [localhost] => (item=ar00n_giga) => {
    "ansible_loop_var": "item", 
    "ar00n_giga": "VARIABLE IS NOT DEFINED!", 
    "item": "ar00n_giga"
}

skipping: [localhost] => (item=Schnooka_goga) 

ok: [localhost] => (item=lorito_r01_gigaFifa) => {
    "ansible_loop_var": "item", 
    "item": "lorito_r01_gigaFifa", 
    "lorito_r01_gigaFifa": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *******************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我注意到范围[0-9]

和多模式,即在
|
中使用
regex_search(...)
有效!

    使用范围
  • r0[0-9]
    成功捕获包含
    r00r01的项目字符串,这很棒。
  • 使用
  • ...|goga
    (它也捕获第二行,它没有
    r0[0-9]
    模式。
但是如果我的值在 with_items: 循环中包含空格,即如果我添加以下内容,则上述代码不起作用:

with_items: - "ar00n_giga" - "new goga shooga" - "Schnooka_goga" - "lorito_r01_gigaFifa"
然后,我收到以下错误消息:

... TASK [show pattern matching lines] ******************************************************************************************************************************************************************************* ok: [localhost] => (item=ar00n_giga) => { "ansible_loop_var": "item", "ar00n_giga": "VARIABLE IS NOT DEFINED!", "item": "ar00n_giga" } fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got 'goga'. String: {{new goga shooga}}"} PLAY RECAP ******************************************************************************************************************************************************************************************************* localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
    
regex loops ansible grep ansible-2.x
2个回答
1
投票
迭代选定的列表更容易。例如,给定列表

data_raw: - ar00n_giga - new goga shooga - Schnooka_goga - lorito_r01_gigaFifa

  1. 搜索模式 'r0[0-9]|goga'
data_sel: "{{ data_raw| select('search', 'r0[0-9]|goga', ignorecase=true) }}"
给出所有时间,因为:

    搜索“r0[0-9]”选择第一项和最后一项,然后
  • 搜索“goga”选择第二项和第三项
data_sel: - ar00n_giga - new goga shooga - Schnooka_goga - lorito_r01_gigaFifa

    更改第二个模式 'r0[0-9]|new'
data_sel: "{{ data_raw| select('search', 'r0[0-9]|new', ignorecase=true) }}"
第三项未被选中

data_sel: - ar00n_giga - new goga shooga - lorito_r01_gigaFifa


用于测试的完整剧本示例

- hosts: localhost vars: data_raw: - ar00n_giga - new goga shooga - Schnooka_goga - lorito_r01_gigaFifa data_sel1: "{{ data_raw| select('search', 'r0[0-9]|goga', ignorecase=true) }}" data_sel2: "{{ data_raw| select('search', 'r0[0-9]|new', ignorecase=true) }}" tasks: - debug: var: data_sel1 - debug: var: data_sel2

    

0
投票
在带有来自 Red Hat 分发渠道的 Ansible 的 RHEL 7.9 上

ansible --version ansible 2.9.27 ... ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, May 30 2023, 03:38:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
一个最小的示例手册

--- - hosts: localhost become: false gather_facts: false vars: ITEMS: - "ar00n_giga" - "new goga shooga" - "Schnooka_goga" - "lorito_r01_gigaFifa" tasks: - name: Show pattern matching lines debug: msg: "{{ item }}" when: ( item | regex_search('r0[0-9]|goga', ignorecase=True)) loop: "{{ ITEMS }}" - name: Show pattern matching lines debug: var: item when: ( item | regex_search('r0[0-9]|goga', ignorecase=True)) loop: "{{ ITEMS }}" - name: Show pattern matching lines debug: var: item when: ( item | regex_search('r0[0-9]|goga', ignorecase=True)) with_items: - "ar00n_giga" - "new goga shooga" - "Schnooka_goga" - "lorito_r01_gigaFifa"
将产生

的输出

TASK [Show pattern matching lines] ************* ok: [localhost] => (item=ar00n_giga) => msg: ar00n_giga ok: [localhost] => (item=new goga shooga) => msg: new goga shooga ok: [localhost] => (item=Schnooka_goga) => msg: Schnooka_goga ok: [localhost] => (item=lorito_r01_gigaFifa) => msg: lorito_r01_gigaFifa TASK [Show pattern matching lines] ************* ok: [localhost] => (item=ar00n_giga) => ansible_loop_var: item item: ar00n_giga ok: [localhost] => (item=new goga shooga) => ansible_loop_var: item item: new goga shooga ok: [localhost] => (item=Schnooka_goga) => ansible_loop_var: item item: Schnooka_goga ok: [localhost] => (item=lorito_r01_gigaFifa) => ansible_loop_var: item item: lorito_r01_gigaFifa TASK [Show pattern matching lines] ************* ok: [localhost] => (item=ar00n_giga) => ansible_loop_var: item item: ar00n_giga ok: [localhost] => (item=new goga shooga) => ansible_loop_var: item item: new goga shooga ok: [localhost] => (item=Schnooka_goga) => ansible_loop_var: item item: Schnooka_goga ok: [localhost] => (item=lorito_r01_gigaFifa) => ansible_loop_var: item item: lorito_r01_gigaFifa
与从 Python Package Index (PyPI) 安装的 RHEL 7.9 和 Ansible 的结果相同

```bash ansible --version ... ansible [core 2.11.12] ... python version = 2.7.5 (default, May 30 2023, 03:38:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] jinja version = 2.11.3 libyaml = True
简而言之,我无法重现错误(注释:不断)。


您收到的错误消息

fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got 'goga'. String: {{new goga shooga}}"}
 是由 

debug

 任务中的错误语法引起的,使用了 

var: "{{ item }}"
而不是

var: item

msg: "{{ item }}"
    
© www.soinside.com 2019 - 2024. All rights reserved.