How can I check numeric string
I am using the assert module to verify my variables defined through the command line. I need to check if my variable contains only numbers(GID). How can I do that? I have tried the following options, but they didn't work in my case
my_var is number
my_var|match('[0-9]')
I feel like there must be a better way to do this, but this should solve your immediate problem. This is a test playbook that demonstrates the logic; the specific solution to your question only involves the my_var
definition at the top and the "Practical example" section at the end...the rest is just test cases.
Anyway, the overall idea here is that you take your original my_var
variable, pass it through the integer conversion (as suggested by Michael Hampton above...sorry, this is my first answer and I'm not sure how to hyperlink but I think you can find it :)) and then add a when clause to subsequent tasks that checks to see if the result of the integer conversion matches the original value. In other words, if the integer conversion didn't change anything, then the original value must have been an integer already. You can also use Ansible "blocks" to apply that when clause to multiple tasks at once.
---
## Run this with something like "ansible-playbook -l localhost test.yml"
- name: Testing playbook
hosts: all
vars:
- input_value_numeric : '12345'
- input_value_alpha : 'no_numbers_here'
- input_value_mixed : '123luggage45'
- my_var: '123'
tasks:
- name: Create converted fact from input (numeric)
set_fact:
converted_input_value_numeric: "{{ input_value_numeric | int }}"
- name: Show original value (numeric)
debug:
var: input_value_numeric
- name: Show converted value (numeric)
debug:
var: converted_input_value_numeric
- name: Print this line if the value was fully numeric (numeric)
debug:
msg: "Value fully numeric: {{ input_value_numeric }}"
when:
- input_value_numeric == converted_input_value_numeric
## Example with pure numeric input value
###########################################################################
- name: Create converted fact from input (alpha)
set_fact:
converted_input_value_alpha: "{{ input_value_alpha | int }}"
- name: Show original value (alpha)
debug:
var: input_value_alpha
- name: Show converted value (alpha)
debug:
var: converted_input_value_alpha
- name: Print this line if the value was fully numeric (alpha)
debug:
msg: "Value fully numeric: {{ input_value_alpha }}"
when:
- input_value_alpha == converted_input_value_alpha
## Example with pure alpha input value
###########################################################################
- name: Create converted fact from input (mixed)
set_fact:
converted_input_value_mixed: "{{ input_value_mixed | int }}"
- name: Show original value (mixed)
debug:
var: input_value_mixed
- name: Show converted value (mixed)
debug:
var: converted_input_value_mixed
- name: Print this line if the value was fully numeric (mixed)
debug:
msg: "Value fully numeric: {{ input_value_mixed }}"
when:
- input_value_mixed == converted_input_value_mixed
## Example with mixed input value
###########################################################################
- name: Create converted fact from input (mixed)
set_fact:
converted_input_value_mixed: "{{ input_value_mixed | int }}"
- name: Show original value (mixed)
debug:
var: input_value_mixed
- name: Show converted value (mixed)
debug:
var: converted_input_value_mixed
- name: Print this line if the value was fully numeric (mixed)
debug:
msg: "Value fully numeric: {{ input_value_mixed }}"
when:
- input_value_mixed == converted_input_value_mixed
## Practical example
###########################################################################
- name: Create converted fact from your input variable
set_fact:
my_var_numeric_conversion: "{{ my_var | int }}"
- name: Do stuff if the value was numeric
debug:
msg: "my_var is fully numeric: {{ my_var }}"
when:
- my_var == my_var_numeric_conversion
- name: Do stuff if the value was NOT numeric
debug:
msg: "my_var is NOT fully numeric: {{ my_var }}"
when:
- my_var != my_var_numeric_conversion
You could check that foo
is a interger and not a string and that it is a whole and positive number like this (note that it might be fine in your case with foo
being a variable of type str
and not int
, if so omit the first check):
- name: Ensure that foo is a whole positive number
assert:
that:
- foo | type_debug == "int"
- foo is regex("^[0-9]+$")