インフラ構築学習の第一歩です。
目次
インフラ構築は頼りきり
今まではすでに用意されているVagrantfile
とansible playbook
を用いて、開発環境を行ってきました。でも自分でできるようにならなきゃアカン!ということでvagrant init
から開発環境を構築してみました。
ansible_localを使ってみる
さらに、今回はhostにインストールしたansibleではなく、guestにインストールしたansibleを用いて構成管理をするansible_local
を使ってみました。
これを使えばhostにansibleは必要ありません。
ただ、hostにvagrantのプラグインが必要みたいです。
$ vagrant plugin install vagrant-vbguest
手順
vagrant ファイルを作成します。
$ vagrant init bento/centos-6.7
作成されたvagrantfileを編集します。
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure(2) do |config| config.vm.box = "bento/centos-6.7" # boxの名前 config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "private_network", ip: "192.168.33.10" config.vm.synced_folder ".", "/vagrant" config.vm.provider "virtualbox" do |vb| vb.memory = "1024" end config.vm.provision :ansible_local do |ansible| ansible.playbook = 'provision.yml' ansible.inventory_path = 'inventory' ansible.limit = 'all' ansible.verbose = true ansible.install = true end end
ansible_localの設定
ansible_localの設定をしていきます。
vagrantの公式サイトに詳しく載ってました!
ansible.cfg
[defaults] host_key_checking = no [ssh_connection] ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes
inventoryファイルを作成していきます。ファイル名はなんでもいいんですが、vagrantfileに記述したipとansible.inventory_path = 'inventory'
を対応させてください。
192.168.33.10 ansible_connection=local
playbookを書いていきます。これもvagrantfileと対応させてください。
今回は最小限のplaybookになってます。
provison.yml
--- - hosts: all become: yes user: vagrant tasks: - name: yum update yum: name=* state=latest
開発環境を構築していく
これで準備が整ったので開発環境を作っていきます。
vagarnt up
mountエラーが起きてしまうので、対処していきます。(これ毎回起きるんだけどどうにかならんかな?)
vagrant halt vagrant up
playbookが動作していることを確認してください!
成功しました!
あとはplaybookさえ書ければOKなはず
最低限のことはできたので、あとはplaybookでRailsの環境構築ができればOKなはずです!
サンプル
今回の内容をgithubにもupしたのでやってみてください!
vagrant upとmountエラー解消をやればOKなはずです!
コメントを残す