---
# reboot an Ubuntu machine if needed and wait for it to come back
- name: Reboot instance
  command: /sbin/shutdown -r now
  register: reboot_result
  when: reboot_needed

- name: Wait for instance to come online
  local_action:
    module: wait_for
    host: "{{ ansible_ssh_host|default(inventory_hostname) }}"
    port: "{{ ansible_ssh_port|default(ssh_port) }}"
    delay: 30
    timeout: 600
    state: started
  when: reboot_result|changed
  become: false


---
# reboot an Ubuntu machine if needed and wait for it to come back
- name: Reboot instance
  command: /sbin/shutdown -r now
  register: reboot_result
  when:
    - reboot_needed
    - ansible_ssh_user != 'vagrant'

- name: Reboot vagrant instance
  local_action: command vagrant reload "{{inventory_hostname}}"
  register: reboot_result
  when:
    - reboot_needed
    - ansible_ssh_user == 'vagrant'
  become: false

- name: Wait for instance to come online
  local_action:
    module: wait_for
    host: "{{ ansible_ssh_host|default(inventory_hostname) }}"
    port: "{{ ansible_ssh_port|default(ssh_port) }}"
    delay: 30
    timeout: 600
    state: started
  when: reboot_result|changed
  become: false


Use vagrant reload for rebooting a vagrant instance
Use vagrant reload for rebooting a vagrant instance

Rebooting the instance from the machine itself bypasses setup that
`vagrant up' does, such as synchronized folders. This breaks, at
least, package installation with the vagrant-cachier plugin, as its
overlay directory won't be mounted.

Use ansible_ssh_user == 'vagrant' as the signal that a machine is a
vagrant instance.
