LoginSignup
0
1

More than 5 years have passed since last update.

vagrant,ansibleを利用した仮想サーバの構築

Last updated at Posted at 2017-07-22

概要

だいぶ今更感もありますが、私の中でサーバの仮想化技術がVirtulBoxあたりで止まっていたので、アップデートしていく。
コマンド一発で欲しい環境を作っちゃう(プロビジョンニング)をやっちゃう。

項目 解説 cf.
VirtualBox 仮想サーバの実行環境を提供,vagrantから見るとプロバイダーという位置付け vmware
vagrant 仮想マシンの起動、停止だけでなく、仮想マシンの状態の設定を自動化(プロビジョニング)する機能を備えている。デフォルトのプロバイダーはvirtualbox,vmwareとかも指定可能
ansible 構成管理ツール、yamlファイル一つでssh接続可能なサーバに必要なアプリケーションを一括でインストールしてくれる Puppet,Chef

インストール

環境
- macOS 10.12.5

virtualbox

公式サイトから

https://www.virtualbox.org/

vagrant

公式サイトから

http://www.vagrantup.com/

ansible

$ brew install ansible

設定

Vagrantfile
provisioning
    hosts
    site.yaml 

以下設定ファイル類

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/xenial64"

  config.vm.network "private_network", ip: "192.168.200.100"

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/site.yaml"
    ansible.inventory_path = "provisioning/hosts"
    ansible.limit = 'all'
  end

end

Vagrantfileはvagrantが仮想サーバを立ち上げる際に呼び出すファイル。

config.vm.box = "ubuntu/xenial64"
というのはVagrantの公式box(仮想サーバのイメージみたい)

他にも色々あるので必要に応じて。

https://app.vagrantup.com/

[vagrants]
192.168.200.100 ansible_python_interpreter=/usr/bin/python3

ansibleのsshを指定するためのファイル

※ubuntu/xenial64はpython3しか入っていない(pathは/usr/bin/python3となる。)ansibleはpythonを利用するため、interpreterを正しく設定する必要があるようです。

参考:
https://stackoverflow.com/questions/32429259/ansible-fails-with-bin-sh-1-usr-bin-python-not-found

site.yaml
---
- hosts: vagrants
  sudo: true
  user: ubuntu
  tasks:
    - name: install packages zsh
      apt: name=zsh update_cache=yes

インストール(ubuntuのapt-getで)するプログラム等を指定するやつ。

実行

インストールの正常性確認

$ vagrant --version

$ ansible --version

$ virtualbox --version

実行とサーバの動作確認

サーバの起動

$ vagrant up 

zshがインストールされているか確認
vagrant sshで仮想サーバにssh接続できる。

$ vagrant ssh

ubuntu@ubuntu-xenial:~$ zsh --version
zsh 5.1.1 (x86_64-ubuntu-linux-gnu)

感想

VirtualBoxのGUIでごにょごにょしてた時代は大変だったけど、すごい楽になってた。

※この先にさらにリソース状況に応じた自動プロビジョニングといった技術があるようですが、それはまた別のお話。

参考にさせていただいたサイト

AnsibleとVagrantで開発環境を構築する
http://knowledge.sakura.ad.jp/tech/2882/

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