ロックとチュウーハイとこりんがるな日々

日々のインプットした事をアウトプットする場所

ansible を使ってさくらVPSにLAMP環境を構築してみる その1

mac からansibleでさくらVPSを構築する 目的はさくらVPSec-cubeを動かす

※なぜec-cubeなのかは気にしない気にしない

今まではサーバ構築にchefを使っていましたが、今回は対象のサーバに対してchefをinstall 出来ない可能性があるので前から試したかったansibleを使ってみる事にしました

ansible を mac に install する

http://docs.ansible.com/intro_installation.html#latest-releases-via-homebrew-mac-osx

公式にhomebrewで入れろと書いてかるので素直に従う

$ brew update
$ brew install ansible

上記でさくっと入りました

※homebrewの設定は語りません

ansible を試す

  • hostsファイルを作る

    実行するサーバのIPを書く 今回はvagrantでサーバを起動しているのでhostsに192.168.33.10を記述

      $ mkdir ec-cube
      $ cd ec-cube
      $ vagranit init centos6.5
      $ vim hosts
      192.168.33.10
    
  • ansibleを実行

    鍵認証を設定していないので通常のパスワード認証を行う

    ansible ホスト -i ホストリストファイル -m 実行モジュール -u ユーザ

    --ask-pass --ask-sudo-pass はパスワード認証を行うときに必要

      $ ansible 192.168.33.10 -i hosts -m ping -u vagrant --ask-pass --ask-sudo-pass
    

    以下のerrorが発生、sshpassなるものを入れろと

      192.168.33.10 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
    

    http://lalyos.github.io/blog/2013/09/30/install-sshpass-on-mac/

    上のページを参考にhomebrewでinstall する

      brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb
    

    もう一度ansibleを実行する

      $ ansible 192.168.33.10 -i hosts -m ping -u vagrant --ask-pass --ask-sudo-pass
      SSH password:
      192.168.33.10 | success >> {
          "changed": false,
          "ping": "pong"
      }
    

    上記のように返ってくればOKです

  • httpdを入れてみる

    以下を実行する

    -m 実行モジュールはyumを指定 -s sudo -a name=[httpdを指定]

      ansible 192.168.33.10 -i hosts -m yum -s -a name=httpd -u vagrant --ask-pass
    

    しばらく待つ

      SSH password:
      192.168.33.10 | success >> {
          "changed": true,
          "msg": "",
          "rc": 0,
          "results": [
              "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: www.ftp.ne.jp\n * epel: ftp.iij.ad.jp\n * extras: www.ftp.ne.jp\n * updates: mirror.awanti.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-31.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-31.el6.centos for package: httpd-2.2.15-31.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-31.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-31.el6.centos.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-31.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package            Arch        Version                      Repository    Size\n================================================================================\nInstalling:\n httpd              x86_64      2.2.15-31.el6.centos         updates      824 k\nInstalling for dependencies:\n apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k\n httpd-tools        x86_64      2.2.15-31.el6.centos         updates       73 k\n mailcap            noarch      2.1.31-2.el6                 base          27 k\n\nTransaction Summary\n================================================================================\nInstall       4 Package(s)\n\nTotal download size: 939 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal                                            48 kB/s | 939 kB     00:19     \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         1/4 \n\r  Installing : httpd-tools-2.2.15-31.el6.centos.x86_64                      2/4 \n\r  Installing : mailcap-2.1.31-2.el6.noarch                                  3/4 \n\r  Installing : httpd-2.2.15-31.el6.centos.x86_64                            4/4 \n\r  Verifying  : httpd-2.2.15-31.el6.centos.x86_64                            1/4 \n\r  Verifying  : mailcap-2.1.31-2.el6.noarch                                  2/4 \n\r  Verifying  : httpd-tools-2.2.15-31.el6.centos.x86_64                      3/4 \n\r  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         4/4 \n\nInstalled:\n  httpd.x86_64 0:2.2.15-31.el6.centos                                           \n\nDependency Installed:\n  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        \n  httpd-tools.x86_64 0:2.2.15-31.el6.centos                                     \n  mailcap.noarch 0:2.1.31-2.el6                                                 \n\nComplete!\n"
          ]
      }
    

    実行結果が反映される

とりあえずここまで、次回はplaybookを作っていきます

使ってみた感想

今までサーバ構築に使っていたchef(chef-solo)に比べたら最初に用意するもの、学習すべき事がすくないと 感じました、chef-soloの実行にはknife-soloを使っていたのですが、それに相当する事が標準で出来るのも ありがたいなと感じました