YAML quoting problem

108 views
Skip to first unread message

mto...@go2uti.com

unread,
Nov 16, 2014, 11:59:56 PM11/16/14
to ansible...@googlegroups.com

    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



Michael DeHaan

unread,
Nov 17, 2014, 3:45:49 PM11/17/14
to ansible...@googlegroups.com
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.





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c46c39da-31ee-4eee-a52e-b39cf79b0668%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mto...@go2uti.com

unread,
Nov 17, 2014, 4:20:09 PM11/17/14
to ansible...@googlegroups.com

    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
Reply all
Reply to author
Forward
0 new messages