# lineinfile 'new line' broken for yaml format

**URL:** <https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440>\
**Category:** Ansible Developer\
**Created:** [June 9, 2015, 12:48am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440 "2015-06-09T00:48:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [June 9, 2015, 12:48am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440/1 "2015-06-09T00:48:08Z")

</div>

I am trying to add the following to the bottom of my /etc/tower/settings.py file (note the new line):

> AWX\_TASK\_ENV[‘ANSIBLE\_LIBRARY’] = ‘/etc/ansible/library/’

So I wrote the following in my playbook:

# example 1

lineinfile:  
dest: /etc/tower/settings.py  
line: ‘\nAWX\_TASK\_ENV[’‘ANSIBLE\_LIBRARY’‘] = ‘’/etc/ansible/library/’‘’  
state: present

This doesn’t work. I end up with the following in my file:

> \nAWX\_TASK\_ENV[‘ANSIBLE\_LIBRARY’] = ‘/etc/ansible/library/’

The \n has been written rather than a new line.

So I changed my playbook to this:

# example 2

lineinfile: dest=/etc/tower/settings.py line=‘\nAWX\_TASK\_ENV[’‘ANSIBLE\_LIBRARY’‘] = ‘’/etc/ansible/library/’‘’ state=present

Now I have the following in my file:

> AWX\_TASK\_ENV[ANSIBLE\_LIBRARY] = /etc/ansible/library/

The new line has been correctly added but it has stripped my quotes.

I just wanted to get a better understanding of why the different way of writing a module task behave differently and what is the Ansible-recommended way of writing them?

Further, is there a way to make what I’m trying to do work? Does my first example need to be raised as a bug and in the meantime, is there a way to quote my second example so that it works as I expected?

Sorry if this seems more like a user-group post but i just wanted to get some idea about the history of writing module tasks and how it evolved and why they behave differently.

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [June 9, 2015, 4:50am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440/2 "2015-06-09T04:50:57Z")

</div>

> line: ‘\nAWX\_TASK\_ENV[’‘ANSIBLE\_LIBRARY’‘] = ‘’/etc/ansible/library/’‘’

Try double quotes: “\nfoo”

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [June 9, 2015, 8:38am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440/3 "2015-06-09T08:38:57Z")

</div>

If i double quoted it I would of expected the response I actually got. Indeed, with double quotes the result is the same.

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [June 20, 2015, 12:45am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440/4 "2015-06-20T00:45:43Z")

</div>

\*bump\*

Any feedback from Ansible core guys in the difference in behaviour between value= and value: ?

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [June 20, 2015, 11:43am UTC](https://forum.ansible.com/t/lineinfile-new-line-broken-for-yaml-format/18440/5 "2015-06-20T11:43:41Z")

</div>

Hi Rob,

So inorder to process a task ansbile splits the task into ‘action’ (module\_name) and ‘args’ (arguments to the task).  
The ‘args’ needs to be a dictionary/hash.  
so if we specify the task as (also called complex args)

- lineinfile:  
line=foo  
state=present.

in yaml this is a list item with key=lineinfile and value a dictionary {line: foo, state: present}, and while parsing ansible get the args easily as it is already a dictionary.

when the task is defined as

- lineinfile: line=foo state=present

in yaml this become key=lineinfile and value is string ‘line=foo state=present’, Here ansible parses this string through a method split\_args to convert this string to a dictionary equivalent to {line: foo, state: present}

In your case an extra "" is added by yaml because you have quoated with single quotes, which make yaml treat all the characters as strings special characters are ignored.  
line: ‘\nAWX\_TASK\_ENV[’‘ANSIBLE\_LIBRARY’‘] = ‘’/etc/ansible/library/’‘’

if you replace the above with double quotes, yaml preserves the special charactercts like \n so if you change to this it shoudl work:  
line: "\nAWX\_TASK\_ENV[‘ANSIBLE\_LIBRARY’] = ‘/etc/ansible/library/’ "

when you changed to lineinfile: line=foo method still yaml will add the additional \ but it is stripped by the split\_args method.

i think even with change the lines might get readded as it would not have a match in the next run due to the \n.

Hope this helps.

- Benno
