2

How can I just simple install apache2 package in a packer template for virtualbox-iso Ubuntu and the shell inline provisionning ?

I'm testing this template :

{
"builders": [{
    "boot_command": [
        "<esc><wait>",
        "<esc><wait>",
        "<enter><wait>",
        "/install/vmlinuz<wait>",
        " auto<wait>",
        " console-setup/ask_detect=false<wait>",
        " console-setup/layoutcode=fr<wait>",
        " console-setup/modelcode=pc105<wait>",
        " debconf/frontend=noninteractive<wait>",
        " debian-installer=fr_FR<wait>",
        " fb=false<wait>",
        " initrd=/install/initrd.gz<wait>",
        " kbd-chooser/method=fr<wait>",
        " keyboard-configuration/layout=fr<wait>",
        " keyboard-configuration/variant=fr<wait>",
        " locale=fr_FR<wait>",
        " netcfg/get_domain=vm<wait>",
        " netcfg/get_hostname=vagrant<wait>",
        " noapic<wait>",
        " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg<wait>",
        " -- <wait>",
        "<enter><wait>"
    ],
    "boot_wait": "20s",
    "disk_size": 20480,
    "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
    "guest_os_type": "Ubuntu_64",
    "http_directory": "http",
    "headless": false,
    "iso_checksum": "0501c446929f713eb162ae2088d8dc8b6426224a",
    "iso_checksum_type": "sha1",
    "iso_url": "http://mirrors.mit.edu/ubuntu-releases/trusty/ubuntu-14.04.3-server-amd64.iso",
    "output_directory": "packer-ubuntu-14.04-amd64-virtualbox",
    "shutdown_command": "echo 'vagrant'|sudo -S shutdown -P now",
    "ssh_username": "vagrant",
    "ssh_password": "vagrant",
    "ssh_port": 22,
    "ssh_wait_timeout": "10000s",
    "type": "virtualbox-iso",
    "vboxmanage": [
        [
            "modifyvm",
            "{{.Name}}",
            "--memory",
            "1024"
        ],
        [
            "modifyvm",
            "{{.Name}}",
            "--cpus",
            "1"
        ]
    ],
    "virtualbox_version_file": ".vbox_version",
    "vm_name": "packer-ubuntu-14.04-amd64"
}],
"post-processors": [{
    "output": "builds/ubuntu-14.04.box",
    "type": "vagrant"
}],
"provisioners": [
    {
        "type": "shell",
        "execute_command": "echo '{{user `ssh_pass`}}' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'",
        "inline": [
            "echo '%sudo    ALL=(ALL)  NOPASSWD:ALL' >> /etc/sudoers",
            "sudo apt-get install -y apache2"
        ]
    }
]

}

This result of test provisionning is :

==> virtualbox-iso: Provisioning with shell script: C:\Users\toto\AppData\Local\Temp\packer-shell178737243
virtualbox-iso: [sudo] password for vagrant: Sorry, try again.
virtualbox-iso: [sudo] password for vagrant:
virtualbox-iso: sudo: 1 incorrect password attempt

The first step of building Vagrant seem to be good with preseed.cfg file. But this issue arrived when start the provisionning vagrant.

What is the trick for use a inline provisionning with good right for administration in packer and vagrant system Ubuntu ?

technical context

  • OS Host : windows 7 pro
  • OS Guest : Ubuntu 14.04 LTS
  • VM technology : VirtualBox 5.0.10 + Vagrant 1.7.4
  • VM tools builder : Packer 0.8.6

PS : My template is validated by packer validate

miltone
  • 4,416
  • 11
  • 42
  • 76

1 Answers1

3

The issue in this case is that the ssh_pass user variable is never set in your Packer template. There are two solutions to this. The easiest option would be to modify your execute_command setting in the shell provisioner and hard code the ssh password from your builder...

"execute_command": "echo 'vagrant' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'",

The more elegant solution would be to add a variables block before the builders block at the top of your Packer template and use that to set the ssh username and password...

"variables": {
    "ssh_user": "vagrant",
    "ssh_pass": "vagrant"
},

That would fix your existing execute_command in the shell provisioner and you could also use those variables in the builder like so...

"shutdown_command": "echo '{{user `ssh_pass`}}' | sudo -S shutdown -P now",
"ssh_username": "{{user `ssh_user`}}",
"ssh_password": "{{user `ssh_pass`}}",

That way you would be defining your ssh credentials in one place in your Packer template.

Patrick Lee
  • 1,990
  • 1
  • 19
  • 24
  • For this moment I just trying the first solution which run correctly. Thank you Patrick Lee. I will vote for your response when I will try your second solution this evening. – miltone Jan 12 '16 at 07:58
  • For the second solution, the build run correctly but impossible to connect into the VM but I ask another question for that. @Patrick Lee : thank you see you later... – miltone Jan 12 '16 at 22:03
  • You should also ensure that the `vagrant.sh` script ran, because it enables passwordless sudo inside the VM, which the prompt for a password to sudo shows didn't happen (likely due to the error mentioned in this answer). – dragon788 Jul 12 '17 at 21:45