ansible ssh 設定
ansibleはデフォルトではユーザーの
~/.ssh/config
を使わずにssh接続を試みる。
#TBD 要確認(たしかそうだったと思うんだけどーーー)
ssh-addで鍵を登録
対象サーバーによっては、鍵が合わない、鍵の試行回数が多くなりすぎて接続に失敗する、といった状態が発生する
ssh鍵を指定
bash$ ansible-playbook --private-key=~/.ssh/key.pem -i hosts site.yml
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
ファイルにサーバー別の接続設定を書いておく
bash$ ANSIBLE_SSH_ARGS='-F ~/.ssh/config' ansible-playbook .....
inventoryファイル内にホスト別の鍵設定を指定する
この方法は、鍵の位置を全て固定化するので、複数人で共有しづらい
inventory.ymlall:
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
参考