1. subset、superset
[root@server4 pd]# cat pd10.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
a:
- 2
- 5
b: [1,2,3,4,5]
tasks:
- debug:
msg: "a subset of b"
when: a is subset(b)
- debug:
msg: "b superset of a"
when: b is superset(a)
测试:
[root@server4 pd]# ansible-playbook pd10.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "a subset of b"
}
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "b superset of a"
}
2. 判断一个对象是否是字符串
[root@server4 pd]# cat pd11.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
testvar1: 1
testvar2: "1"
testvar3: a
tasks:
- debug:
msg: "This testvar1 is a string"
when: testvar1 is string
- debug:
msg: "This testvar2 is a string"
when: testvar2 is string
- debug:
msg: "This testvar3 is a string"
when: testvar3 is string
测试:
[root@server4 pd]# ansible-playbook pd11.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
skipping: [172.25.60.3]
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "This testvar2 is a string"
}
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "This testvar3 is a string"
}
3. 判断对象是否为一个数字
[root@server4 pd]# cat pd12.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
testvar1: 1
testvar2: "1"
testvar3: 00.20
tasks:
- debug:
msg: "This testvar1 is a number"
when: testvar1 is number
- debug:
msg: "This testvar2 is a number"
when: testvar2 is number
- debug:
msg: "This testvar3 is a number"
when: testvar3 is number
测试:
[root@server4 pd]# ansible-playbook pd12.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "This testvar1 is a number"
}
TASK [debug] *******************************************************************
skipping: [172.25.60.3]
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "This testvar3 is a number"
}