简单的shell内联配置

时间:2016-01-10 15:08:49

标签: ubuntu vagrant packer

如何在virtualbox-iso Ubuntu和shell内联配置的打包器模板中简单安装apache2包?

我正在测试这个模板:

{
"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"
        ]
    }
]

}

测试配置的结果是:

==> 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

使用preseed.cfg文件构建Vagrant的第一步似乎很好。但是这个问题在启动配置流浪者时就​​已经到了。

在包装工和流浪者系统Ubuntu中使用具有良好管理权限的内联配置有什么诀窍?

技术背景

  • 操作系统主机:Windows 7专业版
  • OS Guest:Ubuntu 14.04 LTS
  • VM技术:VirtualBox 5.0.10 + Vagrant 1.7.4
  • VM工具构建器:Packer 0.8.6

PS:我的模板由packer validate

验证

1 个答案:

答案 0 :(得分:2)

在这种情况下的问题是,ssh_pass用户变量永远不会在您的Packer模板中设置。有两种解决方案。最简单的选择是修改shell配置程序中的execute_command设置,并从构建器中对ssh密码进行硬编码...

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

更优雅的解决方案是在Packer模板顶部的variables块之前添加builders块,并使用它来设置ssh用户名和密码......

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

这会在shell配置程序中修复现有的execute_command,您也可以在构建器中使用这些变量,如此...

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

这样您就可以在Packer模板中的某个位置定义ssh凭据。