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

How to measure and display time taken for tasks when running ansible-playbook?

我有一本剧本,在这本剧本中,有很多任务。我需要知道哪个任务花费了多少时间?

有什么解决办法吗?

在 ansible.cfg

[default] 部分添加 callback_whitelist = profile_tasks

这是我的 ansible.cfg

[defaults]
inventory = hosts
callback_whitelist = 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

这里终于显示了每个任务完成游戏所花费的时间。

参数callback_whitelist = 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

如果你想在剧本中访问那个时间,你可以这样做:

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

callback_whitelist 已弃用,将在 Ansible 2.15 中删除),callbacks_enabled(在 2.11 中引入)应改为在现代版本的 Ansible 中使用

[defaults]
...
callbacks_enabled = ansible.posix.profile_tasks

参考:

https://docs.ansible.com/ansible/latest/reference_appendices/config.html#callbacks-enabled

感谢@James_SO,我能够让 Ansible 计算我想要测量持续时间的目标任务的秒数。也就是说,我有多个,所以我的问题是对我想测量持续时间的每个任务执行以下操作。

- name: force update of current timestamp
  setup: filter='ansible_date_time'