hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.06k stars 3.32k forks source link

Builder-specific variables #7849

Closed OJFord closed 5 years ago

OJFord commented 5 years ago

Sometimes scripts or files need to know something about the system that's hard to detect. The easy solution is to pass it in as a variable, but likely the reason we're not just writing it explicitly is that it's different on different systems, so the variable varies by builder.

My current example is configuring iptables and needing to know the name of the physical interface. In the VPS builder I'm using at the moment it's ens3, but with different hardware (and so a different builder) it might easily be different.

Current best workaround is:

{
    "type": "shell",
    "only": ["<build name>"],
    "inline": ["sh", "-c", "echo -n '<value>' > /somewhere/<key>"]
}

provisioner for each builder, key, and value.

A cleaner (not currently valid!) solution might be:


{
    "builders": [{..., "variables": ["<key>=<value>"]}],
    "provisioners": [{"environment_vars": ["whatever={{user `<key>`}}"], ...}]
}
SwampDragons commented 5 years ago

I don't like the idea of variables changing in a non-obvious way like this. I know it's verbose, but forcing people to be explicit prevents a whole host of confusion.

However, there may be hope in (optimistically) 6 months or so. We're working on implementing HCL2 configuration for Packer, which means in the future you may be able to declare variables that are maps; that means you will probably be able to store and retrieve variables by build_name, which would do what you want. In the meantime, your other workaround option is to set the variable from a -vars file and only run one builder at a time.

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.