Vagrant

安裝 nvm 後如何重新啟動 ssh 會話?

  • September 10, 2020

nvm 要求使用者在安裝後註銷/重新登錄以使更改生效。我如何在通過 vagrant 執行的 ansible 任務中允許這一點。這是我嘗試過的:

- 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

更新

我還嘗試了以下方法:

- 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.

如果我在託管機器上手動執行 Install nvm 步驟,它會顯示以下內容已附加到.bashrc

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

正如評論中已經指出的那樣,採購.profile應該足以安裝nvm.

只需用這個任務替換 sshd restart 任務:

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

您可能還想看看這個vagrantfile

如果您需要重新啟動 ssh 伺服器(無論出於何種其他原因),您可以嘗試此Ansible 部落格文章中記錄的這種方法:

- 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

我創建了一個可以與 Ansible 2 一起使用的 Galaxy 角色: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

引用自:https://serverfault.com/questions/747604