Generate HTML Table from Ansible Inventory File

Hello,

I have an inventory file like below:

[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

I need to generate an HTML file like below:

Inventory
HostField1Field2
server1.example.coma1a2
server2.example.coma1a2
server3.example.coma3a4
server4.example.coma4a4

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

Thank you,

Xinhuan Zheng

[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
                  
<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

  - hosts: all
    tasks:
      - template:
          src: table.html.j2
          dest: table.html
        delegate_to: localhost
        run_once: true

and the template
https://gist.github.com/vbotka/a43e33f2a2951ef434f72ebd98a9eb3e

  > cat table.html.j2
  {% for host in ansible_play_hosts %}
  {{ host }} {{ hostvars[host]['field1'] }} {{
hostvars[host]['field2'] }}
  {% endfor %}

give

  > cat table.html
  server1.example.com a1 a2
  server2.example.com a1 a2
  server3.example.com a3 a4
  server4.example.com a3 a4

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

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

  {% for host in groups['group'] %}

or, simply

  {% for host in groups.group %}

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

  {% for host in groups[group] %}