generated at
ansible ssh 設定
ansibleはデフォルトではユーザーの ~/.ssh/config を使わずにssh接続を試みる。
#TBD 要確認(たしかそうだったと思うんだけどーーー)

ssh-addで鍵を登録
ssh-addssh-agentに登録した鍵は使ってくれる
対象サーバーによっては、鍵が合わない、鍵の試行回数が多くなりすぎて接続に失敗する、といった状態が発生する

ssh鍵を指定
鍵が対象ホスト群で共通であれば、鍵をansible-playbookコマンドに指定することで解決する
bash
$ ansible-playbook --private-key=~/.ssh/key.pem -i hosts site.yml

ssh config を明示的に指定
sshのオプションに以下のように明示的にssh_configを渡せる
bash
$ ssh -F ~/.ssh/config hostname
そこで、 ansible.cfg に以下のように専用のssh_configを指定する
ansible.cfg
[ssh_connection] ssh_args = -F ./path/to/ssh_config
ansible.cfg の置き場所はいくつかあります
この ssh_config ファイルにサーバー別の接続設定を書いておく
もし ansible.cfg を書きたくない場合は、環境変数 ANSIBLE_SSH_ARGSでも指定できる
bash
$ ANSIBLE_SSH_ARGS='-F ~/.ssh/config' ansible-playbook .....

inventoryファイル内にホスト別の鍵設定を指定する
この方法は、鍵の位置を全て固定化するので、複数人で共有しづらい
inventory.yml
all: hosts: mail.example.com: ansible_ssh_private_key_file: ~/.ssh/key1 children: webservers: hosts: foo.example.com: ansible_ssh_private_key_file: ~/.ssh/key2 bar.example.com: ansible_ssh_private_key_file: ~/.ssh/key3

inventory.ini
[hosts] mail.example.com ansible_ssh_private_key_file=~/.ssh/key1 [webservers:children] foo.example.com ansible_ssh_private_key_file=~/.ssh/key2 bar.example.com ansible_ssh_private_key_file=~/.ssh/key3

参考