當已經在 ansible 中使用串列時,扇出到其他主機

當已經在 ansible 中使用串列時,扇出到其他主機

我有 80 多個主機運行我的應用程序,我正在更新長期存在的 ansible playbook 來更改我們的負載平衡器。在我們目前的負載平衡器設定中,可以透過呼叫 AWS CLI 在一個 ansible 操作中向負載平衡器新增/刪除主機。然而,我們正在切換到在少數我們自己的主機上配置的負載平衡器,並且我們將透過使用 ansible 操作這些主機上的文字檔案來引入和退出主機。本質上,我需要在使用串行時在劇本中的不同主機上進行內部循環。

我在建置 playbook 時遇到問題,以便我可以將blockinfile命令扇出到群組中的主機tag_Type_edge,同時部署到tag_Type_app具有序列的 80 台主機:25%。

這就是我想要做的:

---
- hosts: tag_Type_app
  serial: "25%"
  pre_tasks:

    - name: Gathering ec2 facts
      action: ec2_metadata_facts

    - name: Remove from load balancers
      debug:
        msg: "This is where I'd fan out to multiple different hosts from group tag_Type_edge to manipulate
              text files to remove the 25% of hosts from tag_Type_app from the load balancer"

  tasks:
    - name: Do a bunch of work to upgrade the app on the tag_Type_app machines while out of the load balancer
      debug:
        msg: "deploy new code, restart service"


  post_tasks:
    - name: Put back in load balancer
        debug:
          msg: "This is where I'd fan out to multiple different hosts from group tag_Type_edge to manipulate
                 text files to *add* the 25% of hosts from tag_Type_app back into the load balancer"

我怎麼能構造這個以允許內部循環,tag_Type_edge同時在所有盒子上使用serial: 25% tag_Type_app

答案1

在 Ansible 中,實際上在一台主機上執行一個任務,但代表另一台主機,可能需要使用一個聰明的東西,稱為代表團。文件中的一個範例確實是將主機從負載平衡器中取出。

與任何任務一樣,它可以在某些主機名稱上循環運行多次。不是播放循環,而是任務循環。一個例子會有所幫助,採用你的骨架:

---
- name: Example loop over load balancers for each app server
  hosts: tag_Type_app
  serial: "25%"

  pre_tasks:
    - name: Gathering ec2 facts
      action: ec2_metadata_facts

    - name: Remove from load balancers
      debug:
        msg: "Remove app instance {{ inventory_hostname }} from edge node {{ item }}"
      delegate_to: "{{ item }}"
      loop: "{{ query('inventory_hostnames', 'tag_Type_edge') }}"

  tasks:
    - name: Do a bunch of work to upgrade the app on the tag_Type_app machines while out of the load balancer
      debug:
        msg: "deploy new code, restart service"

  post_tasks:
    - name: Put back in load balancer
      debug:
        msg: "Add app instance {{ inventory_hostname }} to edge node {{ item }}"
      delegate_to: "{{ item }}"
      loop: "{{ query('inventory_hostnames', 'tag_Type_edge') }}"

inventory_hostnames 是一個查找插件,用於執行庫存模式,任何模式都可以在那裡。

關於這件事有一些棘手的事情。 “edge”循環中的任何故障都會導致“app”主機發生故障。這將使某些邊緣主機的部分狀態保持啟用狀態,而有些則不會啟用。除非你有某種退出機制,例如有救援功能的方塊可以撤銷它。

作為任務循環,一些庫存和遊戲相關的功能將不適用。您無法--limit在命令列上進一步限制邊緣主機。你也不能用serial它們來批量處理,這已經在一場比賽中了。

此外,它還會運行相對大量的任務,至少有 app ✕ edge ✕ 2 個。可以透過增加分叉來稍微緩解。

如果您的多主機負載平衡器有一個控制平面,則不需要接觸這麼多主機。每個應用程式伺服器執行一項任務。您目前可能還沒有做好準備,但需要考慮一些事情。

相關內容