如何在Ansible中异步运行多个包含任务? [英] How to run mutiple include tasks asynchronously in ansible?
本文介绍了如何在Ansible中异步运行多个包含任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用‘INCLUDE’和‘WITH_ITEMS’遍历任务块:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
with_items: "{{ list1 }}"
,Multiple.yml为:
---
- name: time before
shell: date +"%H:%M:%S"
- name: run
shell: sleep {{ item.time }}
- name: time after
shell: date +"%H:%M:%S"
该任务运行良好。但是,每个循环的Multiple.yml都是按顺序执行的:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:19"]}
我想异步执行每个循环的Multiple.yml,所以我尝试在主文件中使用异步和轮询:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
async: 20
poll: 0
with_items: "{{ list1 }}"
预期结果为:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +"%H:%M:%S"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:08"]}
但是运行新的main.yml的结果与之前相同,并且两个循环的Multiple.yml不是异步执行的。我不知道问题出在哪里。有什么建议吗?
推荐答案
这是一个known issue,被标记为"改进",而不是错误。可以在这个问题上发表评论或+1或其他任何内容,但我认为这个问题不会在不久的将来得到解决,因为这个问题自2017年以来一直悬而未决。
另外,您可能会感兴趣的是,"内联变量"语法一直受到抨击,支持使用
vars:
,但即使在这种情况下,您也不需要这样做,因为with_items:
会自动公开item
,因此它在vars:
中是隐式的。不推荐使用"Note" in the manual
这篇关于如何在Ansible中异步运行多个包含任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文