Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to search and match pattern to get a value in ansible

My variable info has below value. (Actual case has huge data).

I am trying to search for specific word XYZ_data_001 and get the size information, which is after the pattern 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

here is what is tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

this will give me below output

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

Thanks in advance

>Solution :

You can use

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

See the regex demo.

Details:

  • XYZ_data_001 – a XYZ_data_001 string
  • \b – a word boundary
  • .* – any text (any zero or more chars other than line break chars as many as possible)
  • physical disk, – a literal string
  • \s* – zero or more whitespaces
  • (\d[\d.]*) – Group 1 (\1): a digit and then zero or more digits or dots.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading