ansible邏輯控制語句

條件判斷語句 when

  1. when 的基本用法
    when 是條件判斷語句,類似編程語言的if
tasks:
    - command: /bin/false
      register: result
      ignore_errors: True
    - command: /bin/command
      when: result |failed
    - commadn: /bin/command_else
      when: result|success
    - command: /bin/command_else_else
      when: result|skipped
  1. 與include一起使用
- include: tasks/sometasks.yml
  when: “‘reticulating splines’ in output”

  1. 與role 一起使用
- host: all
roles:
    - {role: debian_stock_config, when: ansible_os_family == 'debian'}

loop 循環(huán)

  1. 字典循環(huán)
    with_items 用于迭代的list類型變量,支持簡單的字符列表,哈希列表
- name: add several users
  user: name{{item.name}} state=present groups={{item.groups}}
  with_iterms:
    - {name: 'name1', groups: 'group1'}
    - {name" 'name2', groups: 'group2'}

使用 .號訪問內(nèi)層和外層的變量

  1. 循環(huán)也可以嵌套,使用[ ]訪問內(nèi)層和外層的循環(huán),例如item[0]
  2. 對哈希表的循環(huán)
    在變量文件中或者使用vars區(qū)域定義了一組列表變量items,可以這樣使用
vars:
    items: ["user1","user2"]
tasks:
    ....

    with_items: "{{iterms}}"
    with_dict: "{{字典名}}"
  1. 文件循環(huán)(with_file, with_fileglob)
      with_file 是將每個(gè)文件的文件內(nèi)容作為item的值
      with_fileglob 是將每個(gè)文件的全路徑作為item的值, 在文件目錄下是非遞歸的, 如果是在role里面應(yīng)用改循環(huán), 默認(rèn)路徑是roles/role_name/files_directory
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
      with_fileglob:
        - /playbooks/files/fooapp/*

bolck 塊

  1. 使用block關(guān)鍵字可以將多個(gè)任務(wù)整合成一個(gè)塊,把這個(gè)塊當(dāng)成一個(gè)整體,對這個(gè)塊進(jìn)行判斷,當(dāng)條件成立時(shí),執(zhí)行塊中的所以語句
tasks:
  - debug:
      msg: "task1 not in block"
  - block:
      - debug:
          msg: "task2 in block1"
      - debug:
          msg: "task3 in block1"
    when: 2 > 1
  1. 錯(cuò)誤處理功能
    錯(cuò)誤處理功能就是當(dāng)任務(wù)出錯(cuò)時(shí),執(zhí)行指定的其他任務(wù)
**failed 的用法**
tasks:
  - block:
      - shell: 'ls /ooo'
    rescue:
      - debug:
          msg: 'I caught an error'

**rescue的用法**
tasks:
  - block:
      - shell: 'ls /opt'
      - shell: 'ls /testdir'
      - shell: 'ls /c'
    rescue:
      - debug:
          msg: 'I caught an error'
    always:
      - debug:
          msg:"this always executes"

如上例所示,block中有三個(gè)任務(wù),這三個(gè)任務(wù)中的任何一個(gè)任務(wù)錯(cuò)了,就會執(zhí)行rescue中的任務(wù),所以通常會使用block和rescue結(jié)合,完成“錯(cuò)誤捕捉,報(bào)出異常”的功能
我們還可以加入always關(guān)鍵字,加入always以后,無論block中的任務(wù)執(zhí)行成功還是失敗,always中的任務(wù)總是被執(zhí)行。

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

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

  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時(shí),會觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 5,985評論 0 9
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,258評論 0 38
  • 1 Block機(jī)制 (Very Good) Block技巧與底層解析 http://www.itdecent.cn...
    Kevin_Junbaozi閱讀 4,163評論 3 48
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,675評論 1 32
  • 坐上車,打開音樂,心想必須找個(gè)喜歡的歌,哼,必須滴。結(jié)果〈朋友的酒〉從耳機(jī)穿出,所有準(zhǔn)備偃旗息鼓,就是喜歡嘛。 所...
    欣然_簡然閱讀 411評論 1 2

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