Add new line “\n” character to Ansible set_fact variable

How can I add new line characters to Ansible variable mailbody. This mailbody variable is used for mail module's body attribute.

I tried the below from suggestions but none of them works.

1. 
  - set_fact:
       mailbody: "{{ mailbody | default('') + 'PROFILE_NAME:' +  PROFILE_NAME ~ '\n\nSERVER_NAME:' + SERVER_NAME ~ '\n\nNODE_NAME:' +  NODE_NAME ~ '\n\n\n\n' }}"
2.
      mailbody: "{{ mailbody | default('') + 'PROFILE_NAME:' +  PROFILE_NAME ~}} {{ 'SERVER_NAME:' + SERVER_NAME ~ }}\n\n{{'NODE_NAME:' +  NODE_NAME ~ }}\n\n\n\n"

I won’t comment on whatever it is you’re trying to do, but in any case you need to use double quotes around the newlines

@Dick VIsser Hi,

Upon your suggestion, I tried something like this but it too does not work.

`

  • set_fact:

mailbody: “{{ mailbody | default(‘’) + ‘PROFILE_NAME:’ + PROFILE_NAME ~ "\n\n"‘SERVER_NAME:’ + SERVER_NAME ~ "\n\n"‘NODE_NAME:’ + NODE_NAME ~ "\n\n\n\n" }}”
`

Can someone please suggest?

I tried all of these but have no clue to a solution.

mailbody: "{{ mailbody | default(‘’) + ‘PROFILE_NAME:’ + PROFILE_NAME + “\n” + ‘SERVER_NAME:’ + SERVER_NAME + “\n” + ‘NODE_NAME:’ + NODE_NAME + “\n\n\n\n" }}”

mailbody: “{{ mailbody | default(‘’) + ‘PROFILE_NAME:’ + PROFILE_NAME + ‘\n’ + ‘SERVER_NAME:’ + SERVER_NAME + ‘\n’ + ‘NODE_NAME:’ + NODE_NAME + '\n\n\n\n' }}”

mailbody: "{{ mailbody | default(‘’) + ‘PROFILE_NAME:’ + PROFILE_NAME ~ }} \n + {{ ‘SERVER_NAME:’ + SERVER_NAME ~ }} + \n + {{ ‘NODE_NAME:’ + NODE_NAME ~ }} \n\n\n\n"

I tried all of these but have no clue to a solution.

mailbody: "{{ mailbody | default('') + 'PROFILE_NAME:' + PROFILE_NAME + "\n" + 'SERVER_NAME:' + SERVER_NAME + "\n" +
'NODE_NAME:' + NODE_NAME + "\n\n\n\n\" }}"

mailbody: "{{ mailbody | default('') + 'PROFILE_NAME:' + PROFILE_NAME + '\n' + 'SERVER_NAME:' + SERVER_NAME + '\n' +
'NODE_NAME:' + NODE_NAME + '\n\n\n\n\' }}"

mailbody: "{{ mailbody | default('') + 'PROFILE_NAME:' + PROFILE_NAME ~ }} \n + {{ 'SERVER_NAME:' + SERVER_NAME ~ }} +
\n + {{ 'NODE_NAME:' + NODE_NAME ~ }} \n\n\n\n\"

Use multiline YAML: https://yaml-multiline.info/

Regards
        Racke

Stephan Hi,

The article does not discuss newline inside parenthesis {{ inside a variable definition. That is where I have no clue.

Stephan Hi,

The article does not discuss newline inside parenthesis `{{` inside a variable definition. That is where I have no clue.

Hello Shifa,

parenthesis are fine inside multiline YAML.

Regards
         Racke

I tried multiline too as below, but that too fails

mailbody: |
“{{ mailbody | default(‘’) + 'PROFILE_NAME: ’ + PROFILE_NAME }}” + “\n”
“{{ 'SERVER_NAME: ’ + SERVER_NAME }}”

Output:
[‘“PROFILE_NAME: SPROD01” + “[file://\n”\n]\n"\n"SERVER_NAME: SPROD01SRV01"\n’, ‘“PROFILE_NAME: SPROD02” + “[file://\n”\n]\n"\n"SERVER_NAME: SPROD02SRV02"\n’]

Also tried:

mailbody: |
“{{ mailbody | default(‘’) + 'PROFILE_NAME: ’ + PROFILE_NAME }}”
“{{ 'SERVER_NAME: ’ + SERVER_NAME }}”

and

mailbody: |
“{{ mailbody | default(‘’) + 'PROFILE_NAME: ’ + PROFILE_NAME }}” + “\n”
“{{ 'SERVER_NAME: ’ + SERVER_NAME }}”

I tried multiline too as below, but that too fails

   mailbody: |
     "\{\{ mailbody | default\(''\) \+ 'PROFILE\_NAME: ' \+ PROFILE\_NAME \}\}" \+ "\\n"
     "\{\{ 'SERVER\_NAME: ' \+ SERVER\_NAME \}\}"

Please try:

mailbody: |
  {{ mailbody | default('') }}PROFILE_NAME: {{ PROFILE_NAME }}
  SERVER_NAME: + {{ SERVER_NAME }}

Regards
         Racke

I see the problem is different than the direction we are debugging.

When i write the variable to a file i see new lines but when the variable assigned to email body it does not interpolate the new line instead prints ‘\n’

`

  • copy
    content: “{{ mailbody }}”
    dest: /tmp/write.log
    `

`

  • name: Send mail
    mail:
    host: localhost:
    port: 25
    body: “{{ mailbody }}”
    `

Can you please suggest?

I see the problem is different than the direction we are debugging.

When i write the variable to a file i see new lines but when the variable assigned to email body it does not interpolate the new line instead prints '\n'

       - copy
            content: "{{ mailbody }}"
            dest: /tmp/write.log

    - name: Send mail
      mail:
          host: localhost:

There is an extra ':' here?

          port: 25
          subject: :Hi"

There is an extra ':' here?

          body: "{{ mailbody }}"
          from: me@myshop.com
          to: you@yourmail.com

Can you please suggest?

You already put two errors in your task which prevent it from running correctly.
So in that light, i'm wondering if your actual task does, in fact,
contain more differences than what you've posted here.

Also, "prints '\n'" indicates that you're echoing? and not mailing?

@Dick Hi,

That was a typo the problem remains writing to a file is all good with newlines while writing to email body does not translate “\n” as new line !! Could this be a bug ?

Dick Hi,

That was a typo; however, the problem remains!! Writing to a file is all good with newline while writing to email body does not translate “\n” as new line !! Could this be a bug? Kindly suggest.

Just tried it out and it works fine here.
Can you show again the complete playbook? Including the task where the
body is generated?
That must be the issue I think.