centos + chef + vagrant 出现“网络不可达”

centos + chef + vagrant 出现“网络不可达”

我正在尝试编写一个 Vagrantfile 来在 CentOS 6.4 图像上安装 nginx。

在我的 Vagrantfile 中我有(ip 已删除):

  config.vm.provision :shell,
   :inline => "echo \"export http_proxy=http://10.0.0.1:3128\; export https_proxy=https://10.0.0.1:3128\" >> /etc/profile"

  config.vm.provision :shell,
   :inline => "echo \"export HTTP_PROXY=http://10.0.0.1:3128\; export HTTPS_PROXY=https://10.0.0.1:3128\" >> /etc/profile"

   config.vm.provision :shell,
    :inline => "echo \"proxy=http://10.0.0.1:3128\" >> /etc/yum.conf"

  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "nginx"
    chef.json = {
      "http_proxy" => "http://10.0.0.1:3128",
      "https_proxy" => "https://10.0.0.1:3128",
      "nginx" => {
        "install_method" => "package"
      }
    }
  end

当我运行它时,一切都很顺利,直到它尝试运行 yum 并尝试安装 GPG 文件时,我收到错误:

Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'Base-CentOS-6.4'...

Progress: 10%
Progress: 20%
Progress: 40%
Progress: 60%
Progress: 80%
Progress: 90%
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks
[default] Running provisioner: shell...
[default] Running: inline script
[default] Running provisioner: shell...
[default] Running: inline script
[default] Running provisioner: shell...
[default] Running: inline script
[default] Running provisioner: chef_solo...
Generating chef JSON and uploading...
Running chef-solo...
[2013-08-22T01:24:00+00:00] INFO: Forking chef instance to converge...
[2013-08-22T01:24:00+00:00] INFO: *** Chef 11.6.0 ***
[2013-08-22T01:24:00+00:00] INFO: Setting the run_list to ["recipe[nginx]"] from JSON
[2013-08-22T01:24:00+00:00] INFO: Run List is [recipe[nginx]]
[2013-08-22T01:24:00+00:00] INFO: Run List expands to [nginx]
[2013-08-22T01:24:00+00:00] INFO: Starting Chef Run for localhost
[2013-08-22T01:24:00+00:00] INFO: Running start handlers
[2013-08-22T01:24:00+00:00] INFO: Start handlers complete.
[2013-08-22T01:24:01+00:00] INFO: ohai plugins will be at: /etc/chef/ohai_plugins
[2013-08-22T01:24:01+00:00] INFO: remote_directory[/etc/chef/ohai_plugins] created directory /etc/chef/ohai_plugins
[2013-08-22T01:24:01+00:00] INFO: remote_directory[/etc/chef/ohai_plugins] mode changed to 755
[2013-08-22T01:24:01+00:00] INFO: cookbook_file[/etc/chef/ohai_plugins/README] created file /etc/chef/ohai_plugins/README
[2013-08-22T01:24:01+00:00] INFO: cookbook_file[/etc/chef/ohai_plugins/README] updated file contents /etc/chef/ohai_plugins/README
[2013-08-22T01:24:01+00:00] INFO: cookbook_file[/etc/chef/ohai_plugins/README] mode changed to 644
[2013-08-22T01:24:01+00:00] INFO: ohai[custom_plugins] reloaded
[2013-08-22T01:24:01+00:00] WARN: Cloning resource attributes for service[nginx] from prior resource (CHEF-3694)
[2013-08-22T01:24:01+00:00] WARN: Previous service[nginx]: /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/default.rb:44:in `from_file'
[2013-08-22T01:24:01+00:00] WARN: Current  service[nginx]: /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/default.rb:51:in `from_file'
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] created file /etc/chef/ohai_plugins/nginx.rb
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] updated file contents /etc/chef/ohai_plugins/nginx.rb
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] owner changed to 0
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] group changed to 0
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] mode changed to 755
[2013-08-22T01:24:01+00:00] INFO: template[/etc/chef/ohai_plugins/nginx.rb] sending reload action to ohai[reload_nginx] (immediate)
[2013-08-22T01:24:01+00:00] INFO: ohai[reload_nginx] reloaded
[2013-08-22T01:24:01+00:00] INFO: Adding RPM-GPG-KEY-EPEL-6 GPG key to /etc/pki/rpm-gpg/
[2013-08-22T01:24:12+00:00] INFO: remote_file[/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6] created file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

================================================================================
Error executing action `create` on resource 'remote_file[/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6]'
================================================================================


Errno::ENETUNREACH
------------------
Network is unreachable - connect(2)


Resource Declaration:
---------------------
# In /tmp/vagrant-chef-1/chef-solo-1/cookbooks/yum/providers/key.rb

 61:       remote_file "/etc/pki/rpm-gpg/#{new_resource.key}" do
 62:         source new_resource.url
 63:         mode "0644"
 64:         notifies :run, "execute[import-rpm-gpg-key-#{new_resource.key}]", :immediately
 65:       end
 66:     end



Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/yum/providers/key.rb:61:in `block in class_from_file'

remote_file("/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6") do
  provider Chef::Provider::RemoteFile
  action "create"
  retries 0
  retry_delay 2
  path "/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6"
  backup 5
  atomic_update true
  source ["http://mirror.aarnet.edu.au/pub/epel/RPM-GPG-KEY-EPEL-6"]
  use_etag true
  use_last_modified true
  cookbook_name :yum
  mode "0644"
end



[2013-08-22T01:25:20+00:00] INFO: Running queued delayed notifications before re-raising exception
[2013-08-22T01:25:20+00:00] ERROR: Running exception handlers
[2013-08-22T01:25:20+00:00] ERROR: Exception handlers complete
[2013-08-22T01:25:20+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2013-08-22T01:25:20+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.
[Finished in 126.4s with exit code 127]

并且堆栈跟踪有(加上堆栈跟踪):

[root@localhost ~]# cat /var/chef/cache/chef-stacktrace.out
Generated at 2013-08-22 01:25:20 +0000
Errno::ENETUNREACH: remote_file[/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6] (/tmp/vagrant-chef-1/chef-solo-1/cookbooks/yum/providers/key.rb line 61) had an error: Errno::ENETUNREACH: Network is unreachable - connect(2)

当我通过 ssh 进入盒子、检查代理并尝试卷曲密钥时,我没有遇到任何问题。

[drew@mymachine dev-environment]$ vagrant ssh
Last login: Thu Aug 22 01:25:36 2013 from 10.0.0.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ echo -e "$http_proxy"" - ""$https_proxy""\n""$HTTP_PROXY"" - ""$HTTPS_PROXY"
http://10.0.0.1:3128 - https://10.0.0.1:3128
http://10.0.0.1:3128 - https://10.0.0.1:3128

[vagrant@localhost ~]$ curl "http://mirror.aarnet.edu.au/pub/epel/RPM-GPG-KEY-EPEL-6"
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)

mQINBEvSKUIBEADLGnUj24ZVKW7liFN/JA5CgtzlNnKs7sBg7fVbNWryiE3URbn1
JXvrdwHtkKyY96/ifZ1Ld3lE2gOF61bGZ2CWwJNee76Sp9Z+isP8RQXbG5jwj/4B
...
XtfLk0W5Ab9pd7tKDR6QHI7rgHXfCopRnZ2VVQ==
=V/6I
-----END PGP PUBLIC KEY BLOCK-----
[vagrant@localhost ~]$

我也可以愉快地在盒子里使用 yum 而不会出现任何问题。

我在公司网络中的代理服务器后面,所以我想 Chef 可能没有注意代理设置。我的问题是,有没有人遇到过这种情况,或者对在代理后面运行 Chef 时无法访问网络有任何见解?

我在想如果我无法修复它,只需在厨师运行之前下载并安装密钥,但我想知道这里是否还有其他事情发生。

答案1

我认为 chef-solo 不支持代理配置。 http://docs.opscode.com/config_rb_solo.html

您可以尝试创建另一个半空的厨师配方,它将在“vagrant up”环境中获取您的http_proxy变量。

就像是:

  ENV['http_proxy'] = "http://10.0.0.1:3128"
  ENV['https_proxy'] = "https://10.0.0.1:3128"

然后将该配方添加到 vagrant 文件执行的第一步中:

  chef.add_recipe '[my_http_recipe]'

希望这可以帮助

答案2

您需要直接在 chef 对象上设置属性:

config.vm.provision :chef_solo do |chef|
  chef.http_proxy = $http_proxy
  chef.https_proxy = $https_proxy
  chef.no_proxy = $no_proxy

此外,我建议不要手动调用 shell 脚本来设置环境变量,而是使用vagrant-proxyconf插件。请注意,这确实不是传播到 chef_solo,不要从上面的代码片段中删除配置。

配置如下:

$http_proxy  = "http://10.10.1.1:4128/"
$https_proxy = "http://10.10.1.1:4128/"
$no_proxy    =  "localhost,127.0.0.1,.ag.hermle.de"

Vagrant.configure("2") do |config|
  config.proxy.http     = $http_proxy
  config.proxy.https    = $https_proxy
  config.proxy.no_proxy = $no_proxy

更新:vagrant-proxyconf 将来可能会支持 chef 代理配置,请参阅https://github.com/tmatilai/vagrant-proxyconf/issues/19

相关内容