Ansible滚动重启多集群环境

时间:2020-06-14 21:15:14

标签: ansible ansible-2.x ansible-inventory ansible-facts ansible-template

我有一个用例,我想在多个集群中执行滚动重启。第一步,我创建了一个任务,该任务要在传递给该任务的主机上顺序执行,该任务将一次执行一次重新启动一个节点。当前,假设所有群集中的节点总数为n,并且节点重启的时间恒定,则节点重启需要O(n)时间。我想优化此任务的执行,以使重新启动节点的任务在群集中串行执行,但在整个群集中并行执行,从而导致有效的重新启动时间与群集中主机的最大数量成比例。给定清单的示例-

[Cluster_1]
host_1_a
host_1_b

[Cluster_2]
host_2_a
host_2_b

[Cluster_3]
host_3_a
host_3_b
host_3_c

当前执行时间为7 *(reboot_time),我想将其优化为3 *(reboot_time)。我不确定这是否可行,是否欢迎提出任何建议。谢谢!

可以重启服务的剧本:

- name: Rolling reboot
  hosts: all
  serial: 1
  gather_facts: no

  tasks:
    - name: stop service
      service:
        name: xyz
        state: stopped

    - name: start service
      service:
        name: xyz
        state: started

    - name: wait for service to start
      uri:
        url: "http://localhost:8081/node-health"
        method: GET
      register: http_status
      until: http_status.status == 200
      retries: 60
      delay: 10

1 个答案:

答案 0 :(得分:2)

一种非常简单的方法是为每个群集并行启动该剧本。这实际上是我的首选解决方案。

ansible-playbook -i inventory -l Cluster_1 playbook.yml 2>&1 > mylog1.txt &
ansible-playbook -i inventory -l Cluster_2 playbook.yml 2>&1 > mylog2.txt &
ansible-playbook -i inventory -l Cluster_3 playbook.yml 2>&1 > mylog3.txt &

同时,如果您真的想一次执行此操作,那么这里是(可能是)解决方案。这个想法是动态创建混合现有群集服务器列表的服务器列表。请注意,这绝对不是防弹的,使其完全可扩展可能会陷入噩梦。

无论如何,这可以给您一些想法。关键是使用zip_longest filter创建列表,然后映射select filter以删除空值并展平最终结果。

---
- name: Create a dynamic group mixing hosts from each cluster
  hosts: localhost
  gather_facts: false

  tasks:
    - add_host:
        name: "{{ item }}"
        groups:
          - all_clusters_imbricated
      loop: "{{ groups['Cluster_1'] | zip_longest(groups['Cluster_2'], groups['Cluster_3']) | list | map('select') | map('list') | flatten }}"

- name: Loop serially over all clusters
  hosts: all_clusters_imbricated
  gather_facts: false
  serial: 3

  tasks:
    - debug:
        msg: I would restart host {{ inventory_hostname }}

其中给出了您当前的示例广告资源:

PLAY [Create a dynamic group mixing hosts from each cluster] ***************************************************************************************************************************************************************************

TASK [add_host] ************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=host_1_a)
changed: [localhost] => (item=host_2_a)
changed: [localhost] => (item=host_3_a)
changed: [localhost] => (item=host_1_b)
changed: [localhost] => (item=host_2_b)
changed: [localhost] => (item=host_3_b)
changed: [localhost] => (item=host_3_c)

PLAY [Loop serially over all clusters] *************************************************************************************************************************************************************************************************

TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [host_1_a] => {
    "msg": "I would restart host host_1_a"
}
ok: [host_2_a] => {
    "msg": "I would restart host host_2_a"
}
ok: [host_3_a] => {
    "msg": "I would restart host host_3_a"
}

PLAY [Loop serially over all clusters] *************************************************************************************************************************************************************************************************

TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [host_1_b] => {
    "msg": "I would restart host host_1_b"
}
ok: [host_2_b] => {
    "msg": "I would restart host host_2_b"
}
ok: [host_3_b] => {
    "msg": "I would restart host host_3_b"
}

PLAY [Loop serially over all clusters] *************************************************************************************************************************************************************************************************

TASK [debug] ***************************************************************************************************************************************************************************************************************************
ok: [host_3_c] => {
    "msg": "I would restart host host_3_c"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
host_1_a                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_1_b                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_2_a                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_2_b                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_3_a                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_3_b                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_3_c                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0