结构2.5:无法向目标主机提供sudo密码

问题描述

我正在尝试学习结构2.5,但我正在苦苦挣扎。我已经阅读了许多页面,试图忽略那些涉及较旧的Fabric版本的页面

我运行以下命令,得到The password submitted to prompt '[sudo] password: ' was rejected.

有人可以指出做错了什么吗?

(f5) albe@vamp398:/srv/file/f5$ fab tt --prompt-for-login-password --prompt-for-sudo-password
Desired 'sudo.password' config value:
Enter login password for use with SSH auth:
Linux vamp398 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
albe
[sudo] password: Sorry,try again.
[sudo] password: Traceback (most recent call last):
  File "/srv/file/f5/bin/fab",line 8,in <module>
    sys.exit(program.run())
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/program.py",line 384,in run
    self.execute()
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/program.py",line 566,in execute
    executor.execute(*self.tasks)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/executor.py",line 129,in execute
    result = call.task(*args,**call.kwargs)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/tasks.py",line 127,in __call__
    result = self.body(*args,**kwargs)
  File "/srv/file/f5/fabfile.py",line 33,in tt
    c.sudo('whoami')
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/context.py",line 173,in sudo
    return self._sudo(runner,command,**kwargs)
  File "/srv/file/f5/lib/python3.5/site-packages/invoke/context.py",line 226,in _sudo
    raise_from(error,None)
  File "<string>",line 2,in raise_from
invoke.exceptions.AuthFailure: The password submitted to prompt '[sudo] password: ' was rejected.
(f5) albe@vamp398:/srv/file/f5$ a
a: command not found
(f5) albe@vamp398:/srv/file/f5$

我的fabfile.py是:

# import getpass
# from fabric import Connection,Config
# from invocations.console import confirm
from fabric import Connection
from invoke import Exit
from fabric import task


# Noworky

# env.user = "albe"
# env.password = "a"
# sudo_pass = getpass.getpass("What's your sudo password?")
# config = Config(overrides={'sudo': {'password': sudo_pass}})
# c = Connection(host='192.168.88.64',user='albe',config=config)
# c = Connection(host='192.168.88.64',user='albe')
# c = Connection(host="albe@192.168.88.64")
    # c.sudo('whoami',hide='stderr')
# c = Connection(host="192.168.88.64",user="albe",connect_kwargs={"password":"a","sudo.password":"a"})

# maybe works

# c = Connection(host="192.168.88.64",connect_kwargs={"password": "a"})

# fab tt --prompt-for-login-password --prompt-for-sudo-password
c = Connection(host='192.168.88.64',user='albe')

@task
def tt(c):
    c.run('uname -a')
    c.run('whoami')
    c.sudo('whoami')

在Ubuntu 16.04上,我像这样设置Fabric 2.5。

cd /srv/file
sudo apt-get install python3-venv
    python3 -m venv f5
    cd f5
    source bin/activate
echo fabric>>requirements.txt
sudo chown -R albe:  /home/albe/.cache
pip3 install --upgrade pip
pip3 install -r requirements.txt
pip3 list

我的点子列表是:

(f5) albe@vamp398:/srv/file/f5$ pip3 list
Package       Version
------------- -------
bcrypt        3.1.7
cffi          1.14.1
cryptography  3.0
fabric        2.5.0
invoke        1.4.1
paramiko      2.7.1
pip           20.2.1
pkg-resources 0.0.0
pycparser     2.20
pynacl        1.4.0
setuptools    20.7.0
six           1.15.0

这些页面似乎最相关。

https://docs.fabfile.org/en/2.5/getting-started.html

https://www.fabfile.org/upgrading.html#the-whole-thing

解决方法

文档中的示例显示了如何使用Connection(),但是它们总是显示不使用@task的原因,因为当您使用@task时,它将自动创建具有连接的context并使用以下值您在命令行中使用。

如果您在c = Connection(...)中放入一些随机值,并在print(c)中使用task,那么您会发现它不使用c = Connection(...)

kwargs = {
    "password": 'random_password',}
ctx = Connection(host='random_host',user='random_user',connect_kwargs=kwargs)    

@task
def one(ctx):
    
    print(ctx)
    
    print('connect_kwargs:',ctx['connect_kwargs'])
    print('user:',ctx['user'])
    
    print('sudo user:',ctx['sudo']['user'])
    print('sudo password:',ctx['sudo']['password'])
        
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

您最终可以在ctx内创建自己的task来替换现有的

def create_ctx():
    password = getpass.getpass("password: ")

    kwargs = {
        "password": password,}

    ctx = Connection(host='192.168.88.64',user='albe',connect_kwargs=kwargs)

    return ctx

@task
def two(ctx):
    
    ctx = create_ctx() # replace original `ctx` with own `ctx`

    print(ctx)
    
    print('connect_kwargs:',ctx['sudo']['password'])
          
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

您还可以替换现有ctx

中的值
@task
def three(ctx0): #,password='xxx',sudo='yyy'):
    
    password = getpass.getpass("password: ")
    sudo = getpass.getpass("sudo: ")

    ctx['user'] = 'pi'
    ctx['connect_kwargs']['password'] = password
    ctx['sudo']['password'] = sudo
    
    print(ctx)
    
    print('connect_kwargs:',ctx['sudo']['password'])
          
    #ctx.run('uname -a')
    #ctx.run('whoami')
    #ctx.sudo('whoami')

如果我使用,代码对我有用

 fab tt --prompt-for-login-password --prompt-for-sudo-password

或首先使用参数(应设置全局值)

 fab --prompt-for-login-password --prompt-for-sudo-password  tt

所以我只能邀请您输入错误的密码。