# Add new line “\\n” character to Ansible set\_fact variable

**URL:** <https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084>\
**Category:** Ansible Project\
**Created:** [June 3, 2020, 6:12pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084 "2020-06-03T18:12:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 3, 2020, 6:12pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/1 "2020-06-03T18:12:25Z")

</div>

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.

```auto
1. 

```

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

```

```auto
2.

```

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

```

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 3, 2020, 6:18pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/2 "2020-06-03T18:18:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 3, 2020, 8:57pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/3 "2020-06-03T20:57:48Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 4:41am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/4 "2020-06-04T04:41:30Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 4, 2020, 5:01am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/5 "2020-06-04T05:01:04Z")

</div>

> 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/](https://yaml-multiline.info/)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 5:21am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/6 "2020-06-04T05:21:56Z")

</div>

Stephan Hi,

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 4, 2020, 6:07am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/7 "2020-06-04T06:07:37Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 6:54am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/8 "2020-06-04T06:54:05Z")

</div>

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 }}”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 4, 2020, 7:56am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/9 "2020-06-04T07:56:23Z")

</div>

> I tried multiline too as below, but that too fails
> 
> ```
> mailbody: |
> &quot;\{\{ mailbody | default\(&#39;&#39;\) \+ &#39;PROFILE\_NAME: &#39; \+ PROFILE\_NAME \}\}&quot; \+ &quot;\\n&quot;
> &quot;\{\{ &#39;SERVER\_NAME: &#39; \+ SERVER\_NAME \}\}&quot;
> 
> ```

Please try:

mailbody: |  
&nbsp;&nbsp;{{ mailbody | default('') }}PROFILE\_NAME: {{ PROFILE\_NAME }}  
&nbsp;&nbsp;SERVER\_NAME: + {{ SERVER\_NAME }}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 8:50am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/10 "2020-06-04T08:50:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 4, 2020, 10:06am UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/11 "2020-06-04T10:06:56Z")

</div>

> 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'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- copy  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ mailbody }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/write.log
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Send mail  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mail:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: localhost:

There is an extra ':' here?

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 25  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject: :Hi"

There is an extra ':' here?

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: "{{ mailbody }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from: me@myshop.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 12:05pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/12 "2020-06-04T12:05:58Z")

</div>

@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 ?

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [June 4, 2020, 12:07pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/13 "2020-06-04T12:07:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 4, 2020, 12:25pm UTC](https://forum.ansible.com/t/add-new-line-n-character-to-ansible-set-fact-variable/33084/14 "2020-06-04T12:25:54Z")

</div>

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.
