1

I am running a simple Ansible ad-hoc command across all hosts in inventory and I want Ansible to also show the time it took to return output from each host. I can see the time in log, but I want to have it as stdout.

I tried adding callback_whitelist = profile_tasks in ansible.cfg but that did not help.

For example, Ansible command

[demo@demoserver ~]$ ansible all -m shell -a 'hostname -f'

output shows

worker0 | CHANGED | rc=0 >>
iworker0.unix.demo.com

worker2 | CHANGED | rc=0 >>
iworker2.unix.demo.com

worker1 | CHANGED | rc=0 >>
iworker1.unix.demo.com

worker3 | CHANGED | rc=0 >>
iworker3.unix.demo.com

and log shows

2020-01-30 15:46:27,924 p=22811 u=demo |  worker0 | CHANGED | rc=0 >>
iworker0.unix.demo.com

2020-01-30 15:46:27,955 p=22811 u=demo |  worker2 | CHANGED | rc=0 >>
iworker2.unix.demo.com

2020-01-30 15:46:27,960 p=22811 u=demo |  worker1 | CHANGED | rc=0 >>
iworker1.unix.demo.com

2020-01-30 15:46:27,975 p=22811 u=demo |  worker3 | CHANGED | rc=0 >>
iworker3.unix.demo.com

So, I want the time from log to appear as stdout as well. Is that possible?

[demo@demoserver ~]$ ansible --version
ansible 2.7.12
  config file = /home/demo/ansible.cfg
  configured module search path = [u'/home/demo/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 11 2019, 14:33:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
U880D
  • 8,601
  • 6
  • 24
  • 40
zatka
  • 63
  • 6
  • I would not know a straight forward way to do it. All I can think about is using playbooks instead of ad hoc, so you can use "profile_tasks". Other option, again, not exactly what you are looking for, is to use the command time "ansible all -m shell -a 'time ; hostname -f'. Lastly, in case you have an ELK, you could use it to parse those logs and get that info...not in "stdout" but you would get it. Sorry, this is all I can think of. – carrotcakeslayer Jan 30 '20 at 17:01
  • Tried that earlier...result is a 'non-zero return code' for 'time ; hostname -f' and 'time && hostname -f'. btw, what is ELK? never used before. – zatka Jan 30 '20 at 20:54
  • Elasticsearch Logstash Kibana stack. Know is known as "elastic stack". Too complicated to implement only for that purpose. I'm curious now about that "non-zero return code". I'll try it tomorrow when I'm on my pc again :) – carrotcakeslayer Jan 30 '20 at 21:03

1 Answers1

0

In respect of your requirement

I want Ansible to also show the time it took to return output from each host

and comment

Tried that earlier ... result is a non-zero return code for time ; hostname -f and time && hostname -f

the command

ansible test --user ${ACCOUNT} --ask-pass -m shell --args 'TIMEFORMAT=%R; time hostname -f'

is just working and producing an output of

test.example.com | CHANGED | rc=0 >>
test.example.com
0.003

In respect of of your requirement

I want the time from log to appear as stdout

which doesn't show the time it took to execute the command but the datetime, you could add a timestamp after the command as follow

ansible test --user ${ACCOUNT} --ask-pass -m shell --args 'date +"%T.%3N"; hostname -f'

and which is producing an output of

test.example.com | CHANGED | rc=0 >>
12:34:56.789
test.example.com

or

echo -n `date +"%T.%3N"` " ";  hostname -f

for an output of

12:34:56.789 test.example.com

Credits To

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40