YAML quoting problem

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)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")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’)dnl
Does anyone know the proper quoting and escaping required to achieve this through YAML?
Thanks,
-Mark

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

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.

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')dnl

When I researched this on the Internet, I found this: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