Ansible-并行运行两个任务



我的剧本如下。。。这与一个主机有关,而不是同时与几个主机有关:

----------------TG的注销

- name: DeRegister instance ID from a Public TG
elb_target:
target_group_name: "{{ TG_public }}"
target_id: "{{ Instance_ID }}"
state: absent
target_status: unused                            ## wait until this status
target_status_timeout: 300                       ## in seconds
profile: "{{ profile_ID }}"                      ## specify AWS profile 
delegate_to: 127.0.0.1                             ## Execute this stage locally
- name: DeRegister instance ID from a Private TG
elb_target:
target_group_name: "{{ TG_private }}"
target_id: "{{ Instance_ID }}"
state: absent
target_status: unused                            ## wait until this status
target_status_timeout: 300                       ## in seconds
profile: "{{ profile_ID }}"                      ## specify AWS profile
delegate_to: 127.0.0.1                             ## Execute this stage locally

----------------------------------

如何并行执行上述任务的问题。。。请帮助

使用异步并设置

poll: 0

例如,在下面的剧本中,使用了两个命令sleep(每个命令10秒(,而不是问题中的两个命令。输出显示两个任务都启动了命令并终止了。命令并行运行。第一个任务async_status等待了3次迭代(每次3秒(,直到第一个命令完成。第二个任务async_status立即完成。整个剧本持续了11秒。

- hosts: localhost
tasks:
- debug:
msg: "{{ lookup('pipe', 'date') }}"
- command: /usr/bin/sleep 10  # Public TG
async: 45
poll: 0
register: public
- command: /usr/bin/sleep 10  # Private TG
async: 45
poll: 0
register: private
- debug:
msg: "{{ lookup('pipe', 'date') }}"
- async_status:
jid: "{{ public.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 100
delay: 3
- debug:
msg: "{{ lookup('pipe', 'date') }}"
- async_status:
jid: "{{ private.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 100
delay: 3
- debug:
msg: "{{ lookup('pipe', 'date') }}"

给出(节略(

TASK [debug] ****
msg: Mon 12 Oct 2020 09:42:21 PM CEST
TASK [command] ****
TASK [command] ****
TASK [debug] ****
msg: Mon 12 Oct 2020 09:42:21 PM CEST
TASK [async_status] ****
FAILED - RETRYING: async_status (100 retries left).
FAILED - RETRYING: async_status (99 retries left).
FAILED - RETRYING: async_status (98 retries left).
TASK [debug] ****
msg: Mon 12 Oct 2020 09:42:32 PM CEST
TASK [async_status] ****
TASK [debug] ****
msg: Mon 12 Oct 2020 09:42:32 PM CEST

可以在一个循环中等待更多作业。例如

- async_status:
jid: "{{ item.ansible_job_id }}"
loop:
- "{{ public }}"
- "{{ private }}"
register: job_result
until: job_result.finished
retries: 100
delay: 3

相关内容

  • 没有找到相关文章

最新更新