如何通过主机组并行化执行

问题描述 投票:0回答:1

我正在按站点动态创建主机组,并且在下面的配方中效果很好。

- name: "generate batches from batch_processor"
  batch_processor:
    inventory: "{{ inventory_url }}"
    ignore_applist:  "{{ ignore_applist | default('None') }}"
    ignore_podlist:  "{{ ignore_podlist | default('None') }}"
  register: batch_output

  - name: "Creating batches by site"
  include: batch_formatter.yml hostkey="{{ 'batch_' + item.key }}" hostlist="{{ item.value | list | join(',') }}"
  with_dict: "{{ batch_output.result['assignments'] }}"

在我的剧本中,我喜欢这样

- hosts: batch_*
  serial: 10
  gather_facts: False
  tasks:
    - include: roles/deployment/tasks/dotask.yml

我最初有strategy: free,但由于某种原因,它没有并行运行。目前,我正在一次使用所有批处理主机10进行部署。

我正在考虑以下项目batch_site1-并行10batch_site2-并行10batch_site3-并行10个

但是在剧本中,我不想按站点指定主机组执行,因为它们是动态的。有时,我们将拥有siteX,有时它将不在那里。请提出最佳方法。

ansible ansible-inventory
1个回答
0
投票
- hosts: batch_*
  gather_facts: False
  strategy: free
  tasks:
    - include: roles/deployment/tasks/dotask.yml
      when: "'batch_site1' not in group_names"
      async: 600
      poll: 0
      register: job1

    - name: Wait for asynchronous job to end
      async_status:
        jid: '{{ job1.ansible_job_id }}'
      register: job_result1
      until: job_result1.finished
      retries: 30

    - include: roles/deployment/tasks/dotask.yml
      when: "'batch_site2' not in group_names"
      register: job2
      async: 600
      poll: 0

    - name: Wait for asynchronous job to end
      async_status:
        jid: '{{ job2.ansible_job_id }}'
      register: job_result2
      until: job_result2.finished
      retries: 30

    - include: roles/deployment/tasks/dotask.yml
      when: "'batch_site3' not in group_names"
      register: job3
      async: 600
      poll: 0

    - name: Wait for asynchronous job to end
      async_status:
        jid: '{{ job3.ansible_job_id }}'
      register: job_result3
      until: job_result3.finished
      retries: 30

此配置对我的目的有点工作,但是我无法拉出async_status在屏幕上显示更好的结果。

{"msg": "The task includes an option with an undefined variable. The error was: 'job1' is undefined\n\nThe error appears to have been in '/../../../../play.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Wait for asynchronous job to end\n      ^ here\n"}

不确定为什么?

© www.soinside.com 2019 - 2024. All rights reserved.