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

时间:2018-11-25 08:28:01

标签: 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的结果与以前相同,并且两个循环的multi.yml不是异步执行的。我不知道问题是什么。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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


另外,您可能会想知道不推荐使用“内联变量”语法,而是推荐使用vars:,但是即使在那种情况下,您也不需要这样做,因为with_items:会自动公开item,因此它暗含在vars:中。弃用在"Note" in the manual