0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrant+virtualboxで仮想マシンを作成

Posted at

はじめに

vagrantについて色々調べたので、頭の整理がてら調べたことをまとめます。

環境

  • ホストOS : Windows10(64bit)
  • Vagrant : 2.2.6
  • 仮想S/W : virtualbox 6.0.14
  • ゲストOS : CentOS8

作業の流れ

今回はAnsibleとterraformを実行するための環境を作成します。

(手順)
1.Box作成
2.Vagrantfile作成
3.環境構築
4.ログイン

1.Box作成

Boxとは、ゲストOSの元ネタとなるOSイメージです。
(AWSで言うところのAMI)

特に意味はありませんが、今回は最新版のCentOS8を使用しました。

尚、addの後ろに記載する引数は(正規ルートかはわかりませんが)Vagrant Cloudのページを見ながら入れています。

> vagrant box add generic/centos8
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop

Enter your choice: 5   ### <=今回はvirtualboxなので"5"を選択 ###
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/virtualbox.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
    box:
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'virtualbox'!

listコマンドでcentos8が表示されれば成功です。

> vagrant box list
generic/centos8 (virtualbox, 2.0.6)

2.Vagrantfile作成

構築する仮想マシンの定義となるVagrantfileを作成します。
(ちなみに大文字小文字は区別するので注意)

ファイルは一から作成しても良いですが、initコマンドでサンプル(テンプレート)を作成することも可能です。

> vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

今回はサンプルを使わず、予め用意したものを使用します。

Vagrant.configure('2') do |config|      ### <= Vagrantファイルのバージョン。"2"を指定
  config.vm.box = "centos/8"            ### <= ゲストOSのソースとなるbox
  config.vm.hostname = "ansible.local"  ### <= 仮想マシンのホスト名
  config.vm.network "private_network",ip: "192.168.33.10" ### <= 仮想マシンのIPアドレス
  config.vm.synced_folder '.', '/vagrant', disabled: true ### <= フォルダ同期をOFFに設定
  config.vm.define 'ansible' do |host|
  end
  config.vm.provider "virtualbox" do |vb| ### <= 仮想マシンを実行する仮想S/W。今回はvirtualbox
    vb.name = "ansible.local"           ### <= vitrualbox上の仮想マシン名
  end
  config.vm.provider "virtualbox" do |vb|
    config.vm.provision "shell", inline: <<-SHELL ### <= 起動後に実行するコマンド
      yum install epel-release -y
      yum install ansible -y
      yum install wget unzip python3 git -y
    SHELL
  end
end

補足1 : IPアドレス

ただ環境を作るだけであれば(ポートフォワーディングで接続するため)IPアドレスは設定不要ですが、AnsibleやTerraformは他マシンへの接続して検証をしたいので設定しています。
IPアドレスはvitualboxのHost-Only AdapterのIPアドレス範囲に合うものを設定します。
(Host-Only Adapterはipconfigなどで確認しましょう)

補足2 : フォルダ同期について

フォルダ同期OFFは必須ではありませんが、デフォルトでは「Vagrantfileが配置されているフォルダを仮想マシン上の/Vagrantディレクトリ下に同期(rsync)」します。
最初は同期有りで進めていたら後述のvagrant upコマンドが全く終わらなくなり、何をしているのかと思ったらユーザのホームフォルダ配下(DocumentやDownloadなどのクソデカファイル置き場がある)を丸々同期しようとしていたので、特別やりたいことが無ければOFFにすることを推奨します。
(これについて触れている記事が少なかった気がするのは、最近のアップデートだからなのか、ユーザのホームフォルダでそんなことをやる奴はいないということなのかは不明)

3.環境構築

vagrant upコマンドを実行すると、色々出力が出た後にログイン可能になります。

> vagrant up
Bringing machine 'ansible' up with 'virtualbox' provider...
==> ansible: Importing base box 'centos/8'...
==> ansible: Matching MAC address for NAT networking...
==> ansible: Checking if box 'centos/8' version '1905.1' is up to date...
==> ansible: Setting the name of the VM: ansible.local
==> ansible: Clearing any previously set network interfaces...
==> ansible: Preparing network interfaces based on configuration...
    ansible: Adapter 1: nat
    ansible: Adapter 2: hostonly
==> ansible: Forwarding ports...
    ansible: 22 (guest) => 2222 (host) (adapter 1)
==> ansible: Booting VM...
==> ansible: Waiting for machine to boot. This may take a few minutes...
    ansible: SSH address: 127.0.0.1:2222
    ansible: SSH username: vagrant
    ansible: SSH auth method: private key
    ansible:
    ansible: Vagrant insecure key detected. Vagrant will automatically replace
    ansible: this with a newly generated keypair for better security.
    ansible:
    ansible: Inserting generated public key within guest...
    ansible: Removing insecure key from the guest if it's present...
    ansible: Key inserted! Disconnecting and reconnecting using new SSH key...
==> ansible: Machine booted and ready!
==> ansible: Checking for guest additions in VM...
    ansible: No guest additions were detected on the base box for this VM! Guest
    ansible: additions are required for forwarded ports, shared folders, host only
    ansible: networking, and more. If SSH fails on this machine, please install
    ansible: the guest additions and repackage the box to continue.
    ansible:
    ansible: This is not an error message; everything may continue to work properly,
    ansible: in which case you may ignore this message.
==> ansible: Setting hostname...
==> ansible: Configuring and enabling network interfaces...
==> ansible: Running provisioner: shell...
(中略)
    ansible: Complete!

4.ログイン

ログインはコマンド一発でできるので非常に便利です(パスワードも不要)

> vagrant ssh
[vagrant@ansible ~]$ cat /etc/centos-release
CentOS Linux release 8.0.1905 (Core)
[vagrant@ansible ~]$

※rootにsuする場合はパスワードが必要になりますが、デフォルトでは"vagrant"が設定されます
(ちなみにvagrantユーザのパスワードも同じ)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?