并行运行某些可完成的任务

时间:2019-03-13 23:20:19

标签: ansible

我有一个简单的ansible剧本,我希望前三个任务并行运行,然后在其余三个任务完成后运行。我看过异步,但是我不太了解它是如何实现的。

现在我有这个:

---
- name: gkeop
  hosts: all
  gather_facts: false
  tasks:
    - name: adminWsInstall
      vmware_deploy_ovf:
        hostname: '{{ hostname_vcenter }}'
        username: '{{ username }}'
        password: '{{ password }}'
        ...
      delegate_to: localhost
    - name: vcenterInstall
      vmware_deploy_ovf:
        hostname: '{{ hostname_vcenter }}'
        username: '{{ username }}'
        password: '{{ password }}'
        ...
      delegate_to: localhost
    - name: f5Install
      vmware_deploy_ovf:
        hostname: '{{ hostname_vcenter }}'
        username: '{{ username }}'
        password: '{{ password }}'
        ...
      delegate_to: localhost
    - name: Cluster Add
      template:
        src: cluster.sh.j2
        dest: /home//cluster.sh
        mode: 0777
    - name: F5 License
      template:
        src: f5.sh.j2
        dest: /home/f5.sh
        mode: 0777
    - name: Run cluster add
      command: sh /home/cluster.sh
      become: true
    - name: Run f5 license
      command: sh /home/f5.sh
      become: true

所以我只添加

async: 600 
poll: 5 

这三个任务中的第一个任务是否会使它们并行运行?

1 个答案:

答案 0 :(得分:1)

第四项任务应检查其他任务是否已完成。看看这个示例剧本:

- hosts: loc
  tasks:
    - name: sleep 10 seconds
      shell: sleep 10
      async: 10
      poll: 0
      register: sleeper1
    - name: sleep 10 seconds again
      shell: sleep 10
      async: 10
      poll: 0
      register: sleeper2
    - name: sleep 10 seconds again
      shell: sleep 10
      async: 10
      poll: 0
      register: sleeper3
    - name: wait for all 3 tasks
      async_status: jid="{{ item }}"
      register: job_result
      until: job_result.finished
      retries: 10
      with_items:
        - "{{ sleeper1.ansible_job_id }}"
        - "{{ sleeper2.ansible_job_id }}"
        - "{{ sleeper3.ansible_job_id }}"
    - name: next task
      debug:
        msg: "ready"

只有完成其他任务后,最后一个任务才会运行。希望有帮助。