Vagrant Install chef-client on top of base image

2019-03-09 00:30发布

问题:

I'm trying to use Chef to install graphite server and I encountered errors saying that either chef-solo or chef-client was not found on the VM. I'm using Ubuntu 12.04.amd64 LTS, this is server version so it will not have chef-client installed. I know that 13 version will auto have chef-client installed but I can not use 13 version.

I googled and saw some people suggest to ssh to the box and apt-get install chef-client.

My question is: is there anyway that I can preinstall chef-client before chef kicked in? Basically I would like my chef program download the raw image and do everything without additional manual steps from users. Is it possible?

My Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu-12.04-amd64"
    config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box"
    config.vm.hostname = "graphite"
    config.vm.network :forwarded_port, guest: 8080, host: 9090

    config.vm.provision :chef_solo do |chef|
      chef.cookbooks_path = "cookbooks"
      chef.roles_path = "roles"
      chef.data_bags_path = "data_bags"
      chef.add_role "Graphite-Server"
      chef.add_role "StatsD-Server"
    end
end

Error Log:

[default] Running provisioner: chef_solo...
The chef binary (either `chef-solo` or `chef-client`) was not found on
the VM and is required for chef provisioning. Please verify that chef
is installed and that the binary is available on the PATH.

Thanks

回答1:

I found 2 solutions and both work as expected:

1) Method 1: In your Vagrantfile, add

config.omnibus.chef_version = :latest

This will make sure either chef-solo or chef-client is installed on the VM and is required for chef provisioning. In order to use omnibus plugin, make sure you install the plugin first: vagrant plugin install vagrant-omnibus

2) Method 2: Use config.vm.provision shell inline as mentioned here: https://github.com/mitchellh/vagrant-aws/issues/19#issuecomment-15487131. In your Vagrantfile, add:

config.vm.provision "shell", path: "utils/tools/install_chef.bash"

The script utils/tools/install_chef.bash that I wrote looks like this:

#!/bin/bash

function error
{
    echo -e "\033[1;31m${1}\033[0m" 1>&2
}

function checkRequireRootUser
{
    if [[ "$(whoami)" != 'root' ]]
    then
        error "ERROR: please run this program as 'root'"
        exit 1
    fi
}

function installChef()
{
    if [[ "$(which chef-client)" = '' ]]
    then
        local chefProfilePath='/etc/profile.d/chef.sh'

        curl -s -L 'https://www.opscode.com/chef/install.sh' | bash && \
        echo 'export PATH="/opt/chef/embedded/bin:$PATH"' > "${chefProfilePath}" && \
        source "${chefProfilePath}"
    fi
}

function main()
{
    checkRequireRootUser
    installChef
}

main

UPDATE:

if you get following error: Unknown configuration section 'omnibus'. It means you are missing omnibus plugin. To install it, type: vagrant plugin install vagrant-omnibus



回答2:

I can recommend the vagrant-omnibus plugin that basically executes the install.sh script for you, if chef is not installed.



标签: chef vagrant