LoginSignup
6
6

More than 5 years have passed since last update.

開発環境を構築するのに使ってるAnsibleのplaybook.yml

Posted at

方針

playbook.ymlを一つ実行すれば環境構築が済むようにする

以前はChef Solo + Berkshelfで開発環境構築を行っていたが、以下のようなことが手間になりAnsibleに移行した。

  • レシピを実行できる環境を作るまでが手間
  • レシピのソースを読まないとカスタマイズがしずらいのと、どう動いてるかが分かりづらい

Vagrant + VirtualBoxで動かす

最近は開発環境にDockerを使うという選択肢もあるが、init.dやsystemdで起動するものがapt-get installしただけだと動かなくて別途設定が必要なこともありVirtualBoxを使っている。

Vagrantの場合に以下のように設定を追加しておくと、初回にvagrant upした時にansibleが実行されるので便利。

Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

ソース

playbook.yml
- hosts: all
  become: yes
  vars:
    user: ubuntu
  tasks:
    - name: Set timezone
      timezone: name=Asia/Tokyo

    - name: Add docker key
      apt_key: url=https://download.docker.com/linux/ubuntu/gpg state=present

    - name: Add docker repo
      apt_repository: repo='deb [arch=amd64] http://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable' state=present

    - name: Add heroku key
      apt_key: url=https://cli-assets.heroku.com/apt/release.key state=present

    - name: Add heroku repo
      apt_repository: repo='deb http://cli-assets.heroku.com/branches/stable/apt ./' state=present

    - name: Install packages
      apt: name={{item}} state=present update_cache=yes
      with_items:
        - language-pack-ja
        - zsh
        - screen
        - emacs-nox
        - silversearcher-ag
        - mysql-server
        - redis-server
        - elasticsearch
        - imagemagick
        - ruby
        - ruby-dev
        - nodejs
        - nodejs-legacy
        - npm
        - phantomjs
        - python-pip
        - libxml2-dev
        - libmagick++-dev
        - libmysql++-dev
        - libpq-dev
        - docker-ce
        - heroku

    - name: Add docker group
      user: name={{user}} append=yes groups=docker

    - name: Install docker-compose
      pip:
        name: docker-compose

    - name: Start services
      action: service name={{item}} state=started enabled=yes
      with_items:
        - mysql
        - redis
        - elasticsearch
        - docker

    - name: Install awscli
      pip: name=awscli state=present

    - name: Install bundler
      gem: name=bundler user_install=no state=present

    - name: Install yarn
      npm: name=yarn global=yes state=present

    - name: Execute chsh zsh
      command: chsh ubuntu -s /bin/zsh

    - name: Execute git clone dotfiles
      become_user: '{{user}}'
      git: repo=https://github.com/f96q/dotfiles.git dest=/home/{{user}}/dotfiles accept_hostkey=yes

    - name: Setup dotfiles
      become_user: '{{user}}'
      command: bash setup.sh chdir=/home/{{user}}/dotfiles
6
6
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
6
6