LoginSignup
3
3

More than 5 years have passed since last update.

windows7 32bit / Proxy環境下でVagrant+Ansibleの環境を構築する。

Last updated at Posted at 2015-06-15

はじめに

  • 社内勉強用の投稿です。
  • 今どきwindows7 32bitでプロキシ環境下で作業している方へ送るansibleの環境構築手順です。
  • Vagrantのインストール、Ansibleのインストールと動作確認を行う手順です。

想定する環境

  • Windows7 32bit Proxy環境下

VirtualBoxのインストール

ここからダウンロードしてインストールします。
Oracle VirtualBox Download

会社で利用しているマシン(Windows7 32bit)ではVagrantがうまく動きませんので
4.1.12をインストールします。
https://www.virtualbox.org/wiki/Download_Old_Builds_4_1_pre14

Vagrantのインストール

ここからダウンロードしてインストールします。
https://www.vagrantup.com/downloads.html

プロキシの設定

  • Vagrantをプロキシ環境下で利用する場合には設定が必要です。PROXY_URLとPROXY_PORTは適宜変更してください。
set http_proxy=PROXY_URL:PROXY_PORT
set https_proxy=PROXY_URL:PROXY_PORT

仮想マシンのプロキシ設定

Vagrantproxyconfプラグインをインストールします。

このプラグインはVagrant管理下のOS起動時にプロキシを自動的に設定してくれるプラグインです。たとえばyum利用時にいちいちproxyの設定をする必要が無くなります。

vagrant plugin install vagrant-proxyconf

プラグインがインストールできたことを確認しましょう。

# vagrant plugin list
vagrant-proxyconf (1.5.0)

Vagrantを使ってCentOSを2台起動します。

1.C:¥Vagrantディレクトリを作成します
2.以下の内容が記載されたVagrantfileというファイルを作成します。
proxyを利用している環境では、PROXY_URL,PROXY_PORTは適宜変更してください。

¥vagrant¥Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.define "ansible-host" do |node|
        node.vm.box = "chef/centos-6.5-i386"
        node.vm.hostname = "ansible-host"
        node.vm.network :private_network, ip: "192.168.1.10"
  end
  config.vm.define "ansible-target" do |node|
        node.vm.box = "chef/centos-6.5-i386"
        node.vm.hostname = "ansible-target"
        node.vm.network :private_network, ip: "192.168.1.20"
  end
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.http = "http://PROXY_URL:PROXY_PORT/"
    config.proxy.https = "http://PROXY_URL:PROXY_PORT/"
    config.proxy.no_proxy = "localhost,127.0.0.1,192.168."
  end
end

3.CentOSを起動します。

#vagrant up

4.しばらくするとansible-host(192.168.1.10),ansible-target(192.168.1.20)2つのhostが起動するはずです。下記の状態になっていることを確認します。

#vagrant status
Current machine status:

ansible-host running (virtualbox)
ansible-target running (virtual box)

5.ansible-hostとansible-targetにsshログインできることを確認します。

windows環境なのでteratermでログインします。

ユーザID:vagrant
パスワード:vagrant

Ansibleをインストールします。

EPELからyumでインストールする方法とpipインストールの方法がありますが、ここではyumでインストールしてみたいと思います。

1.teratermでansible-hostにログイン
2.RH6(32bit)用のyumのepelリポジトリを追加します。

$ sudo yum localinstall http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

その他のOSの場合は下記を指定してください。

RedHat 6系 (32bit)
http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RedHat 6系 (64bit)
http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RedHat 7系 (64bit)
http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm

3.ansibleをインストールします。

$ sudo yum install ansible

4.インストールできていることを確認します。

$ ansible --version

ansibleを使ってansible-targetにコマンドが実行できることを確認します。

ansible-hostの秘密鍵をtargetに渡してsshログイン時にpw入力不要にします。

1.まずは鍵を作成します。何も入力せずにEnter連打で大丈夫です。

$ ssh-keygen -t rsa

2.秘密鍵をansible-targetにコピーします。

$ ssh-copy-id vagrant@192.168.1.20

3.ansible-targetにPWなしでログインできることを確認します。

$ ssh vagrant@192.168.1.20

ログインできることを確認したらログアウトして、ansible-hostに戻ります。

4.カレントディレクトリにhostsというファイルを作成します。
hostsファイルはインベントリファイルと呼ばれ、ansible対象のサーバの一覧を記載します。

192.168.1.20

5.ansible-hostからansible-targetにpingを打って結果が帰ってくれば動作確認完了です。

$ ansible -i hosts 192.168.1.20 -m ping
192.168.1.20 | success >> {
    "changed": false,
    "ping": "pong"
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3