注册事实在Ansible中的“when”条件下不起作用

问题描述 投票:1回答:1

我有我的剧本如下:

---
- hosts: myser
  tasks:
  - name: Checking.
    win_command: mycommand
    register: win_command_result

  - set_fact:
      myvar={{win_command_result.stdout | regex_search('\\d+')}}
    register: myvar_result

  - debug:
      var: myvar_result.ansible_facts.ple

  - name: Checking Condition
    win_command: ipconfig
    register: ipconfig
    when: myvar_result.ansible_facts.ple < 5000

  - debug:
      var: ipconfig

以下是输出。

我得到每个服务器两个不同的值,但任务Checking Condition被跳过。基于该值,对于一个服务器,它应该跳过,而另一个服务器应该执行。

PLAY [myser] 
*******************************************************

TASK [Gathering Facts] 
**************************************************
ok: [ser1]
ok: [ser2]

TASK [Checking] 
****************************
changed: [ser1]
changed: [ser2]

TASK [set_fact] 
*********************************************************
ok: [ser1]
ok: [ser2]

TASK [debug] 
************************************************************
ok: [ser1] => {
    "myvar_result.ansible_facts.ple": "232"
}
ok: [ser2] => {
    "myvar_result.ansible_facts.ple": "378416"
}

TASK [Checking Condition] 
**********************************************
skipping: [ser1]
skipping: [ser2]

TASK [debug] 
************************************************************
ok: [ser1] => {
    "ipconfig": {
        "changed": false, 
        "skip_reason": "Conditional result was False", 
        "skipped": true
    }
}
ok: [ser2] => {
    "ipconfig": {
        "changed": false, 
        "skip_reason": "Conditional result was False", 
        "skipped": true
    }
}

PLAY RECAP 
**************************************************************
ser2 : ok=5    changed=1    unreachable=0    failed=0   
ser1 : ok=5    changed=1    unreachable=0    failed=0   

我想在myvar_result.ansible_facts.ple条件下使用when。所以这里的想法是,如果myvar_result.ansible_facts.ple超过5000的值然后执行“检查名称”

我在这里错过了什么吗?如何让它工作?

ansible
1个回答
0
投票

它工作正常,但您正在将字符串与整数进行比较。

正如您在输出中看到的那样:

"myvar_result.ansible_facts.ple": "232"
"myvar_result.ansible_facts.ple": "378416"

你的值是字符串(就像在stdout中传递的任何命令结果一样,以及regex_search过滤器的输出)。


在条件中进行比较之前将它们转换为整数:

- name: Checking Condition
  win_command: ipconfig
  register: ipconfig
  when: myvar_result.ansible_facts.ple|int < 5000
© www.soinside.com 2019 - 2024. All rights reserved.