ツナワタリマイライフ

日常ネタから技術ネタ、音楽ネタまで何でも書きます。

Ubuntu16.04にVirtualBoxとVagrantとAnsibleをインストール

はじめに

Windowsノートに入れたUbuntuちゃんの環境整備中です。

take-she12.hatenablog.com

take-she12.hatenablog.com

仮想化環境

持ち歩かないサーバとして働いてもらうので、VMホストになってもらいます。仮想化環境は仕事で使っているOpenStackを試すべく、DevStackを使ったAll-in-One構成も考えたのですが、メモリが4GBしかないので、今回はVirtualBox+Vagrantにします。

MacBookAirで使ってたVagrantfileも使いまわせるしね。

VirtualBoxのインストール

公式サイトからダウンロード。

Oracle VM VirtualBox - Downloads | Oracle Technology Network | Oracle

Linux 64-bit Platforms
Ubuntu 16.04 (Xenial Xerus)

debパッケージをダブルクリックしてインストールします。

f:id:take_she12:20161010194440p:plain:w500

入りましたね。

f:id:take_she12:20161010194742p:plain:w500

Vagrantのインストール

Debianの64bitを選べばよいでしょう。

Download - Vagrant by HashiCorp

f:id:take_she12:20161010195030p:plain:w500

さて、vagrantもあっさり入りましたね。terminalで確認します。

take@lenovo:~$ vagrant --version
Vagrant 1.8.6

Vagrantのセットアップ

内部ネットワークである192.168.1.0/24のsubnetに、CentOS7のVMを試しに作ってみましょう。

Boxのダウンロード

Boxはこちらから。

A list of base boxes for Vagrant - Vagrantbox.es

 CentOS 7.2 x64 (Minimal, Puppet 4.2.3, Guest Additions 4.3.30)

vagrant box addコマンドで追加します。ダウンロードに時間がかかります。

take@lenovo:~$ vagrant box add CentOS72 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'CentOS72' (v0) for provider: 
    box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
==> box: Successfully added box 'CentOS72' (v0) for 'virtualbox'!

以下のようにVagrantfileを作成します。

はてなブログに対して操作するbot用serverを作るつもりなのでこの名前にしました。昔はさくらVPS上のサーバで動いてたんですが解約しちゃったので。

take@lenovo:~/work/vagrant/hatena_bot$ cat Vagrantfile 
Vagrant.configure(2) do |config|
  config.vm.box = "CentOS72"
  config.vm.define :hatenabot do | hatenabot |
    hatenabot.vm.hostname = "hatenabot"
    hatenabot.vm.network :private_network, ip: "192.168.1.100"
  end
end

upしてあげます。

oops!host-only networkはホストが持っているネットワークと別セグメントにしてあげないといけないようです。そりゃそうか。

take@lenovo:~/work/vagrant/hatena_bot$ vagrant up
Bringing machine 'hatenabot' up with 'virtualbox' provider...
==> hatenabot: Importing base box 'CentOS72'...
==> hatenabot: Matching MAC address for NAT networking...
==> hatenabot: Setting the name of the VM: hatena_bot_hatenabot_1476097666108_14574
==> hatenabot: Clearing any previously set network interfaces...
The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network so that it no longer matches that of
a bridged or non-hostonly network.

ただMacBookAirからも同一ネットワークとしてアクセスしたい気持ちもあるんですよね。Ubuntuに一旦踏み台になってからでもいいけど。とりあえず今回は一旦別セグメントにして回避します。

take@lenovo:~/work/vagrant/hatena_bot$ diff Vagrantfile.161010 Vagrantfile
5c5
<     hatenabot.vm.network :private_network, ip: "192.168.1.100"
---
>     hatenabot.vm.network :private_network, ip: "192.168.100.100"

upできました。

take@lenovo:~/work/vagrant/hatena_bot$ vagrant up
Bringing machine 'hatenabot' up with 'virtualbox' provider...
==> hatenabot: Clearing any previously set network interfaces...
==> hatenabot: Preparing network interfaces based on configuration...
    hatenabot: Adapter 1: nat
    hatenabot: Adapter 2: hostonly
==> hatenabot: Forwarding ports...
    hatenabot: 22 (guest) => 2222 (host) (adapter 1)
==> hatenabot: Booting VM...
==> hatenabot: Waiting for machine to boot. This may take a few minutes...
    hatenabot: SSH address: 127.0.0.1:2222
    hatenabot: SSH username: vagrant
    hatenabot: SSH auth method: private key
    hatenabot: Warning: Remote connection disconnect. Retrying...
    hatenabot: 
    hatenabot: Vagrant insecure key detected. Vagrant will automatically replace
    hatenabot: this with a newly generated keypair for better security.
    hatenabot: 
    hatenabot: Inserting generated public key within guest...
    hatenabot: Removing insecure key from the guest if it's present...
    hatenabot: Key inserted! Disconnecting and reconnecting using new SSH key...
==> hatenabot: Machine booted and ready!
==> hatenabot: Checking for guest additions in VM...
    hatenabot: The guest additions on this VM do not match the installed version of
    hatenabot: VirtualBox! In most cases this is fine, but in rare cases it can
    hatenabot: prevent things such as shared folders from working properly. If you see
    hatenabot: shared folder errors, please make sure the guest additions within the
    hatenabot: virtual machine match the version of VirtualBox you have installed on
    hatenabot: your host and reload your VM.
    hatenabot: 
    hatenabot: Guest Additions Version: 4.3.30
    hatenabot: VirtualBox Version: 5.1
==> hatenabot: Setting hostname...
==> hatenabot: Configuring and enabling network interfaces...
==> hatenabot: Mounting shared folders...
    hatenabot: /vagrant => /home/take/work/vagrant/hatena_bot

ログインしてみます。

take@lenovo:~/work/vagrant/hatena_bot$ vagrant ssh hatenabot
[vagrant@hatenabot ~]$

できました。

同一セグメントとしてMBAからもアクセスするためにはpublic networkとするかポートフォワードするかですかね。ポートフォワードはめんどくさいのでUbuntuを踏み台にする方向で別にいいかな。。。

Ansibleのインストール

仕事で今後Ansible使うことになるので、vagrantでdeployしたサーバの配備は全部AnsibleでPlaybookを書く予定です。インストールしておきましょう。

take@lenovo:~/work/vagrant$ sudo apt-get install ansible
[sudo] take のパスワード: 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  ieee-data python-crypto python-ecdsa python-httplib2 python-jinja2
  python-markupsafe python-netaddr python-paramiko python-pkg-resources
  python-selinux python-six python-yaml
提案パッケージ:
  sshpass python-crypto-dbg python-crypto-doc python-jinja2-doc ipython
  python-netaddr-docs python-setuptools
以下のパッケージが新たにインストールされます:
  ansible ieee-data python-crypto python-ecdsa python-httplib2 python-jinja2
  python-markupsafe python-netaddr python-paramiko python-pkg-resources
  python-selinux python-six python-yaml
アップグレード: 0 個、新規インストール: 13 個、削除: 0 個、保留: 1 個。
2,963 kB のアーカイブを取得する必要があります。
この操作後に追加で 17.9 MB のディスク容量が消費されます。
続行しますか? [Y/n] y
(省略)

ちゃんと2系が入ったので良いですね。

take@lenovo:~/work/vagrant$ ansible --version
ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

おわりに

この記事はUbuntuから書きました。screenshotも楽にとれるので記事作成も問題ないですね。

次回は外付けHDDを現在つないでいるWindowsからUbuntuに変更し、ファイルサーバ兼メディアサーバにしたいと思います。