默认情况下,任务并行运行。使用ansible-runner。它将为您提供details。例如,给定一本剧本
shell> cat test-copy/test-28.yml
- hosts: test_01,test_02,test_03
tasks:
- copy:
src: /etc/passwd
dest: /tmp/test
命令
shell> ansible-runner run test-copy -i ID01 -p test-28.yml
创建细节
shell> tree test-copy/artifacts/ID01/
test-copy/artifacts/ID01/
├── command
├── fact_cache
├── job_events
│ ├── 10-c9ee2f18-9f8e-4b51-a5a5-81c7353131fb.json
│ ├── 1-45a882a8-a95d-4026-a374-fa3be0dc26bb.json
│ ├── 2-3d614ae4-866b-7e82-3e7a-000000000008.json
│ ├── 3-3d614ae4-866b-7e82-3e7a-00000000000a.json
│ ├── 4-4eb0b550-3537-4120-9fad-6ed5a7703f0d.json
│ ├── 5-e305a7d2-d3bd-409b-963e-4b214a6a129b.json
│ ├── 6-6949e8cc-458a-4626-b2cb-68a244e308af.json
│ ├── 7-e1049b61-5c27-4627-b5b0-181533cb458a.json
│ ├── 8-722940a2-1edd-45b7-8418-697e6f728000.json
│ └── 9-b5983d4c-6973-4626-93ba-462c00cbfa78.json
├── rc
├── status
└── stdout
2 directories, 14 file
查看 job_events。简化分析。安装角色 vbotka.ansible_lib
shell> ansible-galaxy role install vbotka.ansible_lib
并使用剧本中的任务al_runner_events
shell> cat test-29.yml
- hosts: localhost
vars:
al_runner_events_dir: test-copy/artifacts/ID01/job_events
tasks:
- include_role:
name: vbotka.ansible_lib
tasks_from: al_runner_events
- debug:
msg: "{{ item.counter }}
{{ item.event_data.host }}
{{ item.event_data.task }}
start: {{ item.event_data.start }}
end: {{ item.event_data.end }}"
loop: "{{ al_runner_events_list|sort(attribute='counter') }}"
loop_control:
label: "{{ item.counter }}"
when: item.event_data.duration|default(0) > 0
给予
shell> ansible-playbook test-29.yml | grep msg
msg: '7 test_03 copy start: 2020-10-30T07:19:55.541914 end: 2020-10-30T07:20:00.078211'
msg: '8 test_01 copy start: 2020-10-30T07:19:55.520058 end: 2020-10-30T07:20:00.239671'
msg: '9 test_02 copy start: 2020-10-30T07:19:55.527841 end: 2020-10-30T07:20:00.460820'
可以看到 copy 任务是并行运行的。
如果你设置serial: 1
shell> cat test-copy/test-28.yml
- hosts: test_01,test_02,test_03
serial: 1
tasks:
- copy:
src: /etc/passwd
dest: /tmp/test
复制任务将串行运行
shell> ansible-playbook test-29.yml | grep msg
msg: '5 test_01 copy start: 2020-10-30T10:12:55.830801 end: 2020-10-30T10:12:59.696363'
msg: '9 test_02 copy start: 2020-10-30T10:12:59.727570 end: 2020-10-30T10:13:02.787844'
msg: '13 test_03 copy start: 2020-10-30T10:13:02.820704 end: 2020-10-30T10:13:05.835439'