Ansibleで環境構築(2) - Ansibleを使ってみる
今回はAnsibleを軽く使ってみるところまで。
先にPython環境を構築する
後でバージョンが楽に切り替えられるようにpyenv
を入れてみる
$ brew install pyenv
$ vi ~/.zshrc.hentai-kun
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"
fi
$ source .zshrc
Pythonをインストール
$ pyenv install -l
...
3.4.0
バージョン2系統では2.7.6
が最新のよう。とりあえず最新版を入れてみる。
$ pyenv install 2.7.6
$ pyenv rehash
$ pyenv global 2.7.6
$ python -V
Python 2.7.6
Ansibleを入れる
$ pip install ansible
$ ansible --version
ansible 1.6.1
Vagrantと疎通確認する
インベントリーファイルを作る
$ vi hosts
[vagrant]
192.168.33.10
Ansibleは予めアクセス許可したアドレスにしか接続できないため、hosts
というファイルのを作ってVagrantのサーバーを追加する。名前は何でもいいらしい。
Ping
$ ansible -i hosts 192.168.33.10 -m ping
192.168.33.10 | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
しかしエラーになった。言われた通りに-vvv
付けてkwsk見てみる。
$ ansible -i hosts 192.168.33.10 -m ping -vvv
<192.168.33.10> ESTABLISH CONNECTION FOR USER: hentai-kun
<192.168.33.10> REMOTE_MODULE ping
<192.168.33.10> EXEC ['ssh', '-C', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/hentai-kun/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', '192.168.33.10', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1400422441.11-165247279783410 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1400422441.11-165247279783410 && echo $HOME/.ansible/tmp/ansible-tmp-1400422441.11-165247279783410'"]
192.168.33.10 | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
<192.168.33.10> ESTABLISH CONNECTION FOR USER: hentai-kun
あれっ、接続ユーザーがvagrantじゃないですね……
SSHの設定にVagrant接続情報を追加
いつもvagrant ssh
で接続してたので気づかなかった。
$ vi ~/.ssh/config
Host 192.168.33.*
IdentityFile ~/.vagrant.d/insecure_private_key
User vagrant
Pingリトライ
$ ansible -i hosts 192.168.33.10 -m ping
192.168.33.10 | success >> {
"changed": false,
"ping": "pong"
}
今度は成功。
簡単なplaybookを書いてみる
$ vi playbook.yml
- hosts: vagrant
sudo: true
tasks:
- apt: pkg=git update_cache=yes state=latest
git
をインストールするだけ。
実行してみる
$ ansible-playbook playbook.yml
PLAY [vagrant] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.33.10]
TASK: [apt pkg=git update_cache=yes state=latest] *****************************
changed: [192.168.33.10]
PLAY RECAP ********************************************************************
192.168.33.10 : ok=2 changed=1 unreachable=0 failed=0
成功。
$ vagrant ssh
$ git --version
git version 1.7.9.5
ちゃんとインストールされてた!
冪等性の確認のため再実行してみる
冪等性というのは、
大雑把に言って、ある操作を1回行っても複数回行っても結果が同じであることをいう概念である。
と、Wikipedia先生がおっしゃっていました。たいへん大雑把にわかりやすい。
$ ansible-playbook playbook.yml
PLAY [vagrant] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.33.10]
TASK: [apt pkg=git update_cache=yes state=latest] *****************************
ok: [192.168.33.10]
PLAY RECAP ********************************************************************
192.168.33.10 : ok=2 changed=0 unreachable=0 failed=0
確かにエラーにならない。
Ansibleすごい。