When creating an image, packer throws errors

Viewed 122

The packer file looks like this:

{
    "variables": {
        "name": "packer",
        "source_ami": "ami-0d527b8c289b4af7f",
        "access_key": "",
        "secret_key": "",
        "region": "eu-central-1"
    },
    "builders": [
        {
            "type": "amazon-ebs",
            "access_key": "{{user `access_key`}}",
            "secret_key": "{{user `secret_key`}}",
            "ami_name": "{{user `name`}}",
            "region": "{{user `region`}}",
            "source_ami": "{{user `source_ami`}}",
            "instance_type": "t2.micro",
            "communicator": "ssh",
            "ssh_username": "ubuntu",
            "run_tags": {
                "Name": "reddit"
            }
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "script": "/home/ansibleclient/devops/scripts/reddit_scripts/bash/install_ruby.sh",
            "execute_command": "sudo {{.Path}}"
        },
        {
            "type": "shell",
            "script": "/home/ansibleclient/devops/scripts/reddit_scripts/bash/install_mongodb.sh",
            "execute_command": "sudo {{.Path}}"
        }
    ]
}

Ruby installation file:

#!/bin/bash

set -e

# Update system
add-apt-repository ppa:awstools-dev/awstools
apt-get update
# Install Ruby
apt-get install -y ruby-full ruby-bundler build-essential

MondoDB installation file:

#!/bin/bash

# Mongodb
echo "y" | apt-get install mongodb

# Start MongoDB
systemctl start mongodb

# Add to autorun
systemctl enable mongodb

# Check status mongodb
systemctl status mongodb

When executing the "packer build" command, errors appear:

The first error appears 2 times, and I do not understand what it is connected with, it appears when using scripts.

==> amazon-ebs: debconf: unable to initialize frontend: Dialog
==> amazon-ebs: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
==> amazon-ebs: debconf: falling back to frontend: Readline
==> amazon-ebs: debconf: unable to initialize frontend: Readline
==> amazon-ebs: debconf: (This frontend requires a controlling tty.)
==> amazon-ebs: debconf: falling back to frontend: Teletype
==> amazon-ebs: dpkg-preconfigure: unable to re-open stdin:

The second error appears when implementing the script "install_mongodb.sh "

 ==> amazon-ebs: Synchronizing state of mongodb.service with SysV service script with /lib/systemd/systemd-sysv-install.
    ==> amazon-ebs: Executing: /lib/systemd/systemd-sysv-install enable mongodb
1 Answers

This is not a problem with Packer, but a Linux behavior when using non interactive shell.

Assuming you're using Ubuntu, try using export DEBIAN_FRONTEND=noninteractive before apt commands. There are few other alternatives and ways to solve this (in my experience the noninteractive does not always work).

You can also try to add DEBIAN_PRIORITY=critical

So something like this for the provision.sh

#!/bin/bash
set -e

export DEBIAN_FRONTEND=noninteractive;
export DEBIAN_PRIORITY=critical;
apt-get -qy clean;
apt-get -qy update;
apt-get -qy upgrade;

add-apt-repository ppa:awstools-dev/awstools
apt-get -qy update;
apt-get install -y ruby-full ruby-bundler build-essential

apt-get install -y mongodb
systemctl start mongodb
systemctl enable mongodb
systemctl status mongodb

Also, it's worth reading StackOverflow guidelines, it will help you to write better questions, and increase the probability of getting an answer.

Related