hashicorp / packer

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

Packer pass in provisioners array from variables file #7732

Closed NasAmin closed 5 years ago

NasAmin commented 5 years ago

Hi,

I am using Packer version 1.4.1 What is the best practice to manage generic templates. We have a few packer template files for different windows versions. The template's builders section is common and we use var files to pass in any user variables.

The reason we are having to keep multiple json template files is due to the Provisioner sections being different for different types of templates.

Question: Is there a way we can use the same template file with just the builders section. For provisioners, we can either put them in a separate file and then somehow inject it into the main tempalte OR if we can pass in the Provisioners in the form of a json array through a user variable from a variables file?

Some of my provisioners are powershell scripts, ansible and windows restart. Is it possible to pass in provisioners from a variable file like this?

  "host_provisioners": [ 
    {
      "type": "powershell",
      "inline": [
                "cmd.exe /c powershell.exe Set-ExecutionPolicy Unrestricted -force",
                "a:\\scripts\\Configure-MinorSettings.ps1",                     
                "a:\\scripts\\win2019-setup-ssh.ps1",           
                "a:\\scripts\\Setup-Docker.ps1"
                ]
    },
    {
      "type": "windows-restart",
      "restart_check_command": "powershell -command \"& {Write-Output 'Guest Restarted Successfully.'}\""
    }
]
}

Then in the main template file where my builders are, I do this: "provisioners": "{{userhost_provisioners}}"

What is the best practice on structuring and managing multiple templates or making your templates generic?

I'll really appreciate any guidance on this please as our packer scripts and templates are becoming a bit hard to scale and maintain.

Regards,

Nas

SwampDragons commented 5 years ago

Hi! General questions like this are better addressed by the mailing list, since there are a lot more people reading that list than there are maintainers who read the GitHub issues.

To answer your specific question though, "can you pass in an array of provisioners" -- no. That said, you could probably use jq to do it, or a wrapping script. For example, the packer regression test suite (here)[https://github.com/hashicorp/packer-regressions/blob/master/regressions/runner/generated_runner.py] uses Python to read partial templates like this and generate working build configs from them. The code there is not very clean since it's a WIP that isn't really intended for OSS contribution, but maybe you can find some inspiration in it.

NasAmin commented 5 years ago

Hi @SwampDragons thanks a lot for replying and apologies for not knowing where to ask this question (still learning) :)

I am hoping this may become possible with HCL2 otherwise packer's current design makes it really hard to reuse builders and apply DRY.

By the way, the link to the python script above doesn't work.

SwampDragons commented 5 years ago

Sorry, turns out it's a private repo. I'll clean it up and make it public when I have a chance, but it was probably a bad example anyway.

There's some further advice for good technologies to use here:

https://devops.stackexchange.com/questions/4312/is-there-an-idiomatic-way-to-create-reusable-packer-templates

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.