I'm having trouble getting a template fragment to work.  It's throwing errors which I expect relate to my not having escaped everything I need to escape.  Here's what I need to appear in the output:
if [ $1 = "deploy_cert" ] && [ $2 = "pbx.mydomain.tld" ]; then
  KEY=$3
  CERT=$4
  CHAIN=${5/fullchain.pem/chain.pem}
  scp $CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt
  scp $KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key
  scp $CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt
  ssh root@pbx "/sbin/service httpd reload"
  exit 0
fi
Here's what I have in the fragment:
{
    use strict;
    use warnings;
    use esmith::ConfigDB;
    my $configDB = esmith::ConfigDB->open_ro or die("can't open Config DB");
    my $letsencryptStatus = $configDB->get_prop( 'letsencrypt', 'status' )     || 'disabled';
    if ( $letsencryptStatus ne 'disabled' ) {
        $OUT .= "if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.mydomain.tld\" ]; then\n"
        $OUT .= "  KEY=\$3\n"
        $OUT .= "  CERT=\$4\n"
        $OUT .= "  CHAIN=\${5/fullchain.pem/chain.pem}\n"
        $OUT .= "  scp \$CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt\n"
        $OUT .= "  scp \$KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key\n"
        $OUT .= "  scp \$CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt\n"
        $OUT .= "  ssh root@pbx \"/sbin/service httpd reload\"\n"
        $OUT .= "  exit 0\n"
        $OUT .= "fi\n"
    }
}
...but when I try to expand the template, I get these errors:
[root@e-smith hook-script.sh]# expand-template /usr/local/bin/hook-script.sh 
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Scalar found where operator expected at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT"
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: 	(Missing operator before 
        $OUT?)
ERROR in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Program fragment delivered error <<syntax error at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT ">> at template line 1
ERROR: Template processing failed for //usr/local/bin/hook-script.sh: 2 fragments generated warnings, 1 fragment generated errors
 at /sbin/e-smith/expand-template line 45
I'm assuming that I missed escaping something, but it isn't obvious to me at first glance what it is.  Any suggestions?