代码之家  ›  专栏  ›  技术社区  ›  Am1rr3zA

如何使用受密码保护的ssh私钥运行ansible playbook?

  •  32
  • Am1rr3zA  · 技术社区  · 7 年前

    我为亚马逊创建了一个自动缩放组 EC2 我在创建AMI时添加了公钥 packer ,我可以运行ansible playbook和ssh到主机。

    但是当我像这样运行剧本时,有一个问题 ansible-playbook load.yml 我收到一条消息,我需要写密码

    输入密钥“/用户/XXX/的密码短语。ssh/id\u rsa’:
    输入密码短语 对于键“/用户/XXX/”。ssh/id\u rsa’:
    输入密钥的密码短语 “/用户/XXX/。ssh/id\u rsa’:

    问题是它不接受我的密码(我确信我输入的密码是正确的)。

    我发现我可以用 ask-pass 标志,因此我已将命令更改为 ansible-playbook load.yml --ask-pass 我取得了一些进展,但对于其他任务,它再次要求输入密码,但它不接受我的密码

    [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
    
     [WARNING]: No inventory was parsed, only implicit localhost is available
    
     [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    
    PLAY [localhost] *************************************************************************************************************
    
    TASK [ec2_instance_facts] ****************************************************************************************************
    ok: [localhost]
    
    TASK [add_host] **************************************************************************************************************
    changed: [localhost] => (item=xx.xxx.xx.xxx)
    changed: [localhost] => (item=yy.yyy.yyy.yyy)
    
    PLAY [instances] *************************************************************************************************************
    
    TASK [Copy gatling.conf] *****************************************************************************************************
    ok: [xx.xxx.xx.xxx]
    ok: [yy.yyy.yyy.yyy]
    Enter passphrase for key '/Users/ccc/.ssh/id_rsa': Enter passphrase for key '/Users/ccc/.ssh/id_rsa':
    Enter passphrase for key '/Users/ccc/.ssh/id_rsa':
    Enter passphrase for key '/Users/ccc/.ssh/id_rsa':
    Enter passphrase for key '/Users/ccc/.ssh/id_rsa':
    

    如果我不使用 询问通行证 甚至标记任务 [Copy gatling.conf] 未完成,并抱怨无法访问主机。通过添加标志,此部分通过,但我的下一个任务再次要求通过。

    我应该如何解决这个问题?我做错了什么?

    4 回复  |  直到 7 年前
        1
  •  55
  •   030 Saclyr Barlonium    6 年前

    在ansible中,没有存储受密码短语保护的私钥的选项

    为此,我们需要在ssh代理中添加受密码保护的私钥

    在后台启动ssh代理。

    # eval "$(ssh-agent -s)"
    

    将SSH私钥添加到SSH代理

    # ssh-add ~/.ssh/id_rsa
    

    现在尝试运行ansible playbook和ssh到主机。

        2
  •  18
  •   Am1rr3zA    7 年前

    我通过跑步解决了这个问题 ssh-add 一次,就像没有密码保护一样使用它。

        3
  •  2
  •   dirdi    5 年前

    建立在javeed shakeel的基础上 answer ,我在 .bashrc :

    command -v ansible > /dev/null &&
        alias ansible='ssh-add -l > /dev/null || ssh-add 2> /dev/null && ansible'
    command -v ansible-playbook > /dev/null &&
        alias ansible-playbook='ssh-add -l > /dev/null || ssh-add 2> /dev/null && ansible-playbook'
    

    这将运行 ssh-add 之前 ansible(-playbook) 如果没有向ssh代理添加密钥,则尚未添加密钥。这样做的优点是不需要运行 ssh添加 只有在必要时,才会要求一个人提供密码短语。

        4
  •  0
  •   bbaassssiiee    3 年前

    A. 密码短语 不是一个 暗语 。当ssh要求输入密码时,您将以用户身份登录,密码由您的ssh登录计算机检查。它可能是公司范围的密码(AD、LDAP等)或本地管理员密码。

    密码短语用于加密ssh密钥对的私钥。通常,密钥对是使用 ssh-keygen ,此命令在创建密钥对时要求两次密码短语。这是为了安全,因为当私钥上没有密码短语时,任何拥有文件副本的人都可以冒充您!密码短语是安全性的一个附加因素。

    ssh-agent 是一个创建会话以使用密钥对的程序。

      ##
      ## Install ssh-agent if not already installed.
      ## (change apt-get to yum if you use an RPM-based image)
      ##
      - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    
      ##
      ## Run ssh-agent (on the Ansible control host)
      ##
      - eval $(ssh-agent)
    
      ##
      ## Create the SSH directory and give it the right permissions
      ##
      - mkdir -p ~/.ssh
      - chmod 700 ~/.ssh
    
      ## Create a shell script that will echo the environment variable SSH_PASSPHRASE
      - echo 'echo $SSH_PASSPHRASE' > ~/.ssh/tmp && chmod 700 ~/.ssh/tmp
      ## If ssh-add needs a passphrase, it will read the passphrase from the current
      ## terminal if it was run from a terminal.  If ssh-add does not have a terminal
      ## associated with it but DISPLAY and SSH_ASKPASS are set, it will execute the
      ## program specified by SSH_ASKPASS and open an X11 window to read the
      ## passphrase.  This is particularly useful when calling ssh-add from a
      ## .xsession or related script. Setting DISPLAY=None drops the use of X11.
    
      ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
      ## We're using tr to fix line endings which makes ed25519 keys work
      ## without extra base64 encoding.
      ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
      ##
     
      - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | DISPLAY=None SSH_ASKPASS=~/.ssh/tmp ssh-add -
    
      ##
      ## Use ssh-keyscan to scan the keys of your private server. Replace example.com
      ## with your own domain name. You can copy and repeat that command if you have
      ## more than one server to connect to.
      ##
      - ssh-keyscan example.com >> ~/.ssh/known_hosts
      - chmod 644 ~/.ssh/known_hosts