Ansibleの多段SSHで、通常のSSHコマンドとControlPathを共有する
通常のSSHコマンドで繋いだ踏み台サーバへのコネクションをAnsibleで共有して使いたい場合に躓いたので書いておきます。
ansible.cfgのデフォルトではControlPathが独自のものになっているので、これを共有するように変更します。
http://docs.ansible.com/ansible/intro_configuration.html#control-path
[localhost] -> [step.example.com] -> [destination.example.com]
### 読み込んでくれる場所にansible.cfgを置く(今回はカレントディレクトリ)
### 読み込み優先順位の参考: http://docs.ansible.com/ansible/intro_configuration.html
$ vim ansible.cfg
[ssh_connection]
control_path = ~/.ssh/controlmaster-%%r@%%h:%%p
ssh_args = -o ControlPersist=15m -F ssh_config -q
### ssh_configを編集 (ansible.cfgのssh_argsで指定されている設定ファイル)
$ vim ssh_config
# MacOS Sierraでの設定変更回避のオプションも含まれます
HostkeyAlgorithms ssh-dss
AddKeysToAgent yes
ForwardAgent yes
# 対象サーバへのssh設定
Host destination.example.com
ProxyCommand ssh step.example.com nc %h %p
# 踏み台サーバへのssh設定#
# ControlPersist 10は通常のsshで使うときの便利のため、切断後10秒でコネクションが消える
Host step.example.com
ControlMaster auto
ControlPersist 10
ControlPath ~/.ssh/controlmaster-%r@%h:%p
### ControlMasterを作成するためにsshする際は、先ほどのssh_configを参照して作る
$ ssh -F ssh_config step.example.com
### 上記sshセッションをオープンしたままansibleを実行すると、その接続を利用してansibleを実行してくれる
$ ansible-playbook -i hosts example.yml -K