hashicorp / consul-template

Template rendering, notifier, and supervisor for @HashiCorp Consul and Vault data.
https://www.hashicorp.com/
Mozilla Public License 2.0
4.76k stars 782 forks source link

Variable is expanded at unpredictable way #1968

Open EugenKon opened 4 months ago

EugenKon commented 4 months ago

Consul Template version

consul-template v0.37.6 (358ff54)

Configuration

      template {
        destination = "local/script.sh"
        data        = <<-EOH
          set -ex
          cmd='bash -c "source \${NOMAD_ALLOC_DIR}/creds.env; backup"'
          nomad alloc exec -task postgres-task "${DB_ALLOC_ID}" ${cmd}  || {
            exit 1;
          };
        EOH
      }

Debug output

+ cmd='bash -c "source \${NOMAD_ALLOC_DIR}/creds.env; backup"'
+ nomad alloc exec -task postgres-task a4ecf71b-3a7e-5a3b-cabd-2df187852011 bash -c '"source' '\${NOMAD_ALLOC_DIR}/creds.env;' 'backup"'
\${NOMAD_ALLOC_DIR}/creds.env;: -c: line 1: unexpected EOF while looking for matching `"'

Expected behavior

The command should be:

"source" "\${NOMAD_ALLOC_DIR}/creds.env;" "backup"

Actual behavior

"source' '\${NOMAD_ALLOC_DIR}/creds.env;' 'backup"

Steps to reproduce

  1. Nomad plan
  2. Nomad run
  3. "View logs" for the task.

Thoughts

Also it could be

+ nomad alloc exec -task postgres-task a4ecf71b-xx bash -c 'source ${NOMAD_ALLOC_DIR}/creds.env; backup'

Notice, in the case above the string is 'source ${NOMAD_ALLOC_DIR}/creds.env; backup' For the template:

          nomad alloc exec -task postgres-task "${db_alloc_id}" \
            bash -c "source \${NOMAD_ALLOC_DIR}/creds.env; backup"  || {
              exit 1;
          };

It should be the same for the case when I used $cmd.

EugenKon commented 4 months ago

Another example from my draft:

+ cmd='bash -c '\''source ${NOMAD_ALLOC_DIR}/creds.env; backup'\'''
+ nomad alloc exec -task postgres-task a4ecf71b-3a7e-5a3b-cabd-2df187852011 bash -c ''\''source' '${NOMAD_ALLOC_DIR}/creds.env;' 'backup'\'''

Notice how single ' is doubled.