安装nvm之后,我怎么才能重新启动ssh会话呢?

nvm要求用户在安装后注销/返回以使更改生效。 我怎么能让这个在通过stream浪汉运行的任务中实现呢? 这是我的尝试:

- name : Install nvm shell: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash failed_when: False register: nvm_installed - name: Kill open ssh sessions - ansible should log back in on next task shell: "ps -ef | grep sshd | grep `whoami` | awk '{print \"kill -9\", $2}' | sh" when: nvm_installed | changed failed_when: false - name : Install Node.js v 4.2.x command : nvm install v4.2 

但是我得到的错误:

 fatal: [default] => SSH Error: ssh_exchange_identification: Connection closed by remote host while connecting to 127.0.0.1:2222 It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue. TASK: [check if rpmforge installed] ******************************************* FATAL: no hosts matched or all hosts have already failed -- aborting 

命令vagrant ssh现在也失败,错误:

 ssh_exchange_identification: Connection closed by remote host 

我基于这里给出的答案 – https://stackoverflow.com/questions/26677064/create-and-use-group-without-restart

我想也许kill命令是杀死sshd守护进程本身?

 ps -ef | grep sshd | grep `whoami` root 2621 1247 0 11:30 ? 00:00:00 sshd: vagrant [priv] vagrant 2625 2621 0 11:30 ? 00:00:00 sshd: vagrant@notty root 3232 1247 4 11:34 ? 00:00:00 sshd: vagrant [priv] vagrant 3235 3232 0 11:34 ? 00:00:00 sshd: vagrant@pts/0 vagrant 3252 3236 0 11:34 pts/0 00:00:00 grep sshd 

UPDATE

我也尝试了以下内容:

 - name : Install nvm shell: "curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash" register: nvm_installed failed_when: False - name : source bash profiles shell : source /home/vagrant/.bashrc when: nvm_installed register: sourced - name : Install Node.js v 4.2.x command : nvm install v4.2 when: sourced 

但得到以下错误:

 TASK: [Install Node.js v 4.2.x] *********************************************** failed: [default] => {"cmd": "nvm install v4.2", "failed": true, "rc": 2} msg: [Errno 2] No such file or directory FATAL: all hosts have already failed -- aborting PLAY RECAP ******************************************************************** to retry, use: --limit @/Users/lukemackenzie/playbook.retry default : ok=10 changed=3 unreachable=0 failed=1 Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again. 

如果我在托pipe机器上手动运行安装nvm步骤,则说明以下内容已被添加到.bashrc

 export NVM_DIR="/home/vagrant/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm 

正如已经在评论中指出的那样.profile应该足够安装nvm

只需用此任务replacesshd restart任务即可:

 - name: Source bash profile. shell: source $HOME/.profile $HOME/.bash_profile 

你可能也想看看这个stream浪文件 。

如果您需要重新启动ssh服务器(无论其他原因),您可以尝试这种Ansible博客文章中logging的方法 :

 - name : Install nvm shell: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash ignore_errors: true register: nvm_installed - name: Kill open ssh sessions - ansible should log back in on next task shell: "ps -ef | grep sshd | grep `whoami` | awk '{print \"kill -9\", $2}' | sh" async: 0 poll: 0 when: nvm_installed | changed - name: waiting for server to come back local_action: wait_for host={{ inventory_hostname }} state=started - name : Install Node.js v 4.2.x command : nvm install v4.2 

我创build了一个将与Ansible 2一起工作的Galaxyangular色: ssh-reconnect

用法:

 - name : Install nvm shell: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash ignore_errors: true notify: - Kill all ssh connections 

以下是最终的成果:

 - name : Install nvm shell: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash register: nvm_installed sudo: False - name: Show nvm_installed output debug: msg: '{{nvm_installed}}' - name : source bash profiles shell: source ~/.bashrc args: executable: /bin/bash register: sourced when : nvm_installed | success sudo: False - name: Show sourced output debug: msg: '{{sourced}}' - name : Install Node.js v 4.2.x shell : '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm install v4.2' when: sourced|success sudo: False environment: NVM_DIR: /home/vagrant/.nvm 

可能不需要包含source命令,因为它看起来可以用非交互式shelllogin,所以.bashrc的内容永远不会被拾取。

另见https://stackoverflow.com/questions/22256884/not-possible-to-source-bashrc-with-ansible