21
35

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+ansibleで開発環境を構築する(OSインストール編)

Posted at

今回行うこと

前回は、ホスト側でansible2の実行環境の構築を行った。
今回は、 vagrantを使った仮想環境の構築と、ansibleによる最低限の初期化を行う。
なお、今回作成したファイルは
https://github.com/ak-ymst/vm_config
にあります。

Vagrantを使って仮想環境の準備

ansibleによるプロビジョニングに先駆けて、vagrantを使って仮想環境の構築を行う。
OSイメージはこちら(http://www.vagrantbox.es/)からCentOS7.1を貰ってきて使うことにした。

使用したVagrantfileは以下のとおり

Vagrant.configure("2") do |config|
    config.vm.provider :virtualbox do |v|
       v.name = "centos7.1"
       v.customize ["modifyvm", :id, "--memory", 3072]
    end

    config.vm.box = "centos7.1"
    config.vm.box_url = "https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box"

    config.vm.network :private_network, ip: "192.168.33.100" 
    config.ssh.forward_agent = true
end

上記Vagrantfileを用意した上で次のコマンドを実行する。

$ vagrant up

コマンド終了後(初回時のみBOXのダウンロードに多少時間がかかる), sshでログインできることを確認する。

$ vagrant ssh
[vagrant@localhost ~]$ 
[vagrant@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
[vagrant@localhost ~]$ 

Ansibleの動作確認

まず、プロビジョニングが実行されるように、Vagrantfileに以下を追加。

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

ansible.inventory_pathはプロビジョニング対象ホストのリストを保存するファイル名で、内容は以下のとおり。

[vagrant-centos7.1]
192.168.33.100

playbookはプロビジョニングの実際の内容。
とりあえず、動作確認ということで、yumのupdateを行う。

ansible/playbook.yml

---
- hosts: all
  become: true
  roles:
    - init

ansible/roles/init/tasks/main.yml

---
- name: yum update
  become: yes
  yum: name=* state=latest

そして、provision実行

$ vagrant provision
==> default: Running provisioner: ansible...

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.33.100]

TASK [init : yum update] *******************************************************
changed: [192.168.33.100]

PLAY RECAP *********************************************************************
192.168.33.100             : ok=2    changed=1    unreachable=0    failed=0   

$

Timezoneの変更

CentOSのデフォルトのタイムゾーンはUTCとなっているので、これを日本時間に変更する。

[vagrant@vagrant-centos7 ~]$ date
2016年  2月  1日 月曜日 08:12:04 GMT
[vagrant@vagrant-centos7 ~]$ ll /etc/localtime 
lrwxrwxrwx. 1 root root 35 11月  2 14:19 /etc/localtime -> ../usr/share/zoneinfo/Europe/London

以下をansible/roles/init/tasks/main.ymlに追加

- name: change timezone to Asia/Tokyo
  become: yes
  file: src=/usr/share/zoneinfo/Asia/Tokyo dest=/etc/localtime state=link force=true

そしてProvisionを再実行

$ vagrant provision
==> default: Running provisioner: ansible...

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.33.100]

TASK [init : yum update] *******************************************************
ok: [192.168.33.100]

TASK [init : change timezone to Asia/Tokyo] ************************************
changed: [192.168.33.100]

PLAY RECAP *********************************************************************
192.168.33.100             : ok=3    changed=1    unreachable=0    failed=0 

$ vagrant ssh
Last login: Mon Feb  1 17:21:47 2016 from 192.168.33.1
[vagrant@vagrant-centos7 ~]$ date
2016年  2月  1日 月曜日 17:23:44 JST
[vagrant@vagrant-centos7 ~]$ ll /etc/localtime 
lrwxrwxrwx 1 root root 30  2月  1 17:21 /etc/localtime -> /usr/share/zoneinfo/Asia/Tokyo
[vagrant@vagrant-centos7 ~]$ 

無事Timezoneが変更されたことが確認されたところで今回は終了。
次回は、httpd等のインストールを行っていきます。

21
35
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
21
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?