【问题标题】:How to search and match pattern to get a value in ansible如何搜索和匹配模式以获取ansible中的值
【发布时间】:2022-12-31 05:05:27
【问题描述】:

我的变量 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 "
}

提前致谢

【问题讨论】:

    标签: regex split ansible


    【解决方案1】:

    您可以使用

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

    请参阅regex demo

    细节:

    • XYZ_data_001 - XYZ_data_001 字符串
    • - 单词边界
    • .* - 任何文本(除换行符以外的任何零个或多个字符,尽可能多)
    • physical disk, - 文字字符串
    • s* - 零个或多个空格
    • (d[d.]*) - 第 1 组 ():一个数字,然后是零个或多个数字或点。

    【讨论】:

      【解决方案2】:

      集合 Community.General 中有两个过滤器将帮助您从中创建 dictionaries信息.

      1. 拆分行,splittrim项,并使用过滤器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. 拆分属性开发并使用过滤器community.general.dict_kv创建具有属性的字典列表设备
          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
        

        这样您就可以根据需要添加其他属性。


        问:“搜索特定单词 XYZ_data_001 并获取大小。”

        A:创建字典设备大小

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 2011-08-17
        • 2015-06-04
        • 2015-03-12
        • 2012-04-17
        相关资源
        最近更新 更多