Skip to content

Instantly share code, notes, and snippets.

@lhriley
Last active November 2, 2017 18:39
Show Gist options
  • Select an option

  • Save lhriley/7099a51a22e99fabda1649e068a63e00 to your computer and use it in GitHub Desktop.

Select an option

Save lhriley/7099a51a22e99fabda1649e068a63e00 to your computer and use it in GitHub Desktop.
Steps to reproduce packer failure with utils.sh file

Steps to reproduce

  1. Create a packer project
  2. In your <config>.json file similar to the following:
{
  "builders": [
    {
      "ami_name"        : "packer-bastion-{{timestamp}}",
      "ami_regions"     : [ "{{user `aws_region_secondary`}}" ],
      "ami_users"       : "{{user `aws_accounts`}}",
      "instance_type"   : "t2.micro",
      "launch_block_device_mappings": [
        {
          "device_name": "/dev/xvdl",
          "volume_size": "{{user `log_volume_size`}}",
          "volume_type": "standard",
          "delete_on_termination": true
        }
      ],
      "ami_block_device_mappings": [
        {
          "delete_on_termination": true,
          "device_name": "/dev/xvdl",
          "volume_size": "{{user `log_volume_size`}}",
          "volume_type": "standard"
        }
      ],
      "name"            : "{{user `company`}}_bastion",
      "profile"         : "{{user `profile`}}",
      "region"          : "{{user `aws_region_primary`}}",
      "source_ami_filter": {
        "filters": {
          "name"                : "{{user `aws_ami_name`}}",
          "owner-alias"         : "amazon",
          "root-device-type"    : "ebs"
        },
        "most_recent": true
      },
      "ssh_username"    : "ec2-user",
      "type"            : "amazon-ebs",
      "tags": {
        "Name"          : "{{isotime \"20060102-150405\"}}",
        "release"       : "{{user `environment`}}",
        "version"       : "{{user `version`}}"
      }
    }
  ],
  "provisioners": [
    {
      "type"            : "shell",
      "inline"          : [ "mkdir -p /tmp/packer/base/devops /tmp/packer/consul /tmp/packer/bastion" ]
    },
    {
      "type"            : "file",
      "source"          : "assets/base/",
      "destination"     : "/tmp/packer/base"
    },
    {
      "type"            : "file",
      "source"          : "assets/consul/",
      "destination"     : "/tmp/packer/consul"
    },
    {
      "type"            : "file",
      "source"          : "assets/bastion/",
      "destination"     : "/tmp/packer/bastion"
    },
    {
      "type"            : "shell",
      "environment_vars": [
                            "CONSUL_VERSION={{user `consul_version`}}"
                          ],
      "execute_command" : "echo '' | sudo -S su - -c '{{ .Vars }} {{ .Path }}'",
      "scripts"         : [
                            "provisioners/base.sh"
                          ]
    }
  ]
}

  1. Within the assets/base/devops folder, include a file named utils.sh by using touch assets/base/devops/utils.sh.
  2. In your provisioners/base.sh file copy the folder /tmp/packer/base/devops to another location such as /usr/local/bin such that you have the end result of /usr/local/bin/devops/utils.sh.
  3. Set the file mode bits using chmod 0750 /usr/local/bin/devops/*.sh Example:
echo "> Move & chmod devops scripts"
mv /tmp/packer/base/devops /usr/local/bin/
chmod 750 /usr/local/bin/devops/*.sh
  1. Run packer using a command like packer build -var-file=vars.json -var environment=test bastion.json
  2. Packer will run, until it completes and successfully exists provisioners/base.sh then die with the error:
Build 'bastion' errored: Error removing temporary script at /tmp/script_8544.sh!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment