安装 nvm 后,如何让 ansible 重新启动 ssh 会话?

安装 nvm 后,如何让 ansible 重新启动 ssh 会话?

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.

如果我在受管计算机上手动运行安装 nvm 步骤,它会说已附加以下内容.bashrc

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

答案1

正如评论中指出的那样,采购.profile应该足以进行安装nvm

只需用此任务替换 sshd restart 任务:

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

你可能还想看看这个vagrant文​​件

如果你需要重新启动 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

答案2

我创建了一个可以与 Ansible 2 配合使用的 Galaxy 角色:ssh-重新连接

用法:

- 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

答案3

最终效果如下:

- 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

可能不需要包含源命令,因为 ansible 似乎使用非交互式 shell 登录,因此.bashrc永远不会获取其内容。

也可以看看https://stackoverflow.com/questions/22256884/not-possible-to-source-bashrc-with-ansible

相关内容