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

xghobddn  于 2023-10-22  发布在  Linux
关注(0)|答案(2)|浏览(138)

Ansible / Ansible-Playbook 2.9.27
我有下面的剧本,我想只显示那些行(来自数据结构(即,list -->with_items循环),这些循环匹配给定的multi“egrep”类模式(例如:“* 模式1| pattern2| pattern3withRange [0-9a-zA-Z]*”)。

我如何在Ansible中实现这一点,可能只使用一个with_items / loop?

这将是伟大的:

  • 如果我不用N的话不。嵌套循环(每个模式/字符串)--或--
  • 我不必使用shell一行代码(egrep“string|绝对值 *yz| giga”)。

以下是我的Playbook: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"

运行Playbook可以让我:

$ 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]可以成功捕获包含r 00r 01的项字符串,这很棒。
  • 使用...|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
jc3wubiy

jc3wubiy1#

它更容易搜索选定的列表。例如,给定列表

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

1.搜索模式r 0 [0-9]|戈加

data_sel: "{{ data_raw|
                select('search', 'r0[0-9]|goga', ignorecase=true) }}"

选择所有项目,因为:

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

1.改变第二模式'r 0 [0-9]|新'

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
up9lanfz

up9lanfz2#

从红帽分销渠道了解带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包索引(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

简而言之,我无法重现错误(annot.:不断地)。
您收到的错误消息

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 }}"

相关问题