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

fly*_*ish 5 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 }}"
Run Code Online (Sandbox Code Playgroud)

multiple.yml 是:

---
- name: time before
  shell: date +"%H:%M:%S"

- name: run 
  shell: sleep {{ item.time }}

- name: time after
  shell: date +"%H:%M:%S"
Run Code Online (Sandbox Code Playgroud)

任务进行得很好。但是,每个循环的 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"]}
Run Code Online (Sandbox Code Playgroud)

我想异步执行每个循环的 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 }}"
Run Code Online (Sandbox Code Playgroud)

预期结果是:

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"]}
Run Code Online (Sandbox Code Playgroud)

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

mda*_*iel 4

这是一个已知问题,被标记为“改进”,而不是错误。请随意对这个问题发表评论或 +1 或其他任何内容,但我不认为它会在不久的将来得到解决,因为该问题自 2017 年以来一直处于开放状态。


另外,您可能有兴趣知道“内联变量”语法已被弃用,转而使用 using vars:,但即使在这种情况下,您也不需要这样做,因为它with_items:会自动公开item,因此它是隐式的vars:。弃用位于手册的“注释”中