LoginSignup
9
10

More than 5 years have passed since last update.

踏み台サーバーを超えてfabricを実行する

Posted at

以下の様な状態の場合にtargetサーバーに対してfabricを実行できる様にした時のメモ。

local(fabric実行)--->fumidai(公開鍵認証)--->target(パスワード認証)

参考

環境

  • fabric利用->Ubuntu 14.04.1 LTS
  • fabricの操作対象->EC2(Amazon Linux AMI 2015.03)

fabricのインストール

$easy_install fabric
$ fab --version
Fabric 1.10.2
Paramiko 1.10.1

踏み台サーバーを超えてfabricを実行する

まずtargetサーバーに一発でsshログイン出来るように~/.ssh/configの設定をする。

~/.ssh/config
Host fumidai
  HostName 11.22.33.44
  User fumidai-user
  IdentityFile ~/Keys/id_rsa

Host target
  HostName 111.222.333.444
  User target-user
  Port 22
  ProxyCommand ssh -W %h:%p fumidai

これで$ssh targetとコマンドを実行することでtargetサーバーにsshログイン出来るはず(パスワードは聞かれる)

もし、公開鍵認証でもパスフレーズ入力が必要な場合、ssh-agentを使えば都度入力の手間を省けます。

ssh-agent でパスフレーズの入力を省く

これでfabric的には踏み台の事は気にせず、targetに対して実行すれば良いので次にfabricでtargetサーバーの設定を行います。

fabfile.py
from fabric.api import *

env.use_ssh_config = True
env.user = 'target-user'
env.password = 'target-user-password'

def test():
  sudo('hostname')

これでfabric実行時にsudo実行時にパスワードが必要な場合でも都度入力がいらず、実行が可能です。

$fab -H target test

簡単でっす。

9
10
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
9
10