如何搜索和匹配模式以获得可见的值



我的变量info具有以下值。(实际案例数据量大)。

我正在尝试搜索特定的单词XYZ_data_001并获得大小信息,这是在模式physical disk,

之后
XYZ_data_001         file system device, special, dsync off, directio on, physical disk, 16384.00 MB, Free: 0.00 MB      2         0      6       0  8388607
XYZ_data_002         file system device, special, dsync off, directio on, physical disk, 16384.00 MB, Free: 0.00 MB      2         0     13       0  8388607

这里是尝试的

- name: Print size
ansible.builtin.debug:
msg: "{{ info | regex_search('XYZ_data_001(.+)') | split('physical disk,') | last }}"

这将得到如下输出

ok: [testhost] => {
"msg": " 16384.00 MB, Free: 0.00 MB      2         0      6       0  8388607 "
}

Thanks in advance

可以使用

{{ info | regex_search('XYZ_data_001\b.*physical disk,\s*(\d[\d.]*)', '\1') }}

参见regex演示。

细节:

  • XYZ_data_001- aXYZ_data_001string
  • b- a字边界
  • .*-任何文本(除换行符外的任何零个或多个字符)
  • physical disk,-一个文字字符串
  • s*-零或多个空白
  • (d[d.]*)-组1 (1):一个数字,然后是零或多个数字或点。

集合Community中有两个过滤器。通用,将帮助您从信息创建字典。

  1. 分隔行,分隔修剪项,并使用过滤器community.general。dict创建字典列表
info_dict1: "{{ info.splitlines()|
map('split', ',')|
map('map', 'trim')|
map('zip', ['dev', 'spec', 'dsync', 'dir', 'disk', 'size', 'free'])|
map('map', 'reverse')|
map('community.general.dict') }}"

info_dict1:
- dev: XYZ_data_001         file system device
dir: directio on
disk: physical disk
dsync: dsync off
free: 'Free: 0.00 MB      2         0      6       0  8388607'
size: 16384.00 MB
spec: special
- dev: XYZ_data_002         file system device
dir: directio on
disk: physical disk
dsync: dsync off
free: 'Free: 0.00 MB      2         0     13       0  8388607'
size: 16384.00 MB
spec: special
  1. 拆分属性dev并使用过滤器community.general。dict_kv创建带有属性device
  2. 的字典列表
info_dev: "{{ info_dict1|
map(attribute='dev')|
map('split')|
map('first')|
map('community.general.dict_kv', 'device') }}"

info_dev:
- device: XYZ_data_001
- device: XYZ_data_002

组合字典

info_dict2: "{{ info_dict1|zip(info_dev)|map('combine') }}"

info_dict2:
- dev: XYZ_data_001         file system device
device: XYZ_data_001
dir: directio on
disk: physical disk
dsync: dsync off
free: 'Free: 0.00 MB      2         0      6       0  8388607'
size: 16384.00 MB
spec: special
- dev: XYZ_data_002         file system device
device: XYZ_data_002
dir: directio on
disk: physical disk
dsync: dsync off
free: 'Free: 0.00 MB      2         0     13       0  8388607'
size: 16384.00 MB
spec: special

这样你可以在需要的时候添加其他属性。


Q:"搜索特定词XYZ_data_001,得到size.">

A:创建一个字典device_size

device_size: "{{ info_dict2|items2dict(key_name='device', value_name='size') }}"

device_size:
XYZ_data_001: 16384.00 MB
XYZ_data_002: 16384.00 MB

查字典

- debug:
msg: "Size of XYZ_data_001 is {{ device_size.XYZ_data_001 }}"

msg: Size of XYZ_data_001 is 16384.00 MB

用于测试的完整剧本示例

- hosts: localhost
vars:
info: |
XYZ_data_001         file system device, special, dsync off, directio on, physical disk, 16384.00 MB, Free: 0.00 MB      2         0      6       0  8388607
XYZ_data_002         file system device, special, dsync off, directio on, physical disk, 16384.00 MB, Free: 0.00 MB      2         0     13       0  8388607
info_dict1: "{{ info.splitlines()|
map('split', ',')|
map('map', 'trim')|
map('zip', ['dev', 'spec', 'dsync', 'dir', 'disk', 'size', 'free'])|
map('map', 'reverse')|
map('community.general.dict') }}"
info_dev: "{{ info_dict1|
map(attribute='dev')|
map('split')|
map('first')|
map('community.general.dict_kv', 'device') }}"
info_dict2: "{{ info_dict1|zip(info_dev)|map('combine') }}"
device_size: "{{ info_dict2|items2dict(key_name='device', value_name='size') }}"
tasks:
- debug:
var: info_dict1
- debug:
var: info_dev
- debug:
var: info_dict2
- debug:
var: device_size
- debug:
msg: "Size of XYZ_data_001 is {{ device_size.XYZ_data_001 }}"

最新更新