Ansible output from csv file to html table

135 views
Skip to first unread message

Saravanan Ponnusamy

unread,
Aug 26, 2018, 11:27:30 PM8/26/18
to Ansible Project
All,
Ansible output of a playbook is csv file format. This needs to be converted to html table and send as mail body output within Ansible playbook. 
Can someone share the task detail to convert csv file to html table.

Saravanan

unread,
Aug 27, 2018, 7:40:48 AM8/27/18
to Ansible Project
All,
In the below Ansible email task, instead of attaching the csv file to the email, need to send the contend of csv file in the body of the email as html table. 

   - name: Send the SSH status to email
      local_action
: mail
                    host
= '127.0.0.1'
                    port
=25
                    subject
="SSH Connection status for Unix sudo accounts"
                    body
="SSH Failure Service accounts"
                    attach
="/tmp/content.csv"
                   
from="ad...@localhost.com"
                    to
="someone@email.com"
                    subtype
="html"
                    charset
=utf8
      delegate_to
: 127.0.0.1
      run_once
: true

Kai Stian Olstad

unread,
Aug 27, 2018, 10:56:35 AM8/27/18
to ansible...@googlegroups.com
On Monday, 27 August 2018 13.40.48 CEST Saravanan wrote:
> All,
> In the below Ansible email task, instead of attaching the csv file to the
> email, need to send the contend of csv file in the body of the email as
> html table.

I'm not sure what you are trying too achieve but you should probably check at ARA https://github.com/openstack/ara


>
> - name: Send the SSH status to email
> local_action: mail
> host= '127.0.0.1'
> port=25
> subject="SSH Connection status for Unix sudo accounts"
> body="SSH Failure Service accounts"
> attach="/tmp/content.csv"
> from="ad...@localhost.com"
> to="som...@email.com <Saravanan...@email.com>"
> subtype="html"
> charset=utf8
> delegate_to: 127.0.0.1
> run_once: true

What you are trying to do is possible but you need to go through a lot of hoops, choosing csv is making this harder that is could be, yaml or json would have been easier.

As is, you would need to create the csv file to a list of list, then use template to create the html table.


--
Kai Stian Olstad


Saravanan

unread,
Sep 4, 2018, 12:41:49 PM9/4/18
to Ansible Project
Kai,
Here is my actual playbook. This gives me the output in plain csv file format. To represent the output via email, please suggest the right approach.

---
- name: Failure user accounts
  gather_facts: no
  hosts: 127.0.0.1

  tasks:
    - name: Create csv file
      file:
        dest: /tmp/file.csv
        state: touch
      delegate_to: 127.0.0.1

    - include_vars: vars.yaml

    - include_tasks: subtask.yaml
      with_items: '{{ users }}'
      
    - name: Send the Failure user accounts on email
      local_action: mail
                    host= '127.0.0.1'
                    port=25
                    subject="Failure user accounts"
                    body="{{ lookup('file', '/tmp/file.csv') }}"
                    attach="file.csv"
                    from="ad...@localhost.com"
                    to="som...@email.com"
                    subtype="html"
                    charset=utf8
      delegate_to: 127.0.0.1
      run_once: true

    - Clear content file
      file:
        dest: /tmp/file.csv
        state: absent
      delegate_to: 127.0.0.1


Reply all
Reply to author
Forward
0 new messages