Ansible:在一个任务中并行执行 delegate_to 循环

pan*_*nos 5 ansible

我使用 delegate_to 和嵌套循环在一组主机之间分发文件。

但在某些情况下,会存在性能问题,因为任务必须在一台主机上完成才能在下一台主机上执行,依此类推。

为了减少完成任务所需的时间,有没有办法并行执行 delegate_to with 循环?

- name: Create backup
  archive:
    path: xxxxx
    dest: xxxxx
    format: gz
  delegate_to: "{{hosts[ ( ((item[0] -1) + (item[1] | int)) - 1) % (hosts|length|int) ] }}"
  run_once: yes
  with_nested:
    - "{{loop_1}}"
    - "{{loop_2}}"    
Run Code Online (Sandbox Code Playgroud)

我已经尝试过异步模块,但我意识到它不适合我的用例。

Jir*_*i B 2

我将使用add_host任务将您的主机添加到内存组中,并在剧本中创建新的剧本。

一个例子:

- hosts: all
  tasks:
    - name: Add hosts to in-memory group
      add_host:
        name: '{{hosts[ ( ((item.0 -1) + (item.1 | int)) - 1) % (hosts|length|int)
          ] }}'
        group: _backup_hosts
      loop: '{{ loop1 | product(loop2) | list }}'
- hosts: _backup_hosts
  tasks:
    - name: Create backup
      archive:
        path: xxxxx
        dest: xxxxx
        format: gz
      run_once: true
Run Code Online (Sandbox Code Playgroud)