ansible之條件判斷

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: 通過任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài),任務(wù)執(zhí)行成功則返回真
failure或failed: 通過任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài),任務(wù)執(zhí)行失敗則返回真
change或changed: 通過任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài),任務(wù)執(zhí)行狀態(tài)為changed則返回真
skip或skipped: 通過任務(wù)的返回信息判斷任務(wù)的執(zhí)行狀態(tài),當(dāng)任務(wù)沒有滿足條件,而被跳過執(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í)際使用我碰到過問題,暫時(shí)不考慮使用可以使用stat來代替例如:

- 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ì)比,語法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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,727評(píng)論 0 5
  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,353評(píng)論 0 10
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,382評(píng)論 0 17
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,681評(píng)論 1 32
  • 此三小只非彼三小只,所以,大家不要誤會(huì)。 我說的三小只我家的,大黑,小白和小不點(diǎn)。 昨天立秋就開始電閃雷鳴,狂風(fēng)暴...
    是的比心閱讀 342評(píng)論 0 1

友情鏈接更多精彩內(nèi)容