概要
以下のようなケースはSSH経由でGitHubのソースをダウンロードする必要があります。
- 秘密鍵にパスフレーズが設定されている!
- GitHubのアカウントが2段階認証になっている!
- git操作するたびにパスワード入れるのが面倒!
- 環境自動構築ツールでgit cloneさせたい
今回のブログはケース4に焦点を当てて説明をしたいと思います。筆者の作業環境は
- macosx 10.10
- Ansible 1.7.2
- Vagrant 1.6.5
次、作業ポイントについて説明させていただきます。
手順1. ローカル環境に認証鍵を登録
# sshエージェント起動
ssh-agent
# ssh-add [秘密鍵の格納場所]
ssh-add ~/.ssh/id_rsa
# 登録確認
ssh-agent -l
手順2. VagrantのSSH Agent Forwarding設定を有効にする
Vagrantfileを修正
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
config.ssh.forward_agent = true
仮想環境起動(vagrant up)
手順3. plyabookにgit cloneの記述をする
playbook記述
- name: Copy the code from repository
git: repo={{ repository }} dest=/home/vagrant/repo1 accept_hostkey=yes
sudo: no
ansible-playbook -i host [playbook名] でansible実行