AnsibleでRailsが動く環境をDigital Oceanに作る(1)

AnsibleでRailsが動く環境をDigital Oceanに作る(1)*作業メモ的な不完全な記事です。あとでまとめを書くかもしれません。

はぁはぁ、Digital Oceanにansibleでnginxをインストールするの続きです。

前回まで作ったansibleのファイルに以下の設定を追加していきます。

とりあえず、ここまでの所を貼っておきます。いろいろダメなところがたくさんあると思う。

---
- hosts: ocean
  vars:
    iptables:
      template: ./my_iptables.j2
    nginx:
      rpm: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
      template:
        conf: ./my_nginx.conf.j2
    rbenv:
      repo: git://github.com/sstephenson/rbenv.git
    rubybuild:
      repo: git://github.com/sstephenson/ruby-build.git
    ruby:
      version: 2.1.5
  tasks:
  - name: create iptables
    template: src={{ iptables.template }} dest=/etc/sysconfig/iptables

  - name: update all packages
    yum: name=* state=latest

  - name: install nginx rpm from a remote repo
    yum: name={{ nginx.rpm }} state=present

  - name: install nginx
    yum: name=nginx state=present

  - name: rename nginx conf.default to conf.default.original
    command: mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.original removes=default.conf

  - name: apply-nginx.conf
    template: src={{ nginx.template.conf }} dest=/etc/nginx/conf.d/my_nginx.conf

  - name: start and enable nginx
    service: name=nginx state=started enabled=yes

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

  - name: install memcached
    yum: name=memcached state=present

  - name: install epel
    yum: name=epel-release state=present

  - name: install node.js
    yum: name=nodejs state=present

  - name: install npm
    yum: name=npm state=present

  - name: install ImageMagick
    yum: name=ImageMagick state=present

  - name: install libxml
    yum: name=libxml2-devel state=present

  - name: install libxslt
    yum: name=libxslt-devel state=present

  - name: install ruby
    yum: name=ruby state=present

  - name: check out rbenv
    git: repo={{ rbenv.repo }} dest=./.rbenv accept_hostkey=yes

  - name: add .rbenv/bin to $PATH
    lineinfile: dest=.bash_profile line=export\ PATH=".rbenv/bin:$PATH" regexp=PATH.*rbenv create=yes

  - name: add rbenv init
    lineinfile: dest=.bash_profile line=eval\ \"$(rbenv\ init\ -)\" regexp=eval.*rbenv create=yes

  - name: checkout ruby-build as rbenv plugin
    git: repo={{ rubybuild.repo }} dest=.rbenv/plugins/ruby-build accept_hostkey=yes

  - name: check whether a specific version of ruby is installed or not
    shell: .rbenv/bin/rbenv versions | grep {{ ruby.version }} | tr '*' ' ' | sed -e 's/\s\+//' | cut -f1 -d' '
    register: rbenv_version

  - name: install ruby with rbenv
    command: .rbenv/bin/rbenv install {{ ruby.version }}
    when: rbenv_version.stdout != "{{ ruby.version }}"

  - name: set ruby version global
    command: .rbenv/bin/rbenv global {{ ruby.version }}

  - name: update gems
    command: .rbenv/shims/gem update --system

  - name: install bundler gem
    command: .rbenv/shims/gem install bundler --no-ri --no-rdoc

  - name: rbenv rehash
    command: .rbenv/bin/rbenv rehash

実行結果

$ ansible-playbook ocean.yml -i hosts

PLAY [ocean] ******************************************************************

GATHERING FACTS ***************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK: [create iptables] *******************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [update all packages] ***************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install nginx rpm from a remote repo] **********************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install nginx] *********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [rename nginx conf.default to conf.default.original] ********************
ok: [xxx.xxx.xxx.xxx]

TASK: [apply-nginx.conf] ******************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [start and enable nginx] ************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install git] ***********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install memcached] *****************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install epel] **********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install node.js] *******************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install npm] ***********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install ImageMagick] ***************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install libxml] ********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install libxslt] *******************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install ruby] **********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [check out rbenv] *******************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [add .rbenv/bin to $PATH] ***********************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [add rbenv init] ********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [checkout ruby-build as rbenv plugin] ***********************************
changed: [xxx.xxx.xxx.xxx]

TASK: [check whether a specific version of ruby is installed or not] **********
changed: [xxx.xxx.xxx.xxx]

TASK: [install ruby with rbenv] ***********************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [set ruby version global] ***********************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [update gems] ***********************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [install bundler gem] ***************************************************
changed: [xxx.xxx.xxx.xxx]

TASK: [rbenv rehash] **********************************************************
changed: [xxx.xxx.xxx.xxx]

PLAY RECAP ********************************************************************
xxx.xxx.xxx.xxx              : ok=27   changed=25   unreachable=0    failed=0

参考

はじめてのAnsible試行錯誤記録

Pocket
LINEで送る

You may also like...

  • morizotter

    すごい強引に進めてる。