gpt4 book ai didi

ansible - 如何在ansible中异步运行多个包含任务?

转载 作者:行者123 更新时间:2023-12-04 15:52:05 55 4
gpt4 key购买 nike

我使用 '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,所以我尝试在主文件中使用 async 和 poll:

---
- 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"]}

但是运行new main.yml的结果和之前一样,两个循环的multiple.yml不是异步执行的。我不知道是什么问题。有什么建议吗?

最佳答案

那是 known issue并被标记为“改进”,而不是错误。随意在问题中发表评论或 +1 或其他任何内容,但我不希望它在不久的将来得到解决,因为该问题自 2017 年以来一直存在。

另外,您可能会感兴趣地知道“内联变量”语法已被弃用而支持使用 vars: ,但即使在这种情况下您也不需要,因为 with_items:自动暴露 item因此它隐含在 vars: 中为你。弃用在 "Note" in the manual

关于ansible - 如何在ansible中异步运行多个包含任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465825/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com