【Vagrant】pacファイルを利用したプロキシ環境下でVagrantのゲストOSからインターネットへアクセスする

忘備録。OSはWindowsです。

自分の場合は下記で設定できるようになりましたが、
環境によって異なる場合もあると思います。

VagrantCentOSを立てたのですが、
プロキシ環境のためyumが使えなかったのでプロキシを設定しました。

Vagrant-pluginのインストール

Vagrantでプロキシを利用する場合は、プラグインvagrant-proxyconfを使います。

インストールコマンド

> vagrant plugin install vagrant-proxyconf

インストールしようとするとエラー。

PS D:\Vagrant\centos> vagrant plugin install vagrant-proxyconf
Installing the 'vagrant-proxyconf' plugin. This can take a few minutes...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

Could not fetch specs from http://gems.hashicorp.com/

Warning: this Gemfile contains multiple primary sources. Using `source` more than once without a block is a security ris
k, and may result in installing unexpected gems. To resolve this warning, use a block to indicate which gems should come
 from the secondary source. To upgrade this warning to an error, run `bundle config disable_multisource true`.
PS D:\Vagrant\centos>

調べてみたところ、そもそもこのplug-inのインストール自体が、
プロキシ環境下ではうまくいかないらしい。

なので、一旦プロキシ環境から外した状態で実施。

PS D:\Vagrant\centos> vagrant plugin install vagrant-proxyconf
Installing the 'vagrant-proxyconf' plugin. This can take a few minutes...
Installed the plugin 'vagrant-proxyconf (1.5.2)'!

入ったことを確認。

PS D:\Vagrant\centos> vagrant plugin list
vagrant-proxyconf (1.5.2)
vagrant-share (1.1.5, system)

自動構成スクリプト(pacファイル)からプロキシのアドレスを調べる

自動構成スクリプト(pacファイル)の場合は、
実際にアクセスしているプロキシのURLとポートを調べる必要があります。

自分の場合はpacファイルのURLにブラウザで直接アクセスして、そこから取得しました。


http://XXXXXX/XXXX.pac

pacファイルはJavaScriptで書かれており、
return "Proxy" アドレス:ポート"となっている部分がプロキシになります。

動作確認する場合は、
Firefoxなどプロキシ設定を独自で設定できるブラウザなどでチェックします。

Vagrantfileにプロキシ設定を記入

先ほど取得したプロキシ情報をもとに、Vagrantファイルを編集します。
Vagrantfileにプロキシを設定。

設定例

  config.proxy.http     = "http://192.168.0.2:8080/"
  config.proxy.https    = "http://192.168.0.2:8080/"
  config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"

実施後、yumが利用できるようになりました。