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?
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.
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)
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
>
>
>
>
> --
> 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,
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.
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)'
>
>
>
> --
> 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,
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!