ansibleで遊んだ時のメモ
Ansible導入
◆パッケージインストール
yum -y install パッケージ
<対象パッケージ>
bzip2-devel
sqlite-devel
git
patch
gcc
openssl-devel
cd /var/tmp
git clone https://github.com/tagomoris/xbuild.git
xbuild/python-install 2.7.6 /opt/python-2.7
/opt/python-2.7/bin/pip install ansible
◆ユーザ作成
groupadd -g 1500 ansuser
useradd -g ansuser -u 1500 ansuser -d /home/ansuser -s /bin/bash
◆プロファイル修正
vi ~/.bashrc
export PATH=/opt/python-2.7/bin:$PATH
◆SSH鍵作成
・ansibleサーバ→秘密鍵/公開鍵作成
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansuser/.ssh/id_rsa): 鍵の名前
Enter passphrase (empty for no passphrase): ←空白で[Enter]キーを押す
Enter same passphrase again: ←空白で[Enter]キーを押す
※ここでパスワードを指定すると、パスワードが必要な鍵になってしまう
秘密鍵→id_rsa 。 公開鍵→id_rsa.pub
・ansibleサーバ→SSH接続の設定 .ssh/config
vi ~/.ssh/config
Host ホスト名
HostName IP
Port 22
User root
IdentityFile ~/gmo-vps.key
権限変更 chmod 755 ~/.ssh/config
・管理サーバ→公開鍵配置
vi ~/.ssh/authorized_keys
→~.pubをコピー
権限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
・ssh接続確認
ssh ホスト名
◆ansible稼働確認
ansible -i ~/hosts ホスト名 -m ping
Playbookの基礎
◆イベントリファイルにグループを定義
vi ~/hosts
[test-servers]
ホスト名
◆vi test01.yml
- hosts: test-servers
sudo: yes
tasks:
- name: be sure httpd is running and enabled
service: name=httpd state=running enabled=yes
◆構文チェック
ansible-playbook -i ~/hosts test01.yml --syntax-check
playbook: test01.yml
◆タスク一覧
ansible-playbook -i ~/hosts test01.yml --list-tasks
playbook: test01.yml
play #1 (test-servers): TAGS:
be sure httpd is running and enabled TAGS:
◆シュミレーション(実際は実行しないが、どういう挙動になるか確認
ansible-playbook -i ~/hosts test01.yml --check
PLAY [test-servers] ***********************************************************
GATHERING FACTS ***************************************************************
ok: [xxx.xxx.xxx.xxx]
TASK: [be sure httpd is running and enabled] **********************************
changed: [xxx.xxx.xxx.xxx]
PLAY RECAP ********************************************************************
xxx.xxx.xxx.xxx : ok=2 changed=1 unreachable=0 failed=0
◆PlayBookを実行
ansible-playbook -i ~/hosts test.yml
TIPS
ファイルの編集
http://tanksuzuki.com/post/ansible-config-control/
ループ
http://inforno.net/articles/2013/10/15/ansible-tips
外部から値を渡す
http://d.hatena.ne.jp/akishin999/20130818/1376824640
高度な使い方
https://github.com/shkumagai/ansible-doc-ja/blob/master/playbooks2.rst
with_itemsとvarsの組み合わせ
http://qiita.com/hnakamur/items/f8d8ceec3d0e8c4c3a86
file
file: リモートのファイルの設定変更を行う
copy
copy: ローカルのファイルをリモートにコピーするtemplate
template
template: 埋め込んだ変数を置換してファイルをリモートに配置する
templateモジュール自体はファイルコピーとほぼ同じなのだが、変数部分を置換後にコピーするというのが違い。
http://blog.rutake.com/techmemo/2015/06/01/ansible-%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%92%E5%85%83%E3%81%AB%E5%A4%89%E6%95%B0%E3%82%92%E5%9F%8B%E3%82%81%E3%81%A6%E3%82%B3%E3%83%94%E3%83%BC%E3%81%99%E3%82%8Btemplate/
synchronize
synchronize: rsyncでディレクトリごと同期する
http://dev.classmethod.jp/server-side/ansible/ansible-file-modules-intro/
ansibleで実行対象を切り替える方法
http://tdoc.info/blog/2014/05/30/ansible_target_switching.html
マジック変数の一覧
http://qiita.com/h2suzuki/items/15609e0de4a2402803e9