Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use ansible playbook to reboot a ubuntu server?

I am trying to build an ansible playbook to configure a ubuntu vagrant box. The playbook is pretty much working with the exception of controlling the ubuntu box reboot after upgrading the kernel.

I have a host file for ansible as follow :

localhost ansible_connection=local
dockerhost ansible_ssh_port=2222 ansible_ssh_host=127.0.0.1

The latest iteration I tried to solve this problem is as follow :

  - name: Restart the server
    shell: sleep 2s && reboot & executable=/bin/bash

  - name: Wait until the virtual machine stop ie: ssh port stop responding
    local_action: wait_for host={{ansible_ssh_host}} port={{ansible_ssh_port}} state=stopped
    sudo: false

  - name: Wait for server to come up
    local_action: wait_for host={{ansible_ssh_host}} port={{ansible_ssh_port}} delay=30
    sudo: false

With this playbook steps the process block waiting for the ssh port to stop responding, until it reach timeout and exit the playbook, I am guessing that if the reboot is particularly fast it might happen in between the polling intervals of the wait_for command and miss the short time when the ssh port is actually down. The error returned by ansible is :

failed: [dockerhost] => {"elapsed": 300, "failed": true} msg: Timeout when waiting for 127.0.0.1:2222 to stop.

At least once it managed the get to the step where ansible wait for the ssh port to be available again but hung there until timing out. I think this solution is too sensitive to difference in reboot speed, which can vary widely in a virtual environment. The ansible version I am using is 1.5.3 Ubuntu is 12.04lte with a Kernel upgrade to 3.8 The complete playbook install docker and all dependencies.

I tried many variations and ideas found in various web sites but never managed to properly control a reboot and continue my playbook with the next steps.

I am looking for a simple and fool proof way of rebooting the server and continuing with the next steps in a playbook once the machine is back up and running

I have not explored the possibility of running a local vagrant reload because I want to use this same playbook in operation where I will not be running vagrant, I only mentioned vagrant in case it creates some complexities that I am not aware of. I also don't want to just pause 5 minutes and hope for the server to be up again, the point of using this kind of tools is to provision servers in a predictable and timely manner and to be portable from environment to environment, a pause just does not seem right.

I have also looked around for an ansible module that would manage this requirement but comments on the ansible web site seem to rule this out.

Thanks

like image 940
Raymond Avatar asked Nov 24 '22 01:11

Raymond


1 Answers

Might I suggest to use a bridge or private network. Using the port forward might be tricky. I used your code with private and bridged network worked perfectly with both.

like image 88
DomaNitro Avatar answered Nov 25 '22 13:11

DomaNitro