大网数据广告活动
服务器运维问答

How can I check numeric string

· 52CE 我爱测

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 ca…

问题原文

原帖链接 · Henry TheKing · CC BY-SA 4.0

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]')

已采纳解答

原帖链接 · John B · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。