我有一个playbook,在这个playbook中,有很多任务。我需要知道每个任务花费了多少时间。
是否有解决方案?
我有一个playbook,在这个playbook中,有很多任务。我需要知道每个任务花费了多少时间。
是否有解决方案?
[defaults]
inventory = hosts
callbacks_enabled = profile_tasks
deprecation_warnings = False
这是我的剧本
- hosts: localhost
gather_facts: true
tasks:
- name: Sleep for 10 Sec
command: sleep 10
- name: Sleep for 5 Sec
command: sleep 5
- name: Sleep for 2 Sec
command: sleep 2
这是我的游戏输出。
PLAY [localhost] ***************************************
TASK [Gathering Facts] *********************************
Thursday 28 May 2020 09:36:04 +0000 (0:00:00.038) 0:00:00.038 **********
ok: [localhost]
TASK [Sleep for 10 Sec] ********************************
Thursday 28 May 2020 09:36:07 +0000 (0:00:03.695) 0:00:03.733 **********
changed: [localhost]
TASK [Sleep for 5 Sec] *********************************
Thursday 28 May 2020 09:36:18 +0000 (0:00:11.166) 0:00:14.899 **********
changed: [localhost]
TASK [Sleep for 2 Sec] *********************************
Thursday 28 May 2020 09:36:24 +0000 (0:00:05.965) 0:00:20.865 **********
changed: [localhost]
PLAY RECAP *********************************************
localhost : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Thursday 28 May 2020 09:36:27 +0000 (0:00:02.878) 0:00:23.744 **********
===============================================================================
Sleep for 10 Sec------------------------------------------------ 11.17s
Sleep for 5 Sec------------------------------------------------- 5.97s
Gathering Facts------------------------------------------------- 3.70s
Sleep for 2 Sec ------------------------------------------------- 2.88s
最后显示了每项任务完成所需的时间。
有关参数callbacks_enabled = profile_tasks
的说明可在官方ansible文档中找到......
https://docs.ansible.com/ansible/latest/plugins/callback.html#enabling-callback-plugins
https://docs.ansible.com/ansible/latest/plugins/callback.html#plugin-list
https://docs.ansible.com/ansible/latest/plugins/callback/profile_tasks.html
callback_whitelist
已被弃用并将在 Ansible 2.15 中移除,现代版本的 Ansible 应使用 callbacks_enabled
(自 2.11 版本引入)替代。
[defaults]
...
callbacks_enabled = ansible.posix.profile_tasks
Reference:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#callbacks-enabled
如果您想在playbook中访问该时间,可以像这样操作:
- name: Start
set_fact:
start_time: "{{ ansible_date_time.iso8601[:19] }}"
... do a bunch of stuff
- name: force update of current timestamp
setup: filter='ansible_date_time'
- name: Get runtime
set_fact:
runtime: "{{ ((ansible_date_time.iso8601[:19] | to_datetime('%Y-%m-%dT%H:%M:%S')) - (start_time | to_datetime('%Y-%m-%dT%H:%M:%S'))).seconds }}"
那个事实将作为"{{ runtime }}"
对您可访问。
您需要强制更新ansible_date_time,因为它最初是作为一个事实收集的,否则不会刷新。
- name: force update of current timestamp
setup: filter='ansible_date_time'