# lineinfile module: multiline from stdout is automatically wrapped in quotes

**URL:** <https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844>\
**Category:** Ansible Project\
**Created:** [August 2, 2014, 4:26pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844 "2014-08-02T16:26:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [August 2, 2014, 4:26pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/1 "2014-08-02T16:26:54Z")

</div>

Some quick background. I’m using ansible to automate setup of development environments for new projects. These are active development environments, not freshly provisioned machines, so some challenges come with the territory–in particular there are times when I would use a template to configure a new machine, but in an active dev environment I have to modify existing config files.

In one case I am configuring the ~/.ssh/config file to make it possible to SSH in to a vagrant box using a regular ‘ssh vagrantguest’ by taking the resulting config block from ‘vagrant ssh-config’. This is a multi-line block. I have a playbook that does something like this:

`

- name: Retrieve ssh config from vagrant  
shell: vagrant ssh-config --host vagrantguest chdir={{ project\_path }}  
changed\_when: False  
register: vagrant\_ssh\_config

- name: Add ssh config to ~/.ssh/config  
lineinfile: path=~/.ssh/config line={{ vagrant\_ssh\_config.stdout }} regexp=“^Host vagrantguest\n.\*\n”  
`

The variable, vagrant\_ssh\_config.stdout is added to the file but is surrounded in single-quotes. Is this a bug or expected behavior? Is there a way to remove these quotes? Or is there a better solution to this altogether?

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 2, 2014, 5:16pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/2 "2014-08-02T17:16:59Z")

</div>

This is strange, does the output itself have the quotes or it gets added by jinja? can you post the “vagrant\_ssh\_config” output?

maybe you can try doing this it will stop jinja from automatically escaping your variables based on your environment :

`{{ vagrant_ssh_config.stdout | safe }}`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 2, 2014, 6:24pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/3 "2014-08-02T18:24:40Z")

</div>

Let me know what version of Ansible you are running?

In light of some security fixes, it took a few revs to nail quoting in some cases.

All being said, 1.6.10 is good stuff on that front.

---

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [August 2, 2014, 6:42pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/4 "2014-08-02T18:42:13Z")

</div>

I checked and on a debug message do not see single-quotes in the stdout. Adding the | safe also didn’t help. So it seems that still either jinja or ansible is throwing single-quotes around the variable data before writing it to file. I don’t see this same behavior when writing the same block using the template module, so this may be specific to lineinfile.

I’m running the most recent build, v1.7

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 2, 2014, 7:20pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/5 "2014-08-02T19:20:15Z")

</div>

maybe it’s because of the multiline output?

you should try it with stdout\_lines instead of stdout, you’d probably have to change your regexp parameter to an insertafter

`

- name: Add ssh config to ~/.ssh/config  
lineinfile: path=~/.ssh/config line={{ item }} insertafter=“^Host vagrantguest\n.\*\n”  
with\_items: vagrant\_ssh\_config.stdout\_lines  
`

---

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [August 2, 2014, 8:18pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/6 "2014-08-02T20:18:38Z")

</div>

Good tip. I did manage to get this to work and had to add an extra step to make sure that first “Host vagrant” line is there. My tasks look like this and work fine in ansible 1.7:

`

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 2, 2014, 8:33pm UTC](https://forum.ansible.com/t/lineinfile-module-multiline-from-stdout-is-automatically-wrapped-in-quotes/15844/7 "2014-08-02T20:33:25Z")

</div>

glad it worked for you, someone more experienced with the code can probably tell why it’s wrapping multilines in quotes, not sure if it’s a python thing or an issue with the lineinfile module or even with how ansible registers multiline outputs.

if you have the time maybe you can test something - not even sure if this will work haven’t tried joining jinja varaibles using a newling before…

`

- name: Add ssh config to ~/.ssh/config  
lineinfile: path=~/.ssh/config line={{ vagrant\_ssh\_config.stdout\_lines | join(‘\n’) }} regexp=“^Host vagrantguest\n.\*\n”  
`
