読者です 読者をやめる 読者になる 読者になる

みんなのちからになりたい

コピペでブログラムつくっていきたい

WindowsでAnsibleする

ansible windows ruby vagrant

Ansibleをやっていきたい

Vagrant を使ってサーバーを準備する

言葉があってるかわからないけど、なにやらサーバ側からクライアント側へpushして構成を作成するものらしいので、サーバとクライアントの2つを作成する

ansibletestとかいうフォルダを作成してそこをカレントにしてinitしてVagrantfileを以下のように変更した

    config.vm.define :server do |node|
      node.vm.box = "centos6"
      node.vm.network :forwarded_port, guest: 22, host: 2001, id: "ssh"
      node.vm.network :private_network, ip: "192.168.33.11"
    end
         
    config.vm.define :client do |node|
      node.vm.box = "centos6"
      node.vm.network :forwarded_port, guest: 22, host: 2002, id: "ssh"
      node.vm.network :forwarded_port, guest: 80, host: 8000, id: "http"
      node.vm.network :private_network, ip: "192.168.33.12"
    end

こうすることで2つ立ち上がるらしい

やってみる

    vagrant up
    Bringing machine 'server' up with 'virtualbox' provider...
    Bringing machine 'client' up with 'virtualbox' provider...
    [server] Importing base box 'centos6'...
    [server] Matching MAC address for NAT networking...
    [server] Setting the name of the VM...
    [server] Clearing any previously set forwarded ports...
    [server] Creating shared folders metadata...
    [server] Clearing any previously set network interfaces...
    [server] Preparing network interfaces based on configuration...
    [server] Forwarding ports...
    [server] -- 22 => 2001 (adapter 1)
    [server] Booting VM...
    [server] Waiting for VM to boot. This can take a few minutes.
    [server] VM booted and ready for use!
    [server] Configuring and enabling network interfaces...
    [server] Mounting shared folders...
    [server] -- /vagrant
    [client] Importing base box 'centos6'...
    [client] Matching MAC address for NAT networking...
    [client] Setting the name of the VM...
    [client] Clearing any previously set forwarded ports...
    Vagrant cannot forward the specified ports on this VM, since they
    would collide with some other application that is already listening
    on these ports. The forwarded port to 2002 is already in use
    on the host machine.
    
    To fix this, modify your current projects Vagrantfile to use another
    port. Example, where '1234' would be replaced by a unique host port:
    
      config.vm.network :forwarded_port, guest: 22, host: 1234
    
    Sometimes, Vagrant will attempt to auto-correct this for you. In this
    case, Vagrant was unable to. This is usually because the guest machine
    is in a state which doesn't allow modifying port forwarding.

まじ2つ立ち上げにいってる、すごい

ただポートが使われてるって出たのでhaltして両方適当なのに変えて再度チャレンジ

    vagrant up
    Bringing machine 'server' up with 'virtualbox' provider...
    Bringing machine 'client' up with 'virtualbox' provider...
    [server] Setting the name of the VM...
    [server] Clearing any previously set forwarded ports...
    [server] Creating shared folders metadata...
    [server] Clearing any previously set network interfaces...
    [server] Preparing network interfaces based on configuration...
    [server] Forwarding ports...
    [server] -- 22 => 3001 (adapter 1)
    [server] Booting VM...
    [server] Waiting for VM to boot. This can take a few minutes.
    [server] VM booted and ready for use!
    [server] Configuring and enabling network interfaces...
    [server] Mounting shared folders...
    [server] -- /vagrant
    [client] Setting the name of the VM...
    [client] Clearing any previously set forwarded ports...
    [client] Creating shared folders metadata...
    [client] Clearing any previously set network interfaces...
    [client] Preparing network interfaces based on configuration...
    [client] Forwarding ports...
    [client] -- 22 => 3002 (adapter 1)
    [client] -- 80 => 8000 (adapter 1)
    [client] Booting VM...
    [client] Waiting for VM to boot. This can take a few minutes.
    [client] VM booted and ready for use!
    [client] Configuring and enabling network interfaces...
    [client] Mounting shared folders...
    [client] -- /vagrant

今度は立ち上がった

statusを確認してみる

    vagrant status
    Current machine states:

    server                    running (virtualbox)
    client                    running (virtualbox)

動いてる、まじすごいなこれ…

"起動後、Ansible で node1 から node2 へ ssh するため、Vagrant 用の秘密鍵をコピーする"

って書いてあるんでやってみようと思ったんだけど、scp入れてない上にWindowsでやってるからsshPuTTYなんだけどこの先大丈夫なんだろうか…

とりあえずファイルは作っておく

    vagrant ssh-config server > ssh_config
    vagrant ssh-config client >> ssh_config

これでカレントにssh_configができてログイン情報が書き込まれる

先で詰まるのもいやなので、winscp入れてserverのほうに秘密鍵をid_rsaにrenameして入れておく

Ansible をインストールする

serverにsshでログインしてAnsibleをインストールする

書いてあるとおりに写経する

    [vagrant@localhost ~]$ sudo rpm -ivh http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
    Retrieving http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
    warning: /var/tmp/rpm-tmp.S9CzbY: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
    Preparing...                ########################################### [100%]
       1:epel-release           ########################################### [100%]
    [vagrant@localhost ~]$ sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
    [vagrant@localhost ~]$ sudo yum -y install ansible
    Loaded plugins: fastestmirror
    Determining fastest mirrors
    epel/metalink                                            | 5.6 kB     00:00
     * base: www.ftp.ne.jp
     * epel: ftp.riken.jp
     * extras: www.ftp.ne.jp
     * updates: www.ftp.ne.jp
    base                                                     | 3.7 kB     00:00
    base/primary_db                                          | 4.4 MB     00:00
    epel                                                     | 4.2 kB     00:00
    epel/primary_db                                          | 5.4 MB     00:02
    extras                                                   | 3.4 kB     00:00
    extras/primary_db                                        |  18 kB     00:00
    updates                                                  | 3.4 kB     00:00
    updates/primary_db                                       | 3.8 MB     00:00
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package ansible.noarch 0:1.2.2-1.el6 will be installed
    --> Processing Dependency: python-paramiko for package: ansible-1.2.2-1.el6.noarch
    --> Processing Dependency: python-jinja2 for package: ansible-1.2.2-1.el6.noarch
    --> Processing Dependency: PyYAML for package: ansible-1.2.2-1.el6.noarch
    --> Running transaction check
    ---> Package PyYAML.x86_64 0:3.10-3.el6 will be installed
    --> Processing Dependency: libyaml-0.so.2()(64bit) for package: PyYAML-3.10-3.el6.x86_64
    ---> Package python-jinja2.x86_64 0:2.2.1-1.el6 will be installed
    --> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.2.1-1.el6.x86_64
    ---> Package python-paramiko.noarch 0:1.7.5-2.1.el6 will be installed
    --> Processing Dependency: python-crypto >= 1.9 for package: python-paramiko-1.7.5-2.1.el6.noarch
    --> Running transaction check
    ---> Package libyaml.x86_64 0:0.1.3-1.el6 will be installed
    ---> Package python-babel.noarch 0:0.9.4-5.1.el6 will be installed
    ---> Package python-crypto.x86_64 0:2.0.1-22.el6 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

      ================================================================================
     Package                Arch          Version                 Repository   Size
      ================================================================================
    Installing:
     ansible                noarch        1.2.2-1.el6             epel        520 k
    Installing for dependencies:
     PyYAML                 x86_64        3.10-3.el6              epel        157 k
     libyaml                x86_64        0.1.3-1.el6             epel         52 k
     python-babel           noarch        0.9.4-5.1.el6           base        1.4 M
     python-crypto          x86_64        2.0.1-22.el6            base        159 k
     python-jinja2          x86_64        2.2.1-1.el6             base        465 k
     python-paramiko        noarch        1.7.5-2.1.el6           base        728 k

    Transaction Summary
      ================================================================================
    Install       7 Package(s)

    Total download size: 3.5 M
    Installed size: 18 M
    Downloading Packages:
    (1/7): PyYAML-3.10-3.el6.x86_64.rpm                      | 157 kB     00:00
    (2/7): ansible-1.2.2-1.el6.noarch.rpm                    | 520 kB     00:00
    (3/7): libyaml-0.1.3-1.el6.x86_64.rpm                    |  52 kB     00:00
    (4/7): python-babel-0.9.4-5.1.el6.noarch.rpm             | 1.4 MB     00:00
    (5/7): python-crypto-2.0.1-22.el6.x86_64.rpm             | 159 kB     00:00
    (6/7): python-jinja2-2.2.1-1.el6.x86_64.rpm              | 465 kB     00:00
    (7/7): python-paramiko-1.7.5-2.1.el6.noarch.rpm          | 728 kB     00:00
      --------------------------------------------------------------------------------
    Total                                           1.6 MB/s | 3.5 MB     00:02
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
    Warning: RPMDB altered outside of yum.
      Installing : python-crypto-2.0.1-22.el6.x86_64                            1/7
      Installing : python-paramiko-1.7.5-2.1.el6.noarch                         2/7
      Installing : libyaml-0.1.3-1.el6.x86_64                                   3/7
      Installing : PyYAML-3.10-3.el6.x86_64                                     4/7
      Installing : python-babel-0.9.4-5.1.el6.noarch                            5/7
      Installing : python-jinja2-2.2.1-1.el6.x86_64                             6/7
      Installing : ansible-1.2.2-1.el6.noarch                                   7/7
      Verifying  : python-babel-0.9.4-5.1.el6.noarch                            1/7
      Verifying  : python-jinja2-2.2.1-1.el6.x86_64                             2/7
      Verifying  : PyYAML-3.10-3.el6.x86_64                                     3/7
      Verifying  : libyaml-0.1.3-1.el6.x86_64                                   4/7
      Verifying  : python-crypto-2.0.1-22.el6.x86_64                            5/7
      Verifying  : python-paramiko-1.7.5-2.1.el6.noarch                         6/7
      Verifying  : ansible-1.2.2-1.el6.noarch                                   7/7

    Installed:
      ansible.noarch 0:1.2.2-1.el6

    Dependency Installed:
      PyYAML.x86_64 0:3.10-3.el6            libyaml.x86_64 0:0.1.3-1.el6
      python-babel.noarch 0:0.9.4-5.1.el6   python-crypto.x86_64 0:2.0.1-22.el6
      python-jinja2.x86_64 0:2.2.1-1.el6    python-paramiko.noarch 0:1.7.5-2.1.el6

    Complete!

成功した気がする

Ansible を使うための準備

"vagrant ssh を使うと vagrant ユーザーでログインし"って書いてあるけどvagrant sshできないんだけどどうなるの…

Ansible の疎通確認

写経する

    [vagrant@localhost ~]$ ansible 192.168.33.12 -m ping
    No hosts matched
    [vagrant@localhost ~]$ echo 192.168.33.12 > hosts
    [vagrant@localhost ~]$ ansible -i hosts 192.168.33.12 -m ping

    paramiko: The authenticity of host '192.168.33.12' can't be established.
    The ssh-rsa key fingerprint is f03f045136b591c7fa475a49bcfdfe40.
    Are you sure you want to continue connecting (yes/no)?
    y
    192.168.33.12 | success >> {
        "changed": false,
        "ping": "pong"
    }

うごいた、すごい

    [vagrant@localhost ~]$ ansible -i hosts 192.168.33.12 -a 'uname -r'
    192.168.33.12 | success | rc=0 >>
    2.6.32-358.el6.x86_64

動いてます

rubyを入れる

とりあえずrubyだけ入れた環境だけほしいので、この状態でcommitしておきたい

lsとかのaliasを.bashrcに突っ込んでcommitする

    alias la='ls -La'
    alias lal='ls -Lal'

これだけとりあえず入れてsandbox onしてcommitしておく

"8.1. Ruby のインストール"をベースにやっていく

Ruby のコンパイルに必要なものをインストール
    [vagrant@localhost ~]$ sudo yum -y install git gcc gcc-c++ make patch readline-devel zlib-devel libyaml-devel
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: www.ftp.ne.jp
     * epel: ftp.riken.jp
     * extras: www.ftp.ne.jp
     * updates: www.ftp.ne.jp
    Setting up Install Process
    Package gcc-4.4.7-3.el6.x86_64 already installed and latest version
    Package gcc-c++-4.4.7-3.el6.x86_64 already installed and latest version
    Package 1:make-3.81-20.el6.x86_64 already installed and latest version
    Package readline-devel-6.0-4.el6.x86_64 already installed and latest version
    Package zlib-devel-1.2.3-29.el6.x86_64 already installed and latest version
    Resolving Dependencies
    --> Running transaction check
    ---> Package git.x86_64 0:1.7.1-3.el6_4.1 will be installed
    --> Processing Dependency: perl-Git = 1.7.1-3.el6_4.1 for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: rsync for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: perl(Git) for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: perl(Error) for package: git-1.7.1-3.el6_4.1.x86_64
    ---> Package libyaml-devel.x86_64 0:0.1.3-1.el6 will be installed
    ---> Package patch.x86_64 0:2.6-6.el6 will be installed
    --> Running transaction check
    ---> Package perl-Error.noarch 1:0.17015-4.el6 will be installed
    ---> Package perl-Git.noarch 0:1.7.1-3.el6_4.1 will be installed
    ---> Package rsync.x86_64 0:3.0.6-9.el6 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
     Package              Arch          Version                Repository      Size
    ================================================================================
    Installing:
     git                  x86_64        1.7.1-3.el6_4.1        updates        4.6 M
     libyaml-devel        x86_64        0.1.3-1.el6            epel            84 k
     patch                x86_64        2.6-6.el6              base            90 k
    Installing for dependencies:
     perl-Error           noarch        1:0.17015-4.el6        base            29 k
     perl-Git             noarch        1.7.1-3.el6_4.1        updates         28 k
     rsync                x86_64        3.0.6-9.el6            base           334 k

    Transaction Summary
    ================================================================================
    Install       6 Package(s)

    Total download size: 5.2 M
    Installed size: 16 M
    Downloading Packages:
    (1/6): git-1.7.1-3.el6_4.1.x86_64.rpm                    | 4.6 MB     00:01
    (2/6): libyaml-devel-0.1.3-1.el6.x86_64.rpm              |  84 kB     00:00
    (3/6): patch-2.6-6.el6.x86_64.rpm                        |  90 kB     00:00
    (4/6): perl-Error-0.17015-4.el6.noarch.rpm               |  29 kB     00:00
    (5/6): perl-Git-1.7.1-3.el6_4.1.noarch.rpm               |  28 kB     00:00
    (6/6): rsync-3.0.6-9.el6.x86_64.rpm                      | 334 kB     00:00
    --------------------------------------------------------------------------------
    Total                                           309 kB/s | 5.2 MB     00:17
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : 1:perl-Error-0.17015-4.el6.noarch                            1/6
      Installing : rsync-3.0.6-9.el6.x86_64                                     2/6
      Installing : perl-Git-1.7.1-3.el6_4.1.noarch                              3/6
      Installing : git-1.7.1-3.el6_4.1.x86_64                                   4/6
      Installing : patch-2.6-6.el6.x86_64                                       5/6
      Installing : libyaml-devel-0.1.3-1.el6.x86_64                             6/6
      Verifying  : 1:perl-Error-0.17015-4.el6.noarch                            1/6
      Verifying  : libyaml-devel-0.1.3-1.el6.x86_64                             2/6
      Verifying  : perl-Git-1.7.1-3.el6_4.1.noarch                              3/6
      Verifying  : rsync-3.0.6-9.el6.x86_64                                     4/6
      Verifying  : patch-2.6-6.el6.x86_64                                       5/6
      Verifying  : git-1.7.1-3.el6_4.1.x86_64                                   6/6

    Installed:
      git.x86_64 0:1.7.1-3.el6_4.1        libyaml-devel.x86_64 0:0.1.3-1.el6
      patch.x86_64 0:2.6-6.el6

    Dependency Installed:
      perl-Error.noarch 1:0.17015-4.el6      perl-Git.noarch 0:1.7.1-3.el6_4.1
      rsync.x86_64 0:3.0.6-9.el6

    Complete!
後々のこと考えてrbenvで管理していきたいのでrbenv使う方向で

ここみてやっていく

ruby-buildのインストール
    [vagrant@localhost ~]$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    Initialized empty Git repository in /home/vagrant/.rbenv/plugins/ruby-build/.git/
    remote: Counting objects: 2004, done.
    remote: Compressing objects: 100% (1181/1181), done.
    remote: Total 2004 (delta 834), reused 1792 (delta 639)
    Receiving objects: 100% (2004/2004), 275.78 KiB | 20 KiB/s, done.
    Resolving deltas: 100% (834/834), done.
    [vagrant@localhost ruby-build]$ cd ~/.rbenv/plugins/ruby-build
    [vagrant@localhost ruby-build]$ sudo ./install.sh
    Installed ruby-build at /usr/local
rbenvのインストール
    [vagrant@localhost ~]$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
    fatal: destination path '/home/vagrant/.rbenv' already exists and is not an empty directory.

もしかしてruby-buildってrbenvのpluginだから先にrbenv入れないとダメなんじゃ…

こういうときのためのsandboxなのでrollbackしてrbenvから先に入れていく

今度はrbenvから先にインストール
    [vagrant@localhost ~]$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
    Initialized empty Git repository in /home/vagrant/.rbenv/.git/
    remote: Counting objects: 1642, done.
    remote: Compressing objects: 100% (673/673), done.
    remote: Total 1642 (delta 1054), reused 1462 (delta 937)
    Receiving objects: 100% (1642/1642), 238.83 KiB | 133 KiB/s, done.
    Resolving deltas: 100% (1054/1054), done.
    [vagrant@localhost ~]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
    [vagrant@localhost ~]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    [vagrant@localhost ~]$ source ~/.bash_profile
    [vagrant@localhost ~]$ rbenv -h
    rbenv 0.4.0-52-g1cc7536
    Usage: rbenv <command> [<args>]

    Some useful rbenv commands are:
       commands    List all available rbenv commands
       local       Set or show the local application-specific Ruby version
       global      Set or show the global Ruby version
       shell       Set or show the shell-specific Ruby version
       rehash      Rehash rbenv shims (run this after installing executables)
       version     Show the current Ruby version and its origin
       versions    List all Ruby versions available to rbenv
       which       Display the full path to an executable
       whence      List all Ruby versions that contain the given executable

    See `rbenv help <command>' for information on a specific command.
    For full documentation, see: https://github.com/sstephenson/rbenv#readme

ここまでOK

再度ruby-buildのインストール
    [vagrant@localhost ~]$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    Initialized empty Git repository in /home/vagrant/.rbenv/plugins/ruby-build/.git/
    remote: Counting objects: 2004, done.
    remote: Compressing objects: 100% (1181/1181), done.
    remote: Total 2004 (delta 834), reused 1792 (delta 639)
    Receiving objects: 100% (2004/2004), 275.78 KiB | 20 KiB/s, done.
    Resolving deltas: 100% (834/834), done.
    [vagrant@localhost ~]$ cd ~/.rbenv/plugins/ruby-build
    [vagrant@localhost ruby-build]$ sudo ./install.sh
    Installed ruby-build at /usr/local

OKだと思う…

ruby2.0を入れる

listみると2.0.0-p247が最新っぽいのでそれを入れる

    [vagrant@localhost ruby-build]$ rbenv install -l
    Available versions:
      2.0.0-dev
      2.0.0-p0
      2.0.0-p195
      2.0.0-p247
      2.0.0-preview1
      2.0.0-preview2
      2.0.0-rc1
      2.0.0-rc2
      2.1.0-dev
    [vagrant@localhost ruby-build]$ rbenv install 2.0.0-p247
    Downloading ruby-2.0.0-p247.tar.gz...
    -> ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
    Installing ruby-2.0.0-p247...
    Installed ruby-2.0.0-p247 to /home/vagrant/.rbenv/versions/2.0.0-p247
    [vagrant@localhost ruby-build]$ rbenv rehash
    [vagrant@localhost ruby-build]$ rbenv versions
    * system (set by /home/vagrant/.rbenv/version)
      2.0.0-p247
    [vagrant@localhost ruby-build]$ ruby -v
    ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
    [vagrant@localhost ruby-build]$ rbenv global 2.0.0-p247
    [vagrant@localhost ruby-build]$ ruby -v
    ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
    [vagrant@localhost ruby-build]$ rbenv versions
      system
    * 2.0.0-p247 (set by /home/vagrant/.rbenv/version)

OK、できた

gemをupdateしてbundler入れてcommitしておく

    [vagrant@localhost ruby-build]$ gem -v
    2.0.3
    [vagrant@localhost ruby-build]$ gem update --system
      ~
    [vagrant@localhost ruby-build]$ gem -v
    2.0.6
    [vagrant@localhost ruby-build]$ gem install bundler
    Fetching: bundler-1.3.5.gem (100%)
    Successfully installed bundler-1.3.5
    Parsing documentation for bundler-1.3.5
    Installing ri documentation for bundler-1.3.5
    1 gem installed

ていうか…

ここまでの流れをAnsibleでやりたいってことなので、元に戻って続きをruby入れるってことでやってみる

次回はPlaybookを書くとこからやっていきたい