Packer: Shell Provisioner: only override some environment variables?

Created on 29 Apr 2019  路  2Comments  路  Source: hashicorp/packer

I have a shell provisioner a bit like the following:

{
  "type": "shell",
  "environment_vars": [
    "LIST={{user `var_1`}}",
    "OF={{user `var_2`}}",
    "ENV={{user `var_3`}}",
    "VARS={{user `var_4`}}",
    "NAME={{user `prod_name`}}",
    "ALLOW_SSH=false"
  ],
  "scripts": ["scripts/install.sh"]
}

I want to keep most of these variables the same for all builders (though they may change between runs), but vary e.g. the NAME and ALLOW_SSH variables between builders. As far as I can tell, if I override environment_vars then I have to duplicate the whole list. This is especially inconvenient when there are more variables and I need to override multiple builders:

"override": {
    "ami-dev": {
      "environment_vars": [
        "REALLY={{user `var_1`}}",
        "QUITE={{user `var_2`}}",
        "LONG={{user `var_3`}}",
        "LIST={{user `var_4`}}",
        "OF={{user `var_5`}}",
        "ENV={{user `var_6`}}",
        "VARS={{user `var_7`}}",
        "NAME={{user `ami_name`}}",
        "ALLOW_SSH=true"
      ]
    },
    "ovf-dev": {
      "environment_vars": [
        "REALLY={{user `var_1`}}",
        "QUITE={{user `var_2`}}",
        "LONG={{user `var_3`}}",
        "LIST={{user `var_4`}}",
        "OF={{user `var_5`}}",
        "ENV={{user `var_6`}}",
        "VARS={{user `var_7`}}",
        "NAME={{user `ovf_name`}}",
        "ALLOW_SSH=true"
      ]
    }
},

I can work around this by overriding execute_command: "{{.Vars}} NAME={{user `ami_name`}} ALLOW_SSH=true {{.Path}}" instead, but I think this confuses the intent. It would be really great if overriding environment_vars only overrode those variables that are specified, so that I could simply write:

"override": {
  "ami-dev": {
    "environment_vars": [
      "NAME={{user `ami_name`}}",
      "ALLOW_SSH=true"
    ]
  },
  "ovf-dev": {
    "environment_vars": [
      "NAME={{user `ovf_name`}}",
      "ALLOW_SSH=true"
    ]
  }
},

Variables that actually need to be unset could be overridden with e.g. "REALLY=".

config-2.0 enhancement

Most helpful comment

Hi, thanks for reaching out. We're gonna be working on revamping our config as part of the initiative to move it to HCL2 later this year, and I'm going to link this ticket so that we can keep it in mind as we do that work.

All 2 comments

Hi, thanks for reaching out. We're gonna be working on revamping our config as part of the initiative to move it to HCL2 later this year, and I'm going to link this ticket so that we can keep it in mind as we do that work.

See #1768

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tensho picture Tensho  路  3Comments

znerd picture znerd  路  3Comments

shashanksinha89 picture shashanksinha89  路  3Comments

mushon4 picture mushon4  路  3Comments

mvermaes picture mvermaes  路  3Comments