# capturing logs from ansible/ansible-playbook runs

**URL:** <https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064>\
**Category:** Ansible Project\
**Created:** [July 4, 2013, 6:22am UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064 "2013-07-04T06:22:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [July 4, 2013, 6:22am UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/1 "2013-07-04T06:22:28Z")

</div>

I wanted something like sfromm's ansible-report system but for a  
variety of reasons I was concerned about the single sqlite db.

So I started hacking up a solution for lots of simultaneous runs of  
ansible and capturing logs of the whole thing.

this is a callback plugin:

[http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/callback\_plugins/logdetail.py](http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/callback_plugins/logdetail.py)

and this is a viewer for those logs:  
[http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/scripts/logview](http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/scripts/logview)

If you put the callback\_plugin in a path for callback plugins specified  
in your /etc/ansible/ansible.cfg then it will also capture data on  
tasks run via ansible - not just ansible-playbook.

This is an example of the output from running

logview -d yesterday -p mirrorlist

mirrorlist  
20.50.54 host1 Jul 03 2013 20:54:04 54 CHANGED /etc/nagios/nrpe.cfg  
20.50.54 host1 Jul 03 2013 20:55:29 75 CHANGED restart nrpe  
20.50.54 host2 Jul 03 2013 20:54:06 54 CHANGED /etc/nagios/nrpe.cfg  
20.50.54 host2 Jul 03 2013 20:55:31 75 CHANGED restart nrpe  
20.50.54 host3 Jul 03 2013 20:54:05 54 CHANGED /etc/nagios/nrpe.cfg  
20.50.54 host3 Jul 03 2013 20:55:29 75 CHANGED restart nrpe

I thought I'd post it here in case it is helpful to anyone.  
-sv

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 4, 2013, 8:35am UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/2 "2013-07-04T08:35:16Z")

</div>

Seth,

> So I started hacking up a solution for lots of simultaneous runs of  
> ansible and capturing logs of the whole thing.

that's lovely! I'd like to propose the following patch, which avoids a  
traceback when the process doesn't have a controlling tty (happens here,  
dunno why).

Regards,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

> **(attachments)**
>
> [logdetail.patch](https://forum.ansible.com/uploads/short-url/oakyouNPRIViBI8SDzN22eXvndD.patch) (1.65 KB)

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [July 4, 2013, 5:43pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/3 "2013-07-04T17:43:15Z")

</div>

You are absolutely correct. Thank you for that. I had not tested it  
outside of a controlling TTY.

-sv

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [July 4, 2013, 6:01pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/4 "2013-07-04T18:01:04Z")

</div>

I found one bug - if I sudo -i to root - geteuid() doesn't return who I  
\_was\_ - only 'root' which is unfortunate - I'll have to see if I can  
get a best possible situation which uses getlogin() if possible

What's the exception if you don't have a controlling tty with  
os.getlogin() - I can just catch it and use geteuid()

-sv

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 4, 2013, 6:20pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/5 "2013-07-04T18:20:02Z")

</div>

> I found one bug - if I sudo -i to root - geteuid() doesn't return who I  
> \_was\_ - only 'root' which is unfortunate - I'll have to see if I can  
> get a best possible situation which uses getlogin() if possible

maybe  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.getenv('HOME', os.getenv('LOGNAME', 'unknown'))

(Sorry, I'm away from $cust at the moment: can't check on the exception  
I got.)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 6:25pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/6 "2013-07-04T18:25:43Z")

</div>

BTW, if folks are interested in some really nice views into logging, I’d recommend signing up for the AWX webinar here:

[http://ansibleworks.enterthemeeting.com/m/DBVI6YDJ](http://ansibleworks.enterthemeeting.com/m/DBVI6YDJ)

This is coming out August 8th, hopefully about a week after the AWX release. You’ll be able to try it out free for a small number of nodes.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 6:26pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/7 "2013-07-04T18:26:19Z")

</div>

Slight correction, webinar is August 8th – logging is out as part of the release 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 6:30pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/8 "2013-07-04T18:30:46Z")

</div>

Am I terrible with words today or what 🙂

“out” means “is part of the release” 🙂

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [July 4, 2013, 7:27pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/9 "2013-07-04T19:27:38Z")

</div>

I ended up doing

def getlogin():  
&nbsp;&nbsp;&nbsp;&nbsp;try:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user = os.getlogin()  
&nbsp;&nbsp;&nbsp;&nbsp;except OSError, e:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user = pwd.getpwuid(os.geteuid())[0]  
&nbsp;&nbsp;&nbsp;&nbsp;return user

which seems to step around it just fine

-sv

---

<div class="post-metadata">

**Author:** ![Stephen\_Fromm](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Stephen\_Fromm](https://forum.ansible.com/u/Stephen_Fromm)\
**Post date:** [July 4, 2013, 8:11pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/10 "2013-07-04T20:11:02Z")

</div>

> I wanted something like sfromm's ansible-report system but for a  
> variety of reasons I was concerned about the single sqlite db.

One comment on the above. I plan to devote some time in the coming weeks  
to work on the concurrency problem when using a sqlite database. One can  
still choose to use another database that doesn't have the limitations of  
sqlite -- whatever sqlalchemy supports should work with ansible-report. Of  
course, that does introduce dependencies that sqlite doesn't have. 🙂

> So I started hacking up a solution for lots of simultaneous runs of  
> ansible and capturing logs of the whole thing.
> 
> this is a callback plugin:
> 
> [http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/callback\_plugins/logdetail.py](http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/callback_plugins/logdetail.py)
> 
> and this is a viewer for those logs:
> 
> [http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/scripts/logview](http://infrastructure.fedoraproject.org/cgit/ansible.git/tree/scripts/logview)
> 
> If you put the callback\_plugin in a path for callback plugins specified  
> in your /etc/ansible/ansible.cfg then it will also capture data on  
> tasks run via ansible - not just ansible-playbook.
> 
> This is an example of the output from running
> 
> logview -d yesterday -p mirrorlist
> 
> mirrorlist  
> 20.50.54 host1 Jul 03 2013 20:54:04 54 CHANGED /etc/nagios/nrpe.cfg  
> 20.50.54 host1 Jul 03 2013 20:55:29 75 CHANGED restart nrpe  
> 20.50.54 host2 Jul 03 2013 20:54:06 54 CHANGED /etc/nagios/nrpe.cfg  
> 20.50.54 host2 Jul 03 2013 20:55:31 75 CHANGED restart nrpe  
> 20.50.54 host3 Jul 03 2013 20:54:05 54 CHANGED /etc/nagios/nrpe.cfg  
> 20.50.54 host3 Jul 03 2013 20:55:29 75 CHANGED restart nrpe
> 
> I thought I'd post it here in case it is helpful to anyone.

Looking at logdetail.py, how does it handle the case where you have a  
playbook with the same name, but in different paths?

I like the minimal dependencies of this approach and that it is easy to  
prune with something like logrotate.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [July 5, 2013, 5:24am UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/11 "2013-07-05T05:24:25Z")

</div>

Seth,

> What's the exception if you don't have a controlling tty with  
> os.getlogin() - I can just catch it and use geteuid()

The exception is  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OSError: [Errno 2] No such file or directory

I remember now, that the message stumped me. 😉 But you've fixed it all,  
so ignore.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [July 5, 2013, 2:34pm UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/12 "2013-07-05T14:34:01Z")

</div>

> One comment on the above. I plan to devote some time in the coming  
> weeks to work on the concurrency problem when using a sqlite  
> database. One can still choose to use another database that doesn't  
> have the limitations of sqlite -- whatever sqlalchemy supports should  
> work with ansible-report. Of course, that does introduce  
> dependencies that sqlite doesn't have. 🙂

cool!

> \> logview -d yesterday -p mirrorlist  
> \>  
> \> mirrorlist  
> \> 20.50.54 host1 Jul 03 2013 20:54:04 54 CHANGED /etc/nagios/nrpe.cfg  
> \> 20.50.54 host1 Jul 03 2013 20:55:29 75 CHANGED restart nrpe  
> \> 20.50.54 host2 Jul 03 2013 20:54:06 54 CHANGED /etc/nagios/nrpe.cfg  
> \> 20.50.54 host2 Jul 03 2013 20:55:31 75 CHANGED restart nrpe  
> \> 20.50.54 host3 Jul 03 2013 20:54:05 54 CHANGED /etc/nagios/nrpe.cfg  
> \> 20.50.54 host3 Jul 03 2013 20:55:29 75 CHANGED restart nrpe  
> \>  
> \>  
> \> I thought I'd post it here in case it is helpful to anyone.  
> \>
> 
> Looking at logdetail.py, how does it handle the case where you have a  
> playbook with the same name, but in different paths?

It doesn't. I realized that when I wrote it and just kinda said "meh".

If anyone would like to fix it I'm happy to accept it - but in our  
usecase the likelihood of having such a situation is extremely low.

> I like the minimal dependencies of this approach and that it is easy  
> to prune with something like logrotate.

I like that your sqlite db is a million times easier to search 🙂

-sv

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 6, 2013, 1:09am UTC](https://forum.ansible.com/t/capturing-logs-from-ansible-ansible-playbook-runs/13064/13 "2013-07-06T01:09:49Z")

</div>

idk, i find grep and awk much easier to use than sql when looking at events.
