How to pass argument in packer provision script?

≡放荡痞女 提交于 2019-12-08 15:58:27

问题


I am struggling to pass input parameter to packer provisioning script. I have tried various options but no joy.

Objective is my provision.sh should accept input parameter which I send during packer build.

packer build -var role=abc test.json

I am able to get the user variable in json file however I am unable to pass it provision script. I have to make a decision based on the input parameter.

I tried something like

"provisioners": 
{
  "type": "shell",
  "scripts": [
  "provision.sh {{user `role`}}"
 ]
}

But packer validation itself is failed with no such file/directory error message.

It would be real help if someone can help me on this.

Thanks in advance.


回答1:


You should use the environment_vars option, see the docs Shell Provisioner - environment_vars.

Example:

"provisioners": [
  {
    "type": "shell"
    "environment_vars": [
      "HOSTNAME={{user `vm_name`}}",
      "FOO=bar"
    ],
    "scripts": [
      "provision.sh"
    ],
  }
 ]



回答2:


If your script is already configured to use arguments; you simply need to run it as inline rather than in the scripts array.

In order to do this, the script must exist on the system already - you can accomplish this by copying it to the system with the file provisioner.

  "provisioners": [
    {
      "type":        "file",
      "source":      "scripts/provision.sh",
      "destination": "/tmp/provision.sh"
    },
    {
      "type": "shell",
      "inline": [
        "chmod u+x /tmp/provision.sh",
        "/tmp/provision.sh {{user `vm_name`}}"]
    }
  ]


来源:https://stackoverflow.com/questions/47596369/how-to-pass-argument-in-packer-provision-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!