# YAML quoting problem

**URL:** <https://forum.ansible.com/t/yaml-quoting-problem/16802>\
**Category:** Ansible Project\
**Created:** [November 17, 2014, 4:59am UTC](https://forum.ansible.com/t/yaml-quoting-problem/16802 "2014-11-17T04:59:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 17, 2014, 4:59am UTC](https://forum.ansible.com/t/yaml-quoting-problem/16802/1 "2014-11-17T04:59:56Z")

</div>

I am using the lineinfile module to define the SMART\_HOST value in a sendmail.mc file. I have tried many variations of specifying the ‘line=’ value, but all generate almost but not quite correct results.  
For example: lineinfile: dest=/etc/mail/sendmail.mc state=present regexp=‘SMART\_HOST’ line=“define(`SMART_HOST', `{{smarthost}}')dnl”  
generates: define(`SMART_HOST’, `gateway.here.com’)dnl

Or: lineinfile: dest=/etc/mail/sendmail.mc state=present regexp=‘SMART\_HOST’ line=‘define(`SMART_HOST'', `{{smarthost}}’‘)dnl’ (that is two single quotes)  
generates: define(`SMART_HOST, `[gateway.here.com](http://gateway.here.com))dnl

And: lineinfile: dest=/etc/mail/sendmail.mc state=present regexp=‘SMART\_HOST’ line=“define(`SMART_HOST"'", `{{smarthost}}”'“)dnl” (double quote, single, double)  
generates: define(`SMART_HOST", `[gateway.here.com](http://gateway.here.com)")dnl

And so on. Everything I have tried either comes close or just totally fails. The result I am looking for is: define(`SMART_HOST', `[gateway.here.com](http://gateway.here.com)’)dnl  
Does anyone know the proper quoting and escaping required to achieve this through YAML?  
Thanks,  
-Mark

---

<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:** [November 17, 2014, 8:45pm UTC](https://forum.ansible.com/t/yaml-quoting-problem/16802/2 "2014-11-17T20:45:45Z")

</div>

A good trick with the lineinfile module is almost always to use the template module if you can 🙂

The problem here is if you start something with a quote, it assumes that quote is not part of the string and then will end the quote.

Thus you might find it easier to just escape quotes with " or ' when needed.

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 17, 2014, 9:20pm UTC](https://forum.ansible.com/t/yaml-quoting-problem/16802/3 "2014-11-17T21:20:09Z")

</div>

I naturally tried using backslash to escape the quotes since that is pretty much the standard in scripting, but that just resulted in the backslash getting embedded into the result:

lineinfile: dest=/etc/mail/sendmail.mc state=present regexp=‘SMART\_HOST’ line=“define(`SMART_HOST', `{{smarthost}}')dnl”  
generates: define(\`SMART_HOST\', \\`[mailgw-sin.go2uti.com](http://mailgw-sin.go2uti.com)')dnl

When I researched this on the Internet, I found this:[https://github.com/dotmaster/toYaml/issues/1](https://github.com/dotmaster/toYaml/issues/1) Essentially, the proper way to escape a quote is with another quote. Except that isn’t quite working either (example in my original post).  
What I found out minutes ago is that it appears to be related to the OS version. When I use this:

lineinfile: dest=/etc/mail/sendmail.mc state=present regexp=‘SMART\_HOST’ line=“define(`SMART_HOST', `{{smarthost}}')dnl”

it works perfectly when the target server is OEL6.5 but with OEL5.5 as the target server it generates define(`SMART_HOST’, `mailgw-sin.go2uti.com’)dnl.  
So something in OEL5.5 wants to escape the backtick during processing, but does not do that in OEL6.5.

I tried using the replace module instead:

replace: dest=/etc/mail/sendmail.mc regexp=“^define.\*SMART\_HOST.\*dnl$” replace=“define(`SMART_HOST', `{{smarthost}}')dnl”

and that works perfectly on both flavors of OS. I don’t know why since the replace value of the replace module is identical to the line value of the lineinfile module. There is a bug someplace in the chain when OEL5.5 is the target, but we don’t build very many OEL5.5 servers anymore, so I don’t think I am going to lose too much sleep over this. What ever works, I guess.  
-Mark
