我有我的剧本如下:
---
- 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的值然后执行“检查名称”
我在这里错过了什么吗?如何让它工作?
它工作正常,但您正在将字符串与整数进行比较。
正如您在输出中看到的那样:
"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