2

在实际创建共享之前,我需要在我的 Vagrantfile 中设置一个用户。我想启动 Vagrant 实例,配置用户,然后设置共享。设置共享后,我想运行更多配置。

我该如何做到这一点?

谢谢!

示例 YAML 配置文件:

centos64:
    ip: "192.168.33.110"
    properties:
        memory: 8192
        cpus: 2
    forwards:
        - host: 8080
          guest: 8080
        - host: 9115
          guest: 9115
    shares:
        repos:
            owner: zenoss
            group: zenoss
            host: "~/repos/zenoss/"
            guest: "/home/zenoss/repos"
    providers:
        virtualbox: "http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box"
        vmware_fusion: "https://dl.dropbox.com/u/5721940/vagrant-boxes/vagrant-centos-6.4-x86_64-vmware_fusion.box"
    provisions:
        # sets up users and passwords
        - provision: shell
          path: setup_users.sh
          privileged: true
        # installs packages for OS
        - provision: shell
          path: install_packages.sh
          privileged: true
        # update rc files
        - provision: shell
          path: update_rcs.sh
          privileged: true

示例 Vagrantfile:

require 'yaml'

settings = YAML.load_file "vagrant_config.yml"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  settings.each do |name, cfg|
    config.vm.define name do |agent|
      agent.vm.box = name
      cfg[:forwards].each do |port|
        agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
      end
      if cfg.has_key?(:shares)
        cfg[:shares].each do |sname, share|
          share[:host] = File.expand_path(share[:host])
          group = "vagrant"
          owner = "vagrant"
          host = share[:host]
          guest = share[:guest]
          if share.has_key?(:group)
            group = share[:group]
          end
          if share.has_key?(:owner)
            owner = share[:owner]
          end
          if
          box.vm.synced_folder host, guest, owner: owner, group: group
        end
      end
      agent.vm.provider :vmware_fusion do |v, override|
        v.box = name + "_fusion"
        v.box_url = cfg[:providers][:vmware_fusion]
        cfg[:properties].each do |prop, val|
          if prop == "cpus"
            prop = "numvcpus"
          elsif prop == "memory"
            prop = "memsize"
          end
          v.vmx[prop] = val
        end
        v.vmx['fixed-address'] = cfg['ip']
      end
      agent.vm.provider :virtualbox do |v, override|
        override.vm.box = name + "_vb"
        override.vm.box_url = cfg['providers']['virtualbox']
        override.vm.network :private_network, ip: cfg['ip']
        cfg[:properties].each do |prop, val|
          if prop == "numvcpus"
            prop = "cpus"
          elsif prop == "memsize"
            prop = "memory"
          end
          v.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
        end
      end
      cfg[:provisions].each do |prov|
        agent.vm.provision prov[:provision] do |p|
          # setup keys that do not directly map
          if prov.has_key?('recipes')
            prov[:recipes].each do |r|
              p.add_recipe r
            end
            prov.delete(:recipes)
          end
          # setup 1:1 mapping keys
          prov.each do |key, val|
            if key != 'provision'
              p.instance_variable_set("@#{key}", val)
            end
          end
        end
      end
    end
  end
end
4

1 回答 1

0

我通过设置 packer.io 构建并在那里添加配置解决了这个问题。

{
  "builders": [
    {
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos-6.6/ks.cfg<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": 40960,
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "guest_os_type": "RedHat_64",
      "http_directory": "http",
      "iso_checksum": "08be09fd7276822bd3468af8f96198279ffc41f0",
      "iso_checksum_type": "sha1",
      "iso_url": "{{user `mirror`}}/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso",
      "output_directory": "packer-centos-6.6-x86_64-virtualbox",
      "shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_username": "vagrant",
      "ssh_wait_timeout": "10000s",
      "type": "virtualbox-iso",
      "vboxmanage": [
        [
          "modifyvm",
          "{{.Name}}",
          "--memory",
          "8192"
        ],
        [
          "modifyvm",
          "{{.Name}}",
          "--cpus",
          "4"
        ]
      ],
      "virtualbox_version_file": ".vbox_version",
      "vm_name": "zenoss_centos-6.6-x86_64"
    },
    {
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos-6.6/ks.cfg<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": 40960,
      "guest_os_type": "centos-64",
      "http_directory": "http",
      "iso_checksum": "08be09fd7276822bd3468af8f96198279ffc41f0",
      "iso_checksum_type": "sha1",
      "iso_url": "{{user `mirror`}}/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso",
      "output_directory": "packer-centos-6.6-x86_64-vmware",
      "shutdown_command": "echo 'vagrant'|sudo -S /sbin/halt -h -p",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_username": "vagrant",
      "ssh_wait_timeout": "10000s",
      "tools_upload_flavor": "linux",
      "type": "vmware-iso",
      "vm_name": "packer-centos-6.6-x86_64",
      "vmx_data": {
        "cpuid.coresPerSocket": "1",
        "memsize": "8192",
        "numvcpus": "4"
      }
    }
  ],
  "post-processors": [
    {
      "output": "boxes/centos-6.6.zenoss-4.2.5.{{.Provider}}.box",
      "type": "vagrant"
    }
  ],
  "provisioners": [
    {
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
      "scripts": [
        "scripts/common/sshd.sh",
        "scripts/common/vagrant.sh",
        "scripts/common/sshd.sh",
        "scripts/common/sudoers.sh"
      ],
      "type": "shell"
    },
    {
      "execute_command": "echo 'vagrant'|{{.Vars}} sudo -S -E bash '{{.Path}}'",
      "inline": ["reboot", "sleep 10"],
      "type": "shell"
    },
    {
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
      "scripts": [
        "scripts/common/vmtools.sh"
      ],
      "type": "shell",
      "pause_before": "10s"
    },
    {
      "environment_vars": [
        "BUILD_VERSION=4.2.5",
        "INSTALL_TYPE=resmgr",
        "INSTALL_RPS=no",
        "AUTO_INSTALL=yes",
        "ZENOSS_LOC_IP=artifacts.zenoss.loc"
      ],
      "execute_command": "echo 'vagrant' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
      "scripts": [
        "scripts/centos/cleanup.sh",
        "scripts/centos/sol_zenoss_install.sh",
        "scripts/centos/development.sh",
        "scripts/common/minimize.sh"
      ],
      "type": "shell"
    }
  ],
  "variables": {
    "chef_version": "provisionerless",
    "mirror": "http://mirrors.kernel.org/centos"
  }
}
于 2015-03-26T21:43:59.537 回答