Skip to content

Commit

Permalink
CSV template added.
Browse files Browse the repository at this point in the history
  • Loading branch information
fboender committed Mar 4, 2016
1 parent a920c1b commit 80b1bc2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ About
Ansible-cmdb takes the output of Ansible's fact gathering and converts it into
a static HTML overview page containing system configuration information.

It supports multiple templates and extending information gathered by Ansible
with custom data.
It supports multiple templates (html, txt_table, csv, json output) and
extending information gathered by Ansible with custom data.

![](https://raw.githubusercontent.com/fboender/ansible-cmdb/master/contrib/screenshot-overview.png)

Expand Down Expand Up @@ -163,6 +163,8 @@ Ansible-cmdb currently provides the following templates out of the box:
gathered information. This includes all the extra information scanned by
ansible-cmdb such as groups, variables, custom information, etc.

* `csv`: The CSV template outputs a CSV file of your hosts.

You can create your own template or extend an existing one by copying it and
refering to the full path to the template when using the `-t` option:

Expand Down
46 changes: 46 additions & 0 deletions src/ansiblecmdb/data/tpl/csv.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<%

import sys
import csv

col_space = 2

cols = [
{"title": "Name", "id": "name", "visible": True, "field": lambda h: h.get('name', '')},
{"title": "OS", "id": "os", "visible": True, "field": lambda h: h['ansible_facts'].get('ansible_distribution', '') + ' ' + h['ansible_facts'].get('ansible_distribution_version', '')},
{"title": "IP", "id": "ip", "visible": True, "field": lambda h: host['ansible_facts'].get('ansible_default_ipv4', {}).get('address', '')},
{"title": "Arch", "id": "arch", "visible": True, "field": lambda h: host['ansible_facts'].get('ansible_architecture', 'Unk') + '/' + host['ansible_facts'].get('ansible_userspace_architecture', 'Unk')},
{"title": "Mem", "id": "mem", "visible": True, "field": lambda h: '%0.0fg' % (int(host['ansible_facts'].get('ansible_memtotal_mb', 0)) / 1000.0)},
{"title": "MemFree", "id": "memfree", "visible": True, "field": lambda h: '%0.0fg' % (int(host['ansible_facts'].get('ansible_memfree_mb', 0)) / 1000.0)},
{"title": "MemUsed", "id": "memused", "visible": True, "field": lambda h: '%0.0fg' % (int(host['ansible_facts'].get('ansible_memory_mb', {}).get('real', {}).get('used',0)) / 1000.0)},
{"title": "CPUs", "id": "cpus", "visible": True, "field": lambda h: str(host['ansible_facts'].get('ansible_processor_count', 0))},
{"title": "Virt", "id": "virt", "visible": True, "field": lambda h: host['ansible_facts'].get('ansible_virtualization_type', 'Unk') + '/' + host['ansible_facts'].get('ansible_virtualization_role', 'Unk')},
{"title": "Disk avail", "id": "disk_avail", "visible": True, "field": lambda h: ', '.join(['{0:0.1f}g'.format(i['size_available']/1048576000) for i in host['ansible_facts'].get('ansible_mounts', []) if 'size_available' in i and i['size_available'] > 1])},
]

# Enable columns specified with '--columns'
if columns is not None:
for col in cols:
if col["id"] in columns:
col["visible"] = True
else:
col["visible"] = False

def get_cols():
return [col for col in cols if col['visible'] is True]

fieldnames = []
for col in get_cols():
fieldnames.append(col['title'])

writer = csv.writer(sys.stdout, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(fieldnames)
for hostname, host in hosts.items():
if 'ansible_facts' not in host:
sys.stdout.write('{0}: No info collected'.format(hostname))
else:
out_cols = []
for col in get_cols():
out_cols.append(col['field'](host))
writer.writerow(out_cols)
%>

2 comments on commit 80b1bc2

@kmf
Copy link

@kmf kmf commented on 80b1bc2 Mar 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@fboender
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's great! :-D I use it for reporting and such.

Please sign in to comment.