0

I've got a awk output

   - name: read files
   shell: awk -F "|" 'FNR==NR {lines[$1]=$2; next} $9 in lines {print lines[$9], "on", $2}' file1 file2
   register: resultawk

   - debug:
        msg: "{{ resultawk.stdout.split('\n') }}"

this prints the output nicely with the line breaks

TASK [debug] ****************************************************************************************
ok: [localhost] => {
    "msg": [
        "field2file1 on field2file2",
        "field2file1 on field2file2"
    ]
}

now, when i try sending the same message over mail

 - name: send mail
   mail:
      subject: Report for {{ ansible_hostname }}
      body:
         msg: "{{ resultawk.stdout.split('\n') }}"
      to:
      - John Doe <xyz@xyz.com>

i get the following message

TASK [send mail] *********************************************************************
[WARNING]: The value {'msg': ['field2file1 on field2file2', 'field2file1 on field2file2']}" (type string).
If this does not look like what you expect, quote the entire value to ensure it does not change.
ok: [localhost]

is there anyway to send the result over mail with the proper line breaks?

javierccs
  • 1
  • 1
  • 3
  • See [Jinja2](https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html). – Vladimir Botka May 25 '20 at 20:21
  • Can we see the output of debug with `msg: "{{ resultawk.stdout }}"`? – Jack May 25 '20 at 20:25
  • TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "msg": "field2file1 on field2file2'\n'field2file1 on field2file2" } that's why I added the split('\n') – javierccs May 25 '20 at 21:09
  • If you don't do the `.split('\n')`, what happens in the email? – Jack May 25 '20 at 22:09
  • I find it odd that you have single quotes around the `\n` in your last comment, but they do not appear in the elements after the `.split('\n')`. Anyway, I cannot reproduce your AWK, but when I use `set_fact: body="field2file1 on field2file2\nfield2file1 on field2file2"`, and have `body: "{{ body }}"` in the `email` task, everything looks fine on the receiving end. I get two lines. – Jack May 25 '20 at 22:28
  • Try putting the quotes BEFORE `msg:` : `body: "msg: {{ resultawk.stdout.split('\n') }}"` – Jack May 26 '20 at 01:12

2 Answers2

1

The warning actually says what needs to be done

"quote the entire value to ensure it does not change"

Try this


 - name: send mail
   mail:
     subject: "Report for {{ ansible_hostname }}"
     body: |
       '{% for line in resultawk.stdout_lines %}
        {{ line }}
        {% endfor %}'
     to: John Doe <xyz@xyz.com>
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • pretty much the same result [WARNING]: The value ['field2file1 on field2file2', 'field2file1 on field2file2']}" (type string). If this does not look like what you expect, quote the entire value to ensure it does not change. – javierccs May 25 '20 at 21:15
  • I've updated the answer and tested it. This one should work. – Vladimir Botka May 25 '20 at 21:34
0

had a look at the jinja2 docs (thanks Vladimir) and found this

body: "{{ resultawk.stdout.split('\n'',')| to_yaml }}"

which defines the mail in an acceptable yaml format

['field2file1 on field2file2 
   field2file1 on field2file2']
javierccs
  • 1
  • 1
  • 3