# Generate HTML Table from Ansible Inventory File

**URL:** <https://forum.ansible.com/t/generate-html-table-from-ansible-inventory-file/33697>\
**Category:** Ansible Project\
**Created:** [August 28, 2020, 6:56pm UTC](https://forum.ansible.com/t/generate-html-table-from-ansible-inventory-file/33697 "2020-08-28T18:56:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [August 28, 2020, 6:56pm UTC](https://forum.ansible.com/t/generate-html-table-from-ansible-inventory-file/33697/1 "2020-08-28T18:56:09Z")

</div>

Hello,

I have an inventory file like below:

[group1]  
[server1.example.com](http://server1.example.com)  
[server2.example.com](http://server2.example.com)

[group2]  
[server3.example.com](http://server3.example.com)  
[server4.example.com](http://server4.example.com)

[group1:vars]  
field1=a1  
field2=a2

[group2:vars]  
field1=a3  
field2=a4

I need to generate an HTML file like below:

Inventory

| Host | Field1 | Field2 |
| server1.example.com | a1 | a2 |
| server2.example.com | a1 | a2 |
| server3.example.com | a3 | a4 |
| server4.example.com | a4 | a4 |

Basically all the hosts and their values becomes HTML Table. How do I use Ansible playbook to accomplish it?

Thank you,

Xinhuan Zheng

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 28, 2020, 8:06pm UTC](https://forum.ansible.com/t/generate-html-table-from-ansible-inventory-file/33697/2 "2020-08-28T20:06:37Z")

</div>

> [group1]  
> server1.example.com  
> server2.example.com
> 
> [group2]  
> server3.example.com  
> server4.example.com
> 
> [group1:vars]  
> field1=a1  
> field2=a2
> 
> [group2:vars]  
> field1=a3  
> field2=a4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> \<tr\>\<td\>server1.example.com\</td\>\<td\>a1\</td\>\<td\>a2\</td\>\</tr\>  
> \<tr\>\<td\>server2.example.com\</td\>\<td\>a1\</td\>\<td\>a2\</td\>\</tr\>  
> \<tr\>\<td\>server3.example.com\</td\>\<td\>a3\</td\>\<td\>a4\</td\>\</tr\>  
> \<tr\>\<td\>server4.example.com\</td\>\<td\>a4\</td\>\<td\>a4\</td\>\</tr\>

The playbook

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: table.html.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: table.html  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

and the template  
[https://gist.github.com/vbotka/a43e33f2a2951ef434f72ebd98a9eb3e](https://gist.github.com/vbotka/a43e33f2a2951ef434f72ebd98a9eb3e)

&nbsp;&nbsp;\> cat table.html.j2  
&nbsp;&nbsp;{% for host in ansible\_play\_hosts %}  
&nbsp;&nbsp;{{ host }} {{ hostvars[host]['field1'] }} {{  
hostvars[host]['field2'] }}  
&nbsp;&nbsp;{% endfor %}

give

&nbsp;&nbsp;\> cat table.html  
&nbsp;&nbsp;server1.example.com a1 a2  
&nbsp;&nbsp;server2.example.com a1 a2  
&nbsp;&nbsp;server3.example.com a3 a4  
&nbsp;&nbsp;server4.example.com a3 a4

Add the html code to the template. See "Jinja"  
[https://jinja.palletsprojects.com/en/2.11.x/en/](https://jinja.palletsprojects.com/en/2.11.x/en/)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 2, 2020, 3:45pm UTC](https://forum.ansible.com/t/generate-html-table-from-ansible-inventory-file/33697/3 "2020-09-02T15:45:31Z")

</div>

If "group" is the name of the group then use

&nbsp;&nbsp;{% for host in groups['group'] %}

or, simply

&nbsp;&nbsp;{% for host in groups.group %}

If "group" is the variable with the name of the group use

&nbsp;&nbsp;{% for host in groups[group] %}
