miltone
miltone

Reputation: 4721

simple shell inline provisionning

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

PS : My template is validated by packer validate

Upvotes: 2

Views: 2221

Answers (1)

Patrick Lee
Patrick Lee

Reputation: 2013

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.

Upvotes: 3

Related Questions