python - 从 fabric 调用/etc/sudoers 命令

标签 python fabric sudoers

在我的 /etc/sudoers 我有:

# Allow the picky user to restart its own gunicorn process
picky ALL=(ALL) NOPASSWD: /usr/bin/supervisorctl restart picky

我确实可以从挑剔的用户那里运行这个命令:

$ su picky
$ sudo supervisorctl restart picky

在我的 fabfile.py 中有以下内容:

from fabric.api import sudo

def restart():
    sudo("supervisorctl restart picky")

但是,当我运行我的结构文件时,它仍然提示输入密码:

[picky@myhost] sudo: supervisorctl restart picky
[picky@myhost] out: sudo password: 

如何在 Fabric 中运行 sudo 命令而不需要提供密码?

编辑:

我注意到这个有效:

from fabric.api import run

def restart():
    run("sudo supervisorctl restart picky")

最佳答案

https://stackoverflow.com/questions/3737003/can-i-prevent-fabric-from-prompting-me-for-a-sudo-password 中所述,当您使用 fabric sudo 命令时,该命令将发送到 shell。执行的实际命令包含对 /bin/bash 的调用,这就是它与您的 sudoer 条目不匹配的原因。

要解决这个问题,只需将 shell=False 添加到您的调用中:

from fabric.api import sudo

def restart():
    sudo("supervisorctl restart picky", shell=False)

您可能必须将完整路径添加到 supervisorctl

关于python - 从 fabric 调用/etc/sudoers 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602232/

相关文章:

python - 如何在 MapReduce Python Streaming 中对多个字段进行排序?

python - 如何将 seaborn 轴添加到带有子图的 matplotlib 图?

python - 在运行时加载模块可以正常工作,但在结构下除外

linux - 如何在没有sudo的情况下执行 'iftop'

centos - 无法以 Root 身份打开 session

python - 如何使用 .csv 文件中同一列中的值进行操作

python - 在强化学习中标准化奖励以产生返回

python - 如何让 Fabric 忽略 env.hosts 列表中的离线主机?

python - 我可以使用 Fabric 在 vi​​rtualenv 上工作并运行 Python 应用程序吗?

linux - 如何配置访问 usr/bin/inlinux 中的 whoami