Provisioner section shell inline

Please excuse my ignorance I am new to using Packer. To be brief I think I may be doing this wrong or my syntax is wrong or both.

In any event, first off, aside from the documentation, can anyone recommend to me some packer provisioner examples?

I am currently running a Packer build against our VMware environment to roll out CentOS-7.6 images.

I am particularly interested in the provisioners section to do some post software installs and also add some lines to some configuration files. And is it safe to assume that this is the section where I would execute shell commands?

I am running into an issue when I run this “shell” command inline, and maybe is inline is wrong. I want to simply add a line to /etc/sudoers to add a Windows AD group. Real information omitted. There is other stuff I want to do especially with config files, but this is an example that is failing for me.

The section looks like this:

“provisioners”:[
{
“type”:“shell”,
“inline”:[
“echo %Domain\Group ALL=(ALL) ALL >> /etc/sudoers”
]
}
]
}

I am getting some end of statement error, don’t have it now will have to reproduce.

Is my thinking correct if I want to run any shell command whether its a yum install or a simple one liner add to config file, do I do in the type: shell, inline:?

I’ve looked around on git and across the interwebs and I am seeing various different way to do this, some put an “execute command” in there along with the inline.

Again, I am new to Packer have mercy on me. Any help would be appreciated. Thankls.

Hello!

Your snippet syntax is correct for Packer’s specification, the inline value might not be parseable because of the backslash. You might get it to work by escaping the backslash for the AD group, as such:

{  
   "provisioners":[  
      {  
         "type":"shell",
         "inline":[  
            "echo %Domain\\Group ALL=(ALL) ALL >> /etc/sudoers"
         ]
      }
   ]
}

You are correct, the shell commands you would like to execute can be added to the inline. This documentation can better explain it.

execute_command is used for executing scripts with the shell type. I use it for when I have a script to run, such as startup.sh, that takes in a set of environment variables as parameters. Documentation for that here.

The HashiCorp Learn platform has tutorials with some examples you might find useful. Check out this example that includes provisioners.

Hope this helps!

I see some individuals have a scripts directory in their packer folder. Is it safe to assume that I can also store shell scripts locally in the scripts directory and call them that way? Example,

“provisioners”: [
{
“type”: “shell”,
“script”: “scripts/base-provision.sh”

If the scripts are hosted on your build machine in the scripts directory, like your laptop, you will need to use shell-local. The documentation is very similar. The configuration will be a bit different, namely:

{
  "type": "shell-local",
  "script": "scripts/base-provision.sh"
}

If your scripts directory is on your target machine, meaning the image, then you can run the configuration you outlined above.

The way I do it with the Google Cloud Builder is by having a script file in my packer directory. e.g:

$ ls /home/erik/packer
packer.json
script.sh

I would then in my packer.json have:

"provisioners": [
    {
      "type": "shell",
      "script": "script.sh"
    }
  ]

and the script.sh includes the commands I want to execute in the image that is being created e.g:

#!/bin/bash
apt-get update && apt-get install curl

When I then do a packer build packer.json it will execute the script.sh in the image that is being created. This works for the cloud builders, but I don’t know if the VMWare one behaves the same. You might have to try it out for yourself.

Yes I tried with just the shell command and having the scripts directory on my laptop and that works fine, I actually prefer calling the script then doing it inline, feel like I have more control. I did not have to do shell-local just simply shell and the path to my scripts.

Thank you for all of your help new to Packer. And desperately need something in our pipeline to produce faster, cleaner, baked-in images.

1 Like