LoginSignup
11
12

More than 5 years have passed since last update.

ansibleでSSHトンネリング(SSHポートフォワーディング)

Last updated at Posted at 2016-09-03

要件

ansibleを使ってローカルPCから踏み台サーバへSSH接続したあとに、SSH接続をしたサーバに対して操作がしたい

ローカルPC -----SSH-----> 踏み台サーバ -----SSH-----> 対象サーバ

実現するには

そのためには以下の二つが必要
・トンネルを掘る
・インベントリの設定

トンネルを掘る

以下のコマンドを実行

ssh -L 10022:対象サーバ:22 ユーザー名@踏み台サーバ

(ポート番号は10022にしたが、空いているローカルPCの任意のポート番号でよい)

SSH接続が完了したら、そのまま接続済の状態にしておく

インベントリの設定

作成したトンネルを利用するようにインベントリを記述する。

ansible/etc/hosts
[local]
127.0.0.1
[local:vars]
ansible_port = 10022

以上で設定が完了

テスト

試しにファイルのコピーを行ってみる

ansible local -m copy -a "src=~/a.txt dest=~/b.txt" --ask-pass

対象サーバへファイルがコピーできることがわかる

参考

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12