如何在运行ansible-playbook时测量和显示任务所需的时间?

22

我有一个playbook,在这个playbook中,有很多任务。我需要知道每个任务花费了多少时间。

是否有解决方案?

4个回答

38
在你的ansible.cfg文件中,在[defaults]部分添加callbacks_enabled = profile_tasks。(对于Ansible <2.11,可以使用callback_whitelist。)以下是我的ansible.cfg文件。
[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


请您能否添加一些参考资料(例如文档链接),解释回调函数的作用,介绍一些选项供 OP 进行配置,并说明它是如何实现其要求的?谢谢。 - Zeitounator
这是官方链接,介绍如何进行调整。
  • https://docs.ansible.com/ansible/latest/plugins/callback.html#enabling-callback-plugins
  • https://docs.ansible.com/ansible/latest/plugins/callback.html#plugin-list
- Pradeep Kumar H C
通常我们会将此作为答案的编辑,以便所有信息一次性可见。 - Zeitounator
@PradeepKumarHC 是的!它显示了任务启动时的时间。 “星期四,2020年5月28日11:59:26 +0530(0:00:00.033)0:00:30.978 **********”显示了上述计时格式。 你能解释一下它是如何工作的吗? 这不显示特定任务所需的时间。 - Dharti Sutariya

10

1

如果您想在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,因为它最初是作为一个事实收集的,否则不会刷新。


-1
感谢 @James_SO,我可以让 Ansible 计算我想要测量持续时间的目标任务的秒数。话虽如此,我有多个任务需要测量,所以对我来说必须为每个任务执行以下操作。
- name: force update of current timestamp
  setup: filter='ansible_date_time'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接