うさぎ好きエンジニアの備忘録

うさぎたちに日々癒されているエンジニアが業務で直面したもの & 個人的な学習メモを残していきます。

fabric2でのsudo実行方法

fabric2でsudoを実行しようと-Iオプションを指定したら失敗したので調べてみたところ、fabric v2になってこのオプションが消え去っていたので対応方法を備忘録としてまとめておく。

課題

fabric v1の時は -I オプションでsudoパスワードを渡すことができたが、fabric v2になってこのオプションが消え去った。

-S,ssh-config

Takes a path to load as a runtime SSH config file. See Loading and using ssh_config files .

-H,—hosts

Takes a comma-separated string listing hostnames against which tasks should be executed, in serial. See Runtime specification of host lists .

-I,—identity

Overrides thekey_filenamevalue in theconnect_kwargsconfig setting (which is read by Connection , and eventually makes its way into Paramiko; see the docstring for Connection for details.) Typically this can be thought of as identical tossh-i, i.e. supplying a specific, runtime private key file. Likessh-i, it builds an iterable of strings and may be given multiple times. Default:[].

—prompt-for-passphrase

Causes Fabric to prompt ‘up front’ for a value to store as theconnect_kwargs.passphraseconfig setting (used by Paramiko to decrypt private key files.) Useful if you do not want to configure such values in on-disk conf files or via shell environment variables.

—prompt-for-login-password

Causes Fabric to prompt ‘up front’ for a value to store as theconnect_kwargs.passwordconfig setting (used by Paramiko when authenticating via passwords and, in some versions, also used for key passphrases.) Useful if you do not want to configure such values in on-disk conf files or via shell environment variables.

sudo パスワードの渡し方

以下のようなタスクを作成。

@task
def sudo_sample(c):
    """rootユーザでwhoamiを実行します"""
    c.sudo("whoami", password=c.config.sudo.password)

タスク実行時に --prompt-for-sudo-password を指定。

$ fab2 --prompt-for-sudo-password -H dev001.ponteru.co.jp sudo_sample