mail multi-line format

17 views
Skip to first unread message

dbs34

unread,
Jun 23, 2023, 12:40:48 PM6/23/23
to Ansible Project
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Hello!

I have tried various ways to get my output to email in a readable format, with newlines, but ansible strips them off everytime.  Its just a few shell commands i am testing.  I've tried a for loop, with items, replace, etc.  My email output is all on one-line like so:

"- ' 12:23:27 up 64 days, 1:36, 2 users, load average: 0.21, 0.08, 0.02' - USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT - 'dbsxxx pts/0 ho-it-dbsxxx-dt Tue08 19:39m 0.16s 0.30s sshd: dbsxxxx [priv]' - root pts/1 ho-lx-ansible01. 12:23 0.00s 0.10s 0.00s /usr/bin/w - Filesystem Size Used Avail Use% Mounted on - devtmpfs 11G 0 11G 0% /dev - tmpfs 11G 0 11G 0% /dev/shm - tmpfs 11G 1.1G 9.1G 11% /run - tmpfs 11G 0 11G 0% /sys/fs/cgroup - /dev/mapper/rootvg-root 46G 28G 19G 61% / - /dev/mapper/rootvg-home 10G 33M 10G 1% /home - /dev/sda1 1.1G 383M 656M 37% /boot - /dev/mapper/satvg-satlv01 503G 408G 96G 82% /var/satellite - /dev/mapper/oravg-oralv01 142G 41G 102G 29% /opt/oracle - tmpfs 2.1G 0 2.1G 0% /run/user/0 - tmpfs 2.1G 0 2.1G 0% /run/user/1922732921 "

 my yaml code is:

- name: DF + UPTIME PLAYBOOK
  hosts: "*"
  gather_facts: false

  tasks:
    - shell: |
        /usr/bin/w
        /usr/bin/df -h
      register: dfu

    - debug: var=dfu.stdout_lines

    - name: mail result
      community.general.mail:
        sender: ro...@ho-lx-ansible.xxxxxx.com
        host: internal-smtp.xxxxxx.com
        subtype: html
        to: 'dbs...@xxxxxx.com'
        subject: test
        body: <h3> "{{ dfu.stdout_lines | to_nice_yaml(indent=4) }}" </h3>
...

How do I tell ansible to output in a email format like the way I see it from the debug output on my screen?

ok: [ho-lx-sys-olm01] =>
  dfu.stdout_lines:
  - ' 12:23:27 up 64 days,  1:36,  2 users,  load average: 0.21, 0.08, 0.02'
  - USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
  - 'dbxxxx  pts/0    ho-it-dbxxx-dt Tue08   19:39m  0.16s  0.30s sshd: dbsxxx [priv]'
  - root     pts/1    ho-lx-ansible01. 12:23    0.00s  0.10s  0.00s /usr/bin/w
  - Filesystem                 Size  Used Avail Use% Mounted on
  - devtmpfs                    11G     0   11G   0% /dev
  - tmpfs                       11G     0   11G   0% /dev/shm
  - tmpfs                       11G  1.1G  9.1G  11% /run
  - tmpfs                       11G     0   11G   0% /sys/fs/cgroup
  - /dev/mapper/rootvg-root     46G   28G   19G  61% /
  - /dev/mapper/rootvg-home     10G   33M   10G   1% /home
  - /dev/sda1                  1.1G  383M  656M  37% /boot
  - /dev/mapper/satvg-satlv01  503G  408G   96G  82% /var/satellite
  - /dev/mapper/oravg-oralv01  142G   41G  102G  29% /opt/oracle
  - tmpfs                      2.1G     0  2.1G   0% /run/user/0
  - tmpfs                      2.1G     0  2.1G   0% /run/user/1922732921

thank you!

Dick Visser

unread,
Jun 23, 2023, 1:32:34 PM6/23/23
to ansible...@googlegroups.com
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
On Fri, 23 Jun 2023 at 18:40, dbs34 <derekbel...@gmail.com> wrote:
Hello!

I have tried various ways to get my output to email in a readable format, with newlines, but ansible strips them off everytime.  Its just a few shell commands i am testing.  I've tried a for loop, with items, replace, etc.  My email output is all on one-line like so:

"- ' 12:23:27 up 64 days, 1:36, 2 users, load average: 0.21, 0.08, 0.02' - USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT - 'dbsxxx pts/0 ho-it-dbsxxx-dt Tue08 19:39m 0.16s 0.30s sshd: dbsxxxx [priv]' - root pts/1 ho-lx-ansible01. 12:23 0.00s 0.10s 0.00s /usr/bin/w - Filesystem Size Used Avail Use% Mounted on - devtmpfs 11G 0 11G 0% /dev - tmpfs 11G 0 11G 0% /dev/shm - tmpfs 11G 1.1G 9.1G 11% /run - tmpfs 11G 0 11G 0% /sys/fs/cgroup - /dev/mapper/rootvg-root 46G 28G 19G 61% / - /dev/mapper/rootvg-home 10G 33M 10G 1% /home - /dev/sda1 1.1G 383M 656M 37% /boot - /dev/mapper/satvg-satlv01 503G 408G 96G 82% /var/satellite - /dev/mapper/oravg-oralv01 142G 41G 102G 29% /opt/oracle - tmpfs 2.1G 0 2.1G 0% /run/user/0 - tmpfs 2.1G 0 2.1G 0% /run/user/1922732921 "

 my yaml code is:

- name: DF + UPTIME PLAYBOOK
  hosts: "*"
  gather_facts: false

  tasks:
    - shell: |
        /usr/bin/w
        /usr/bin/df -h
      register: dfu

    - debug: var=dfu.stdout_lines

    - name: mail result
      community.general.mail:
        sender: ro...@ho-lx-ansible.xxxxxx.com
        host: internal-smtp.xxxxxx.com
        subtype: html
        to: 'dbs...@xxxxxx.com'
        subject: test
        body: <h3> "{{ dfu.stdout_lines | to_nice_yaml(indent=4) }}" </h3>
...

How do I tell ansible to output in a email format like the way I see it from the debug output on my screen?


The console is using a fixed width font, while your mail client is not.
You need to format the email body in a way that your mail client uses a fixed width font to display it. This is not something this list is for, but as a start wrap the output in <pre> tags instead of <h3>. 
Google for email formatting and html to tweak things further. 





ok: [ho-lx-sys-olm01] =>
  dfu.stdout_lines:
  - ' 12:23:27 up 64 days,  1:36,  2 users,  load average: 0.21, 0.08, 0.02'
  - USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
  - 'dbxxxx  pts/0    ho-it-dbxxx-dt Tue08   19:39m  0.16s  0.30s sshd: dbsxxx [priv]'
  - root     pts/1    ho-lx-ansible01. 12:23    0.00s  0.10s  0.00s /usr/bin/w
  - Filesystem                 Size  Used Avail Use% Mounted on
  - devtmpfs                    11G     0   11G   0% /dev
  - tmpfs                       11G     0   11G   0% /dev/shm
  - tmpfs                       11G  1.1G  9.1G  11% /run
  - tmpfs                       11G     0   11G   0% /sys/fs/cgroup
  - /dev/mapper/rootvg-root     46G   28G   19G  61% /
  - /dev/mapper/rootvg-home     10G   33M   10G   1% /home
  - /dev/sda1                  1.1G  383M  656M  37% /boot
  - /dev/mapper/satvg-satlv01  503G  408G   96G  82% /var/satellite
  - /dev/mapper/oravg-oralv01  142G   41G  102G  29% /opt/oracle
  - tmpfs                      2.1G     0  2.1G   0% /run/user/0
  - tmpfs                      2.1G     0  2.1G   0% /run/user/1922732921

thank you!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/55dbf7bc-2383-47ad-bd17-1debbd9e683an%40googlegroups.com.
--
Sent from Gmail Mobile

dbs34

unread,
Jun 23, 2023, 1:57:37 PM6/23/23
to Ansible Project
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
ok thank you Dick, I follow you.  Anyway, this is what I came up with and it worked like a charm.

---


- name: DF + UPTIME PLAYBOOK
  hosts: "*"
  gather_facts: false

  tasks:
    - shell: |
        /usr/bin/w
        /usr/bin/df -h
      register: dfu

    - debug: var=dfu.stdout_lines

    - name: Create email body
      set_fact:
        email_body: |
          <html>
            <body>
              <pre>
              {{ dfu.stdout |replace('\n', '<br>') }}
              </pre>
            </body>
          </html>


    - name: mail result
      community.general.mail:
        sender: ro...@ho-lx-ansible.xxxx.com
        host: internal-smtp.xxxxx.com
        subtype: html
        to: 'dbs...@mxxxxx.com'
        subject: test
        body: "{{ email_body }}"
...

Reply all
Reply to author
Forward
0 new messages