如何使用Ansible重启CentOS 7?

Dom*_*kuš 29 centos ansible centos7

我正在尝试重新启动CentOS 7在VirtualBox上运行的服务器.我用这个任务:

- name: Restart server
  command: /sbin/reboot
  async: 0
  poll: 0
  ignore_errors: true
Run Code Online (Sandbox Code Playgroud)

服务器重新启动,但我收到此错误:

TASK: [common | Restart server] ***********************************************
fatal: [rolcabox] => SSH Error: Shared connection to 127.0.0.1 closed.
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

FATAL: all hosts have already failed -- aborting
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我怎样才能解决这个问题?

Bru*_*e P 39

你可能没有做任何真正的错误,只是/ sbin/reboot正在关闭服务器这么快,服务器正在拆除Ansible使用的SSH连接,然后Ansible自己可以关闭它.因此,Ansible报告错误,因为它看到SSH连接因意外原因而失败.

您可能想要解决的问题是从使用切换/sbin/reboot到使用/sbin/shutdown.shutdown命令允许您传递时间,当与-r开关结合使用时,它将执行重新启动而不是实际关闭.所以你可能想尝试这样的任务:

- name: Restart server
  command: /sbin/shutdown -r +1
  async: 0
  poll: 0
  ignore_errors: true
Run Code Online (Sandbox Code Playgroud)

这将延迟服务器重启1分钟,但这样做应该给Ansible足够的时间来关闭SSH连接本身,从而避免你当前得到的错误.

  • 谢谢,这很棒!只有一个小问题:我有一个```无法解析时间规范:+ 1m```错误,所以我不得不用``+ 1```替换```+ 1m```. (4认同)

小智 12

在重新启动任务之后,您应该有一个local_action等待远程主机完成重新启动的任务,否则,ssh连接将被终止,Playbook也将被终止.


- name: Reboot server
  command: /sbin/reboot

- name: Wait for the server to finish rebooting
  sudo: no
  local_action: wait_for host="{{ inventory_hostname }}" search_regex=OpenSSH port=22 timeout=300
Run Code Online (Sandbox Code Playgroud)

我还写了一篇关于实现类似解决方案的博客文章:https://oguya.github.io/linux/2015/02/22/ansible-reboot-servers/


Sye*_*med 9

- name: restart server
  shell: sleep 2 && shutdown -r now "Ansible updates triggered"
  async: 1
  poll: 0
  become: true
  ignore_errors: true


- name: waiting for the server to come back
  local_action: wait_for host=testcentos state=started delay=30 timeout=300
  sudo: false
Run Code Online (Sandbox Code Playgroud)


小智 7

另一种方案:

- name: reboot host
  command: /usr/bin/systemd-run --on-active=10 /usr/bin/systemctl reboot
  async: 0
  poll: 0

- name: wait for host sshd
  local_action: wait_for host="{{ inventory_hostname }}" search_regex=OpenSSH port=22 timeout=300 delay=30
Run Code Online (Sandbox Code Playgroud)

systemd-run创建"即时"新服务,该服务将systemctl reboot在延迟10秒后开始(--on-active=10). delay=30wait_for添加额外的20秒,以确保主机实际上开始重新启动.


cro*_*vel 6

上述解决方案都不能可靠地为我工作.

发出/sbin/reboot崩溃戏(前ansible完成任务的SSH连接被关闭,它崩溃甚至ignore_errors: true)和/usr/bin/systemd-run --on-active=2 /usr/bin/systemctl reboot2秒后不会重新启动,但20秒一分钟之间的一个随机时间后,这样的延迟是有时不足够,这是不可预测的.

此外,我不想等待几分钟,而云服务器可以在几秒钟内重启.

所以这是我的解决方案:

- name: Reboot the server for kernel update
  shell: ( sleep 3 && /sbin/reboot & )
  async: 0
  poll: 0 

- name: Wait for the server to reboot
  local_action: wait_for host="{{ansible_host}}" delay=15 state=started port="{{ansible_port}}" connect_timeout=10 timeout=180
Run Code Online (Sandbox Code Playgroud)

这就是shell: ( sleep 3 && /sbin/reboot & )诀窍.

( command & )在shell脚本中使用在后台运行程序并将其分离:命令立即成功但在shell被销毁后仍然存在.

Ansible立即得到响应,服务器在3秒后重新启动.


Tel*_*her 5

Ansible正在快速发展,旧的答案对我不起作用.

我发现了两个问题:

  • 建议的重新启动方式可能会在Ansible完成任务之前终止SSH连接.

最好运行: nohup bash -c "sleep 2s && shutdown -r now" &

这将使用sleep&& 启动一个shell shutdown,但不会因为最后一个而等待shell结束&.睡眠将为Ansible任务提供一些时间在重新启动之前结束,并且nohup将保证在任务结束时bash不会被杀死.

  • wait_for模块是不可靠等待SSH服务.

它检测到端口打开,可能是由systemd打开的,但是当下一个任务运行时,SSH仍然没有准备好.

如果您使用的是Ansible 2.3+,则wait_for_connection可靠地运行.

根据我的经验(我使用的是Ansible 2.4),最好的'重启和等待'如下:

- name: Reboot the machine
  shell: nohup bash -c "sleep 2s && shutdown -r now" &

- name: Wait for machine to come back
  wait_for_connection:
    timeout: 240
    delay: 20
Run Code Online (Sandbox Code Playgroud)

我从nohup命令:https://github.com/keithchambers/microservices-playground/blob/master/playbooks/upgrade-packages.yml

我将此消息编辑为:

  • 添加krad的可移植性建议,使用shutdown -r now而不是reboot
  • 加上延迟.如果重启速度很慢,则需要避免Ansible执行下一步
  • 增加超时,120s对于一些慢速BIOS来说太少了.