Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reboot CentOS 7 with Ansible?

I'm trying to reboot server running CentOS 7 on VirtualBox. I use this task:

- name: Restart server   command: /sbin/reboot   async: 0   poll: 0   ignore_errors: true 

Server is rebooted, but I get this error:

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 

What am I doing wrong? How can I fix this?

like image 451
Domen Blenkuš Avatar asked Apr 29 '15 22:04

Domen Blenkuš


People also ask

How do I reboot CentOS?

The quickest method to reboot a Linux system is by using the keyboard shortcut. Whenever you need to reboot your CentOS system, simply press Ctrl+Alt+Del.

How do I restart Ansible?

Use systemctl restart ansible-tower to restart services on clustered environments instead. Also you must restart each cluster node for certain changes to persist as opposed to a single node for a localhost install.

How to reboot a Linux machine using Ansible?

Introduction: You can reboot a Linux or Unix based machine, wait for it to go down (say for kernel update), come back up, and respond to commands. You can use either command or shell module to reboot the Linux server when kernel updated. However, now we have a reboot module to reboot a machine using Ansible. I tested this module with:

What is wait_for_connection in Ansible?

If you are a network engineer that has operational Ansible Playbooks that need to reboot devices or take them offline, this module will help you make more programmatic playbooks to handle disconnects. By leveraging wait_for_connection network automation playbooks can look and behave more like playbooks for Linux or Windows hosts.

How to update RedHat family systems using Ansible playbook?

Recently I’ve spent some time tuning ansible playbook to develop a nice way to update my RedHat family systems. The playbook does the following: First, it checks if there are any packages to be updated and displays them. Next, it starts the update.

How to execute a program in the background in Ansible?

Using ( command & ) in shell script runs a program in the background and detaches it: the command succeed immediately but persists after the shell is destroyed. Ansible get its response immediately and the server reboots 3 seconds later.


2 Answers

You're likely not doing anything truly wrong, it's just that /sbin/reboot is shutting down the server so quickly that the server is tearing down the SSH connection used by Ansible before Ansible itself can close it. As a result Ansible is reporting an error because it sees the SSH connection failing for an unexpected reason.

What you might want to do to get around this is to switch from using /sbin/reboot to using /sbin/shutdown instead. The shutdown command lets you pass a time, and when combined with the -r switch it will perform a reboot rather than actually shutting down. So you might want to try a task like this:

- name: Restart server   command: /sbin/shutdown -r +1   async: 0   poll: 0   ignore_errors: true 

This will delay the server reboot for 1 minute, but in doing so it should give Ansible enough time to to close the SSH connection itself, thereby avoiding the error that you're currently getting.

like image 177
Bruce P Avatar answered Sep 20 '22 01:09

Bruce P


After the reboot task, you should have a local_action task that waits for the remote host to finish rebooting, otherwise, the ssh connection will be terminated and so is the 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 

I also wrote a blog post about achieving a similar solution: https://oguya.github.io/linux/2015/02/22/ansible-reboot-servers/

like image 24
James Oguya Avatar answered Sep 20 '22 01:09

James Oguya