Block-rescue-always

  1. 前面章節 Playbooks 介紹過區塊 (block),經常配合 when 修件在某些狀況下執行,當 block 執行任務出現錯誤時,執行 rescue 任務,而 always 則不管有無錯誤都執行。
    - name: Attempt and graceful roll back demo
      block:
        - debug:
            msg: 'I execute normally'
        - name: i force a failure
          command: /bin/false
        - debug:
            msg: 'I never execute, due to the above task failing, :-('
      rescue:
        - debug:
            msg: 'I caught an error'
        - name: i force a failure in middle of recovery! >:-)
          command: /bin/false
        - debug:
            msg: 'I also never execute :-('
      always:
        - debug:
            msg: "This always executes"
    
  2. debug 模組的 msg 列印客製化訊息,如果省略則預設列印 "Hello World!",查看 debug 模組範例。
    [deyu1@kvm19 ansible]$ ansible-doc debug | grep ^EX -A4
    EXAMPLES:
    
    # Example that prints the loopback address and gateway for each host
    - debug:
        msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}