天天看點

ansible6:ansible條件判斷

目錄

  • when
  • tests
    • 判斷路徑
    • 判斷變量
    • 判斷執行結果
    • 判斷字元串
    • 判斷整除
    • 其它判斷
  • 其它說明
    • block
  • 錯誤判斷

  1. 直接舉例(此處牽扯到with_item循環可在第8節檢視)。
    0 18:10:49 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "{{item}}"
        with_items: [1,2,5,52]
        when: item > 2
      0 18:10:50 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
               
  2. ansible運算符:
    • 比較運算符:==、!=、>、>=、<、<=、
    • 邏輯運算符:and、or、not
    0 18:14:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: task1
        shell: "ls -l /wula"
        register: return_info
        ignore_errors: true		# 忽略報錯,繼續往下執行。
      - name: task2
        debug:
          msg: "Command execution successful"
        when: return_info.rc == 0		# task1任務的傳回值。
      - name: task3
        debug:
          msg: "Command execution failed"
        when: return_info.rc != 0
      0 18:14:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test2.yaml
               

  jinja2模闆中的tests是一類判斷的統稱,下面一些test判斷均針對于ansible控制主機,與目标主機無關。每一種判斷都有兩種情況:is和is not,下面簡單舉例說明。

  1. exists:判斷檔案是否存在。
    • is exists:檔案存在則為真。
    • is not exists:檔案不存在則為真。
    0 18:44:02 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test3.yaml 
    ---
    - hosts: ck-node1
      vars:
        testdir: /root/temp
      tasks:
      - debug:
          msg: "file exist"
        when: "testdir is exists"
      0 18:44:06 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test3.yaml
               
  2. file:判斷路徑是否是一個檔案,如果路徑是一個檔案則傳回真。
  3. directory:判斷路徑是否是一個目錄,如果路徑是一個目錄則傳回真。
  4. link:判斷路徑是否是一個軟連結,如果路徑是一個軟連結則傳回真
  5. mount:判斷路徑是否是一個挂載點,如果路徑是一個挂載點則傳回真

  1. defined:判斷變量是否已經定義,已經定義則為真。
  2. undefined:判斷變量是否已經定義,未定義則傳回真。
  3. none:判斷變量值是否為空,如果變量已定義且值為空,則傳回真。

  1. success或succeeded:通過任務的傳回資訊判斷任務的執行狀态,任務執行成功,則傳回真。
  2. failure 或 failed:通過任務的傳回資訊判斷任務的執行狀态,任務執行失敗,則傳回真。
  3. change 或 changed:通過任務的傳回資訊判斷任務的執行狀态,任務執行狀态為changed,則傳回真。
  4. skip 或 skipped:通過任務的傳回資訊判斷任務的執行狀态,當任務沒有滿足條件,而被跳過執行時,則傳回真。

  1. lower:判斷包含字母的字元串中的字母是否是純小寫,字元串中的字母全部為小寫則傳回真
  2. upper:判斷包含字母的字元串中的字母是否是純大寫,字元串中的字母全部為大寫則傳回真
  3. string:判斷對象是否是一個字元串,是字元串則傳回真

  1. even :判斷數值是否是偶數,是偶數則傳回真
  2. odd :判斷數值是否是奇數,是奇數則傳回真
  3. divisibleby(num) :判斷是否可以整除指定的數值,如果除以指定的值以後餘數為0,則傳回真

  1. number:判斷對象是否是一個數字,是數字則傳回真。
    0 18:45:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test4.yaml
    ---
    - hosts: ck-node1
      vars:
        var1: 1
        var2: "1"
        var3: 0.1
      tasks:
      - name: task1
        debug:
          msg: "this variable is number"
        when: var1 is number
      - name: task2
        debug:
          msg: "this variable is number"
        when: var2 is number
      - name: task3
        debug:
          msg: "this variable is number"
        when: var3 is number
      0 18:45:54 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test4.yaml
               

  1. 使用when判斷,條件成立後隻能執行一個任務,想要在條件成立後執行多個任務就需要用到block塊。
    0 19:10:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test5.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "task1 not in block"
      - block:
        - debug:
            msg: "task2 in block"
        - debug:
            msg: "task3 in block"
        when: 2 > 1
      0 19:10:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test5.yaml
               
  2. 使用block+rescue實作錯誤判斷。
    # 學block之前可以使用tests的faild或者success方法來判斷。
      0 19:16:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test6.yaml
    ---
    - hosts: ck-node1
      tasks:
      - shell: "ls -l /root/wula"
        register: return_info
        ignore_errors: true
      - debug:
          msg: "command exec failed"
        when: return_info is failed
      0 19:16:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test6.yaml
    # 現在可以使用block實作錯誤判斷(此種方式會自動忽略錯誤繼續執行,不用再加“ignore_errors: true”)。
      0 19:19:11 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test7.yaml
    ---
    - hosts: ck-node1
      tasks:
      - block:
        - shell: "ls -l /root/wula"
        rescue:
        - debug:
            msg: "command exec failed"
      0 19:19:12 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test7.yaml
               
  1. 在shell中,判斷某個條件成立後,立即中止腳本的運作可以使用exit,在ansible中想要實作此功能則需要借助fail子產品。預設情況下,playbook執行過程中出錯會自動中止,除非設定了ignore_errors: true,fail子產品就可以實作“執行失敗”的場景。
    0 19:22:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test8.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "1"
      - debug:
          msg: "2"
      - fail:
          msg: "interrupt running playbook"		# “msg”用來自定義錯誤輸出資訊,可以不定義,預設是“Failed as requested from task”
      - debug:
          msg: "3"
      0 19:22:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test8.yaml
               
  2. 通常不會無緣無故想要中止playbook,fail一般與when連用,當觸發某種條件時中斷playbook。
    0 19:29:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test9.yaml
    ---
    - hosts: ck-node1
      tasks:
      - shell: "echo error"
        register: return_info
      - fail:
          msg: "commad exec failed"        
        when: "'error' in return_info.stdout" 
      - debug:
          msg: "how are you"
      0 19:29:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test9.yaml
               
  3. failed_when:在條件成立時,将對應任務的執行狀态設定為FAILED。跟fail+when的作用類似。
    0 19:33:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test10.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "wula"
      - shell: "echo error"
        register: return_info
        failed_when: "'error' in return_info.stdout" 
      - debug:
          msg: "how are you"
      0 19:33:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test10.yaml
               
  4. changed_when:在條件成立時,将對應任務的執行狀态設定為changed。可以将下面列子中的changed_when改成when對比一下,會發現when對應的是綠色的“OK”狀态”,changend_when對應的是黃色的“changed”狀态。
    0 19:36:18 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test11.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "test message"
        changed_when: 2 > 1
      0 19:36:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test11.yaml