need assistance in using regex filter to extract a data

Hi,

I went through ansible documentation for regex filter and it’s various example. But please excuse my incomprehension, iam stuck on using regex to extract the integers.

Below is the output on which i want to use regex. I want to filter out only ‘48700416’ and leave out everything else and then use ‘int’ filter on it so that i can then compare it against a particular number. Can you please give a sample regex syntax to achieve this?

122185728 bytes total (48700416 bytes free)

Thanks,
Vikram

Hi,

I went through ansible documentation for regex filter and it's various example. But please excuse my incomprehension,
iam stuck on using regex to extract the integers.

Below is the output on which i want to use regex. I want to filter out only '48700416' and leave out everything else and
then use 'int' filter on it so that i can then compare it against a particular number. Can you please give a sample
regex syntax to achieve this?

*122185728 bytes total (48700416 bytes free)*
*
*
Thanks,
Vikram

Hello Vikram,

if you are talking about diskspace, you can use the ansible_mounts variable, e.g.

- debug
    msg: |
      {% for mt in ansible_mounts %}
      {{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
      {% endfor %}

Regards
          Racke

Hi Racke,

I am not sure if ansible_mounts may help me. Reason is in a stacked cisco switch, ‘ansible_net_filesystems_info’ of ios_facts module only shows free space of master switch. It doesn’t show space of member switches. But I need to write a script that will check for certain amt of free space in EVERY member switch before copying ios image.

So i thought of using regex to extract the number and use int filter to convfert it into a integer acheive my objective. Can you please give me sample regex syntax?

command in cli: sh flash2:

data i want to use regex: 122185728 bytes total (9308672 bytes free)

Thanks,
Vikram

Hi Racke,

I am not sure if ansible_mounts may help me. Reason is in a stacked cisco switch, 'ansible_net_filesystems_info' of
ios_facts module only shows free space of master switch. It doesn't show space of member switches. But I need to write a
script that will check for certain amt of free space in EVERY member switch before copying ios image.

So i thought of using regex to extract the number and use int filter to

convfert it into a integer acheive my objective.

Can you please give me sample regex syntax?

*command in cli: sh flash2:*

*data i want to use regex: 122185728 bytes total (9308672 bytes free)*
*
*
Thanks,
Vikram

Hello Vikram,

this should help you to get the second number from your string:

    - debug:
        msg: "{{ diskspace_string | regex_findall('\\d+') | last }}"
      vars:
        diskspace_string: '122185728 bytes total (9308672 bytes free)'

Regards
        Racke

    > Hi,
    >
    > I went through ansible documentation for regex filter and it's various example. But please excuse my incomprehension,
    > iam stuck on using regex to extract the integers.
    >
    > Below is the output on which i want to use regex. I want to filter out only '48700416' and leave out everything
    else and
    > then use 'int' filter on it so that i can then compare it against

a particular number. Can you please give a sample

    > regex syntax to achieve this?
    >
    > *122185728 bytes total (48700416 bytes free)*
    > *
    > *
    > Thanks,
    > Vikram

    Hello Vikram,

    if you are talking about diskspace, you can use the ansible_mounts variable, e.g.

    - debug
    msg: |
    {% for mt in ansible_mounts %}
    {{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
    {% endfor %}

    Regards
    Racke

    >
    >
    >
    >
    > --
    > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
    > To unsubscribe from this group and stop receiving emails from it,

send an email to

Hi Racke,

Thanks for the syntax, it is giving an output close to what i want but not exactly the numbers i want to extract.

OUTPUT IN DEVICE:

SWITCH#sh flash: | i bytes
122185728 bytes total (48700416 bytes free)

MY SCRIPT:

tasks:

  • name: GATHER MASTER FLASH FREE SPACE
    ios_command:
    commands: “show flash: | i bytes”

register: show_space

  • debug:
    msg: “{{ show_space | regex_findall(‘\d+’) | last }}”

ANSIBLE OUTPUT:

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [TEST2960] => {
“msg”: “3”

If i give the script without the last keyword, below is output:

  • debug:
    msg: “{{ show_space | regex_findall(‘\d+’) }}”

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [TEST2960] => {
“msg”: [
“122185728”,
“48700416”,
“122185728”,
“48700416”,
“3”
]

So how can the syntax be modified such that i get only ‘48700416’ in output? I then plan to ‘int’ filter to convert it into a integer so that i can then compare it againt a value.

Thanks,
Vikram

Hi Racke,

Thanks for the syntax, it is giving an output close to what i want but not exactly the numbers i want to extract.

Hello Vikram,

that is because you didn't give the exact input, aka the contents of the show_space variable.

Regards
        Racke

*OUTPUT IN DEVICE:*
SWITCH#sh flash: | i bytes
122185728 bytes total (48700416 bytes free)

*MY SCRIPT:*
tasks:
- name: GATHER MASTER FLASH FREE SPACE
ios_command:
commands: "show flash: | i bytes"

  register: show\_space

\- debug:
    msg: "\{\{ show\_space | regex\_findall\('\\\\d\+'\) 

last }}"

*ANSIBLE OUTPUT:*
TASK [debug]
******************************************************************************************************************************************************************************************************
ok: [TEST2960] => {
"msg": "3"

If i give the script without the last keyword, below is output:
- debug:
msg: "{{ show_space | regex_findall('\\d+') }}"

TASK [debug]
******************************************************************************************************************************************************************************************************
ok: [TEST2960] => {
"msg": [
"122185728",
"48700416",
"122185728",
"48700416",
"3"
]

So how can the syntax be modified such that i get only '48700416' in output? I then plan to 'int' filter to convert it
into a integer so that i can then compare it againt a value.

Thanks,
Vikram

    > Hi Racke,
    >
    > I am not sure if ansible_mounts may help me. Reason is in a stacked cisco switch, 'ansible_net_filesystems_info' of
    > ios_facts module only shows free space of master switch. It doesn't show space of member switches. But I need to
    write a
    > script that will check for certain amt of free space in EVERY member switch before copying ios image.
    >
    > So i thought of using regex to extract the number and use int filter to
    convfert it into a integer acheive my objective.
    > Can you please give me sample regex syntax?
    >
    > *command in cli: sh flash2:*
    >
    > *data i want to use regex: 122185728 bytes total (9308672 bytes free)*
    > *
    > *
    > Thanks,
    > Vikram

    Hello Vikram,

    this should help you to get the second number from your string:

    - debug:
    msg: "{{ diskspace_string | regex_findall('\\d+') | last }}"
    vars:
    diskspace_string: '122185728 bytes total (9308672 bytes free)'

    Regards
    Racke

    >
    >
    > > Hi,
    > >
    > > I went through ansible documentation for regex filter and it's various example. But please excuse my
    incomprehension,
    > > iam stuck on using regex to extract the integers.
    > >
    > > Below is the output on which i want to use regex. I want to filter out only '48700416' and leave out everything
    > else and
    > > then use 'int' filter on it so that i can then compare it against
    a particular number. Can you please give a sample
    > > regex syntax to achieve this?
    > >
    > > *122185728 bytes total (48700416 bytes free)*
    > > *
    > > *
    > > Thanks,
    > > Vikram
    >
    > Hello Vikram,
    >
    > if you are talking about diskspace, you can use the ansible_mounts variable, e.g.
    >
    > - debug
    > msg: |
    > {% for mt in ansible_mounts %}
    > {{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
    > {% endfor %}
    >
    > Regards
    > Racke
    >
    > >
    > >
    > >
    > >
    > > --
    > > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
    > > To unsubscribe from this group and stop receiving emails from it,
    send an email to
    > > ansible-proje...@googlegroups.com <mailto:ansible-proje…@googlegroups.com>.
    > > To view this discussion on the web visit
    > > https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com&gt;
    > <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com&gt;&gt;
    > >
    >
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com?utm_medium=email&utm_source=footer
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com?utm_medium=email&utm_source=footer&gt;

    >
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com?utm_medium=email&utm_source=footer
    <https://groups.google.com/d/msgid/ansible-project/2999c167-454e-4d35-a198-a71e515053fcn%40googlegroups.com?utm_medium=email&utm_source=footer&gt;&gt;&gt;\.

    >
    >
    >
    > --
    > Ecommerce and Linux consulting + Perl and web application programming.
    > Debian and Sympa administration. Provisioning with Ansible.
    >
    >
    >
    > --
    > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
    > To unsubscribe from this group and stop receiving emails from it,

send an email to

Hi Racke,

Giving below syntax worked as you said!!! My deepest gratitude to you for solving this nerve wracking issue!
“{{ show_space.stdout[0] | regex_findall(‘\d+’) | last }}”

This made me wonder about some thing else - as i understand, ‘stdout’ means that it quotes the output of variable and ‘[0]’ means referencing the first char or command. So how does adding .stdout[0] make all the difference?

Also i saw several in ansible documentation with (‘\d+’) but it was never really explained what this meant (though i understand now). Should i read python to understand these parameters?

Once again, many thanks Racke. You have been consistently helping with so many of my queries!

Vikram