ansibleをサックと簡単に使うんよ

ansibleでvagrantの開発環境をサックと整備したいということで色々やってみた。

ansible_localというのでvagrant内にansibleをインストールして、そこで、playbookに記述した内容を実行する。

ruby on railsの開発環境を構築してみた。

Vagrantファイルのあるところに
play_book.yml
hosts
ansible.cfg
の3つを用意する。

play_book.yml

---
- hosts: all
  become: yes
  user: vagrant
  tasks:
    - yum: name={{item}} state=latest
      with_items:
        - gcc
        - zlib-devel
        - openssl-devel
        - libffi-devel
        - readline-devel

    - name: install git
      yum: name=git state=latest

    - git: >
        repo=https://github.com/sstephenson/rbenv.git
        dest=/home/vagrant/.rbenv/

    - git: >
        repo=https://github.com/sstephenson/ruby-build.git
        dest=/home/vagrant/.rbenv/plugins/ruby-build/
    - file: path=/home/vagrant/.rbenv owner=vagrant state=directory recurse=yes


    - lineinfile: 
        dest: /home/vagrant/.bashrc
        line: export PATH=$PATH:$HOME/.rbenv/bin:$HOME/.rbenv/shim
   
    - lineinfile: 
        dest: /home/vagrant/.bashrc
        line: eval "$(rbenv init -)"
    

    - shell: source $HOME/.bashrc

    - shell: rbenv install -v 2.1.5
      become: no
    - shell: rbenv rehash
      become: no
    - shell: rbenv global 2.1.5
      become: no

    - shell: /usr/bin/bash -l -c '$HOME/.rbenv/shims/gem install bundler --no-document'
      become: yes
      become_user: vagrant
    - shell: /usr/bin/bash -l -c '$HOME/.rbenv/shims/gem install rails -v 4.1.0'
      become: yes
      become_user: vagrant

    - yum: name={{item}} state=latest
      with_items:
        - ruby-devel
        - rubygems

hosts

192.168.33.10 ansible_connection=local


ansible.cfg

[defaults]
host_key_checking = no
executable = /bin/bash -l

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes

Vagrantfile

Vagrant.configure("2") do |config|
 config.vm.box = "CentOS72"
 config.vm.network "private_network", ip: "192.168.33.10"

 config.vm.provision :ansible_local do |ansible|
    ansible.playbook = 'play_book.yml'
    ansible.verbose = true
    ansible.install = true
    ansible.limit = 'all'
    ansible.inventory_path = 'hosts'

  end
end