1.判斷目錄或文件是否存在
- hosts: cf601
remote_user: root
gather_facts: no
vars:
testpath: /testdir
tasks:
- debug:
msg: "file exist"
when: testpath is exists #不存在就用testpath is not exists
2.判斷變量
defined:判斷變量是否已經(jīng)定義,已經(jīng)定義則為真
undefined:判斷變量是否已經(jīng)定義总放,未定義則返回真
none:判斷變量是否已經(jīng)定義哺眯,如果變量值已經(jīng)定義谷浅,但是變量值為空,則返回真
- hosts: cf601
remote_user: root
gather_facts: no
vars:
testvar: "test"
testvar1:
tasks:
- debug:
msg: "Variable is defined"
when: testvar is defined
- debug:
msg: "Variable is undefined"
when: testvar2 is undefined
- debug:
msg: "The variable is defined, but there is no value"
when: testvar1 is none
3.判斷執(zhí)行結(jié)果
success或succeeded: 通過(guò)任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài)奶卓,任務(wù)執(zhí)行成功則返回真
failure或failed: 通過(guò)任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài)一疯,任務(wù)執(zhí)行失敗則返回真
change或changed: 通過(guò)任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài),任務(wù)執(zhí)行狀態(tài)為changed則返回真
skip或skipped: 通過(guò)任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài)夺姑,當(dāng)任務(wù)沒(méi)有滿足條件墩邀,而被跳過(guò)執(zhí)行時(shí),則返回真
- hosts: cf601
remote_user: root
gather_facts: no
vars:
doshell: "yes"
tasks:
- shell: "cat /testdir/abc"
when: doshell == "yes"
register: returnmsg
ignore_errors: true
- debug:
msg: "success"
when: returnmsg is success
- debug:
msg: "failed"
when: returnmsg is failure
- debug:
msg: "changed"
when: returnmsg is change
- debug:
msg: "skip"
when: returnmsg is skip
稍加改變看看三種情況的結(jié)果
4.判斷路徑
file: 判斷路徑是否是一個(gè)文件盏浙,如果路徑是一個(gè)文件則為真(該參數(shù)在實(shí)際使用我碰到過(guò)問(wèn)題眉睹,暫時(shí)不考慮使用可以使用stat來(lái)代替例如:
- name: check if exists /usr/local/bin/python3
stat: path={{ py3_dir }}
register: dir_stat
- name: check py3 is installed and force exit
fail: msg="python3 has been installed already!"
when: dir_stat.stat.exists
)
directory: 判斷路徑是否是一個(gè)目錄,如果路徑是一個(gè)目錄則為真
link: 判斷路徑是否是一個(gè)軟連接废膘,如果路徑是一個(gè)軟連接則為真
mount: 判斷路徑是否是一個(gè)掛載點(diǎn)竹海,如果路徑是一個(gè)掛載點(diǎn)則為真
exists: 判斷路徑是否存在,如果路徑存在則為真
注意:某些版本之前可能需要加上“is_”前綴
- hosts: cf601
remote_user: root
gather_facts: no
vars:
testpath1: "/testdir/test"
testpath2: "/testdir/"
testpath3: "/testdir/testsoftlink"
testpath4: "/testdir/testhardlink"
testpath5: "/boot"
tasks:
- debug:
msg: "file"
when: testpath1 is file
- debug:
msg: "directory"
when: testpath2 is directory
- debug:
msg: "link"
when: testpath3 is link
- debug:
msg: "link"
when: testpath4 is link
- debug:
msg: "mount"
when: testpath5 is mount
- debug:
msg: "exists"
when: testpath1 is exists
5.判斷整除(比較少用)
even: 判斷數(shù)值是否是偶數(shù)丐黄,偶數(shù)則為真
odd: 判斷數(shù)值是否是奇數(shù)斋配,奇數(shù)則為真
divisibleby(num): 判斷是否可以整除指定的數(shù)值,如果除以指定的值以后余數(shù)為0孵稽,則返回真
- hosts: cf601
remote_user: root
gather_facts: no
vars:
num1: 4
num2: 7
num3: 64
tasks:
- debug:
msg: "An even number"
when: num1 is even
- debug:
msg: "An odd number"
when: num2 is odd
- debug:
msg: "Can be divided exactly by"
when: num3 is divisibleby(8)
5.其他
version: 可以用于對(duì)比兩個(gè)版本號(hào)的大小许起,或者與指定的版本號(hào)進(jìn)行對(duì)比,語(yǔ)法version('版本號(hào)'菩鲜,'比較操作符')
2.5版本此test從version_compare 更名為version
- hosts: test70
remote_user: root
vars:
ver: 7.4.1708
ver1: 7.4.1707
tasks:
- debug:
msg: "This message can be displayed when the ver is greater than ver1"
when: ver is version(ver1,">")
- debug:
msg: "system version {{ansible_distribution_version}} greater than 7.3"
when: ansible_distribution_version is version("7.3","gt")
這里的例子里面兩種比較都是可以的 支持多種風(fēng)格操作符园细。
subset: 判斷一個(gè)list是不是另一個(gè)list的子集,是則為真
siperset: 判斷一個(gè)list是不是另一個(gè)list的父集接校,是則為真
注:2.5版本之前是issubset和issuperset
- hosts: cf601
remote_user: root
gather_facts: no
vars:
a:
- 2
- 5
b: [1,2,3,4,5]
tasks:
- debug:
msg: "A is a subset of B"
when: a is subset(b)
- debug:
msg: "B is the parent set of A"
when: b is superset(a)
string: 判斷對(duì)象是否是一個(gè)字符串猛频,是則為真
number: 判斷對(duì)象是否是一個(gè)數(shù)字狮崩,是則為真
- hosts: cf601
remote_user: root
gather_facts: no
vars:
testvar1: 1
testvar2: "1"
testvar3: a
tasks:
- debug:
msg: "This variable is a string"
when: testvar1 is string
- debug:
msg: "This variable is a string"
when: testvar2 is string
- debug:
msg: "This variable is a string"
when: testvar3 is string
- debug:
msg: "This variable is number"
when: testvar1 is number
- debug:
msg: "This variable is a number"
when: testvar2 is number
- debug:
msg: "This variable is a number"
when: testvar3 is number
轉(zhuǎn)載自:http://www.zsythink.net/archives/2817