Reboot and reconnect between Ansible tasks

Tags:

go to ansible.com

You can reboot and reconnect to your hosts between Ansible tasks.

# Other task above here.

# Now reboot and reconnect
- block:

    # The at module allows ansible to issue the command and disconnect 
    # cleanly before the reboot disconnects the network connection.
   - name: Reboot in one minute
     at: command=reboot count=1 units=minutes

   # You may need to adjust this dependong on how fast your host halts.
   - name: Wait for reboot to start and hopefully finish 
     pause: minutes=2

   - name: Try to re-connect to rebooted host before continuing  
     command: /bin/true
     register: online
     retries: 20
     delay: 30
     until: online|success

# Continue your tasks...

Update 5 Jan 2017

Here's another option using the wait_for module.

- name: reboot                                                              
    at: command=reboot count=1 units=minutes                                  

- name: wait for sshd on vms to resume                                      
  wait_for:                                                                 
    host: maple.example.com                                                    
    port: 22                                                                
    delay: 90       

# Continue your tasks...
  1. blocks group tasks for better control and debugging.
  2. at uses the UNIX at tool.
  3. pause pauses like sleep.
  4. Retries and delay works like a do until loops.
submit to reddit