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

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

ansible / ansible-playbook 2.9.27

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

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

那就太好了:

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

这是我的Playbookaks.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   

我注意到BOTHRANGE

[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
1个回答
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

简而言之,我无法不断地重现错误。

© www.soinside.com 2019 - 2024. All rights reserved.