我如何检查数字字符串

我如何检查数字字符串

我正在使用 assert 模块来验证通过命令行定义的变量。我需要检查我的变量是否仅包含数字 (GID)。我该怎么做?我尝试了以下选项,但它们对我的情况不起作用

my_var is number
my_var|match('[0-9]')

答案1

我觉得肯定有更好的方法,但这应该可以解决您的眼前问题。这是一个演示逻辑的测试剧本;您的问题的具体解决方案仅涉及my_var顶部的定义和末尾的“实际示例”部分……其余的只是测试用例。

无论如何,这里的总体思路是,你获取原始my_var变量,将其传递给整数转换(如上文 Michael Hampton 所建议的那样……抱歉,这是我的第一个答案,我不确定如何超链接,但我想你可以找到它 :)),然后在后续任务中添加一个 when 子句,检查整数转换的结果是否与原始值匹配。换句话说,如果整数转换没有改变任何东西,那么原始值一定已经是整数了。你也可以使用Ansible“阻止”将该 when 子句同时应用于多个任务。

---
## 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

答案2

您可以检查它foo是一个整数而不是一个字符串,并且它是一个像这样的整数和正数(请注意,在您的情况下,它是一个类型而不是foo的变量可能是可以的,如果是这样,请省略第一个检查):strint

- name: Ensure that foo is a whole positive number
  assert:
    that:
      - foo | type_debug == "int"
      - foo is regex("^[0-9]+$")

答案3

您可以使用将字符串更改为整数的过滤器。

例如:

  when: ansible_distribution_major_version|int == 7

如果字符串实际上不包含数字,则将返回 0。

相关内容