| 更新: | 2014-05-07 |
|---|---|
| バージョン: | 0.0.1 |
| 作者: | @voluntas |
| URL: | http://voluntas.github.io/ |
| version: | 1.6.0 |
|---|
デプロイを行う場合、サーバ毎に鍵を登録するのが面倒です。 特に GitHub を使っている場合は色々だるくなります。 ということで ssh -A と同じ効果を付与するようにしてみます。
| OS: | CentOS 6.4 64 bit |
|---|
念のため sudo の場合でも動作するように書いている
プロビジョン:
$ ssh-agent zsh $ ssh-add $ ansible-playbook -i ansible_hosts provision.yml
デプロイ:
$ ssh-agent zsh $ ssh-add $ ansible-playbook -i ansible_hosts deploy.yml
ansible ディレクトリ構成:
asnbile/
ansible.cfg
provision.yml
deploy.yml
roles/
core/
tasks/
main.yml
files/
ssh/
ssh_config
ansible.cfg:
[ssh_connection] ssh_args = -o ForwardAgent=yes
provision.yml:
---
- hosts: vagrant
user: vagrant
roles:
- core
deploy.yml:
---
- hosts: vagrant
user: vagrant
tasks:
- git: [email protected]:voluntas/example.git
dest=example
version=master
core/files/ssh/ssh_config:
# 以下の情報を追記した ssh_config を用意してください StrictHostKeyChecking no
core/tasks/main.yml:
- copy: src=ssh/ssh_config dest=/etc/ssh/ssh_config
- yum: name={{ item }} state=latest
with_items:
- git
- shell: grep -q SSH_AUTH_SOCK /etc/sudoers
register: result
failed_when: result.rc not in [0, 1]
- shell: echo 'Defaults env_keep += "SSH_AUTH_SOCK"' >> /etc/sudoers
when: result.rc == 1
- git: [email protected]:voluntas/example.git
dest=example
version=master
ignore_errorsよりfailed_whenを使うほうがお勧めです。
ignore_errorsだとログがfailed ... ignoringと出てfailedの部分が赤字なので一瞬ドキッとしてしまいますが、failed_whenなら赤字のfailedが出ないので利用者にやさしいです。
http://qiita.com/hnakamur/items/af07f2c5e09c05bc44eb
また、http://linux.die.net/man/1/grep によるとエラーの場合終了コードが2になるのですが、failed_whenならエラーとして終了できます。ignore_errorsだと2の場合も継続してしまうのでwhen: result|failedの処理が実行されてしまいます。