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连接本身,从而避免你当前得到的错误.
小智 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/
- 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=30
在wait_for
添加额外的20秒,以确保主机实际上开始重新启动.
上述解决方案都不能可靠地为我工作.
发出/sbin/reboot
崩溃戏(前ansible完成任务的SSH连接被关闭,它崩溃甚至ignore_errors: true
)和/usr/bin/systemd-run --on-active=2 /usr/bin/systemctl reboot
2秒后不会重新启动,但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秒后重新启动.
Ansible正在快速发展,旧的答案对我不起作用.
我发现了两个问题:
最好运行: 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
我将此消息编辑为:
归档时间: |
|
查看次数: |
41366 次 |
最近记录: |