Suppose you are running a ansible playbook with multiple tasks and want to print execution time of each task, then do the below changes.

Lets see the below sample playbook execution . In this ouput we dont know how much it took for each task execution.

[root@ans_scripts]#  ansible-playbook playbook_copyfile_multi.yml -i /etc/ansible/inventory.txt
 
PLAY [copy files to test servers] ***********************************************************************************************************************************************
 
TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [db_server2]
ok: [db_server1]
 
TASK [Ansible copy files to remote machine] *************************************************************************************************************************************
changed: [db_server1] => (item=test1.log)
changed: [db_server2] => (item=test1.log)
ok: [db_server1] => (item=test2.log)
ok: [db_server2] => (item=test2.log)
 
PLAY RECAP **********************************************************************************************************************************************************************
db_server1                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
db_server2                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

For enabling the changes, we need to set the callback_whitelist = profile_tasks inside ansible.cfg file.

 

[root@ansible]# cat /etc/ansible/ansible.cfg |grep callback_whitelist
callback_whitelist = profile_tasks

 

Now run the same playbook in ansible again.

 

[root@ans_scripts]#  ansible-playbook playbook_copyfile_multi.yml -i /etc/ansible/inventory.txt
 
PLAY [copy files to test servers] ***********************************************************************************************************************************************
 
TASK [Gathering Facts] **********************************************************************************************************************************************************
Sunday 31 May 2020  08:27:31 +0300 (0:00:00.047)       0:00:00.047 ************
ok: [db_server1]
ok: [db_server2]
 
TASK [Ansible copy files to remote machine] *************************************************************************************************************************************
Sunday 31 May 2020  08:27:35 +0300 (0:00:04.027)       0:00:04.074 ************
ok: [db_server1] => (item=test1.log)
ok: [db_server2] => (item=test1.log)
ok: [db_server1] => (item=test2.log)
ok: [db_server2] => (item=test2.log)
 
PLAY RECAP **********************************************************************************************************************************************************************
db_server1                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
db_server2                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
 
Sunday 31 May 2020  08:27:38 +0300 (0:00:03.103)       0:00:07.178 ************
===============================================================================
Gathering Facts ---------------------------------------------------------------------------------------------------------------------------------------------------------- 4.03s
Ansible copy files to remote machine ------------------------------------------------------------------------------------------------------------------------------------- 3.10s