Ansible 的委托 並發和任務超時


異步和輪詢

Ansible 有時候要執行等待時間很長的操作,  這個操作可能要持續很長時間, 設置超過ssh的timeout. 這時候你可以在step中指定async 和 poll 來實現異步操作

async 表示這個step的最長等待時長,  如果設置為0, 表示一直等待下去直到動作完成.

poll 表示檢查step操作結果的間隔時長.

例1:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 10
      poll: 2

結果:
TASK: [wair for] ************************************************************** 
ok: [localhost]
<job 207388424975.101038> polling, 8s remaining
ok: [localhost]
<job 207388424975.101038> polling, 6s remaining
ok: [localhost]
<job 207388424975.101038> polling, 4s remaining
ok: [localhost]
<job 207388424975.101038> polling, 2s remaining
ok: [localhost]
<job 207388424975.101038> polling, 0s remaining
<job 207388424975.101038> FAILED on localhost

這個step失敗, 因為操作時間超過了最大等待時長

例2:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 10
      poll: 0

結果:
TASK: [wair for] ************************************************************** 
<job 621720484791.102116> finished on localhost

PLAY RECAP ********************************************************************
 poll 設置為0, 表示不用等待執行結果, 該step執行成功

例3:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 0
      poll: 10

結果:
# time ansible-playbook xiama.yml 
TASK: [wair for] ************************************************************** 
changed: [localhost]

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=1    unreachable=0    failed=0   


real    0m16.693s

async設置為0, 會一直等待直到該操作完成.

Play執行時的並發限制

一般情況下, ansible會同時在所有服務器上執行用戶定義的操作, 但是用戶可以通過serial參數來定義同時可以在多少太機器上執行操作.

- name: test play
  hosts: webservers
  serial: 3

webservers組中的3台機器完全完成play后, 其他3台機器才會開始執行,

serial參數在ansible-1.8以后就開始支持百分比.

最大失敗百分比

默認情況下, 只要group中還有server沒有失敗,  ansible就是繼續執行tasks. 實際上, 用戶可以通過"max_fail_percentage" 來定義, 只要超過max_fail_percentage台的server失敗, ansible 就可以中止tasks的執行.

- hosts: webservers
  max_fail_percentage: 30
serial: 10
Note: 實際失敗機器必須大於這個百分比時, tasks才會被中止. 等於時是不會中止tasks的.

委托

通過"delegate_to", 用戶可以把某一個任務放在委托的機器上執行.

- hosts: webservers
  serial: 5

  tasks:

  - name: take out of load balancer pool
    command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
    delegate_to: 127.0.0.1

上面的task會在跑ansible的機器上執行, "delegate_to: 127.0.0.1" 可以用local_action來代替

  tasks:

  - name: take out of load balancer pool
    local_action: command /usr/bin/take_out_of_pool {{ inventory_hostname }}

委托者的facts

默認情況下, 委托任務的facts是inventory_hostname中主機的facts, 而不是被委托機器的facts. 在ansible 2.0 中, 設置delegate_facts為true可以讓任務去收集被委托機器的facts.

- hosts: app_servers
  tasks:
    - name: gather facts from db servers
      setup:
      delegate_to: "{{item}}"
      delegate_facts: True
      with_items: "{{groups['dbservers'}}"

該例子會收集dbservers的facts並分配給這些機器, 而不會去收集app_servers的facts

RUN ONCE

通過run_once: true來指定該task只能在某一台機器上執行一次. 可以和delegate_to 結合使用

- command: /opt/application/upgrade_db.py
  run_once: true
  delegate_to: web01.example.org

指定在"web01.example.org"上執行這

如果沒有delegate_to, 那么這個task會在第一台機器上執行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM