Let's say you have the following playbook which will create the /tmp/foo.txt file.
---
- hosts: localhost
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
Running this playbook should return something like this. Notice that there is no timestamp included in the output.
~]$ ansible-playbook testing.yml
PLAY [localhost]
TASK [Gathering Facts]
ok: [localhost]
TASK [file]
changed: [localhost] => (item=touch)
PLAY RECAP
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The ansible.posix.profile_tasks callback can be used to include timestamps in the console output. The profile_tasks callback is part of the ansible.posix collection, so you typically want to first determine if you have installed the ansible.posix collection in your collections paths. If you are not familiar with collections, check out my article on Getting Started with Collections.
The collections_path or collections_paths directive in your ansible.cfg file can be used to specify the directory or directories that Ansible will use for collections.
[defaults]
collections_path = /usr/local/ansible/collections,/usr/local/ansible_testing/collections
The ansible-galaxy collection list command can be used to list the collection you have installed in your collections paths.
~]$ ansible-galaxy collection list
# /home/john.doe/.ansible/collections/ansible_collections
Collection Version
----------------- -------
amazon.aws 6.1.0
community.docker 1.9.0
community.general 4.0.2
# /usr/share/ansible/collections/ansible_collections
Collection Version
------------------------ -------
redhat.rhel_system_roles 1.0.1
Optionally, you can include the path to a specific directory that contains collections you have installed.
~]$ ansible-galaxy collection list --collections-path /path/to/collections
Collection Version
----------------- -------
amazon.aws 6.1.0
community.docker 1.9.0
community.general 4.0.2
If you do not have the ansible.posix collection installed, the ansible-galaxy collection install command can be used to install a collection. By default, this will install the collection to /home/username/.ansible/collections/ansible_collections/.
ansible-galaxy collection install ansible.posix
Now you should be able to use the ansible.posix.profile_tasks callback. There are a number of ways to configure Ansible to use the ansible.posix.profile_tasks callback, where the higher option in this list will take precedence over the lower option.
- ANSIBLE_CALLBACKS_ENABLED variable
- callbacks_enabled in ansible.cfg in the present working directory
- callbacks_enabled in .ansible.cfg in the user home directory (e.g. /home/john.doe/.ansible.cfg)
- callbacks_enabled in /etc/ansible/ansible.cf
ANSIBLE_CALLBACKS_ENABLED ​variable
Here is how you could set the profile_tasks callback using the ANSIBLE_CALLBACKS_ENABLED variable on a Linux system. Be aware that you would need to issue the export command before issuing the ansible-playbook command.
export ANSIBLE_CALLBACKS_ENABLED=ansible.posix.profile_tasks
Or, you can include this in your users hidden .bash_profile file (e.g. /home/john.doe/.bash_profile) to make this setting permanent.
~]$ cat /home/john.doe/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
export ANSIBLE_CALLBACKS_ENABLED=ansible.posix.profile_tasks
Or in a playbook using environment.
---
- hosts: localhost
environment:
ANSIBLE_CALLBACKS_ENABLED: ansible.posix.profile_tasks
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
...
callbacks_enabled directive
You can define the callbacks_enabled directive in your Ansible configuration file (ansible.cfg), where the higher option in this list will take precedence over the lower options.
- callbacks_enabled in ansible.cfg in the present working directory
- callbacks_enabled in .ansible.cfg in the user home directory (e.g. /home/john.doe/.ansible.cfg)
- callbacks_enabled in /etc/ansible/ansible.cfg
AVOID TROUBLE
The callbacks_enabled directive must be under the [defaults] header.
[defaults]
callbacks_enabled = ansible.posix.profile_tasks
And now when you run your Ansible playbook, the console output should include timestamps.
~]$ ansible-playbook testing.yml
PLAY [localhost]
TASK [Gathering Facts]
Friday 10 May 2024 04:46:10 -0500 (0:00:00.050) 0:00:00.050 ************
ok: [localhost]
TASK [file]
Friday 10 May 2024 04:46:13 -0500 (0:00:03.174) 0:00:03.225 ************
changed: [localhost] => (item=touch)
changed: [localhost] => (item=absent)
PLAY RECAP
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Friday 10 May 2024 04:46:15 -0500 (0:00:01.557) 0:00:04.782 ************
===============================================================================
Gathering Facts ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 3.17s
file ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1.56s
Notice the last 2 lines contain the time it took to gather facts and complete each task. If you don't want this output, you can set PROFILE_TASKS_TASK_OUTPUT_LIMIT=0.
export PROFILE_TASKS_TASK_OUTPUT_LIMIT=0
Did you find this article helpful?
If so, consider buying me a coffee over at