Skip to content

Instantly share code, notes, and snippets.

@tho
Last active January 9, 2018 02:40
Show Gist options
  • Save tho/c705b13e710954bb8952c95bc33755c8 to your computer and use it in GitHub Desktop.
Save tho/c705b13e710954bb8952c95bc33755c8 to your computer and use it in GitHub Desktop.
Reboot host with Ansible

Reboot host with Ansible

- name: Reboot host
  shell: sleep 2 && shutdown -r now "Reboot initiated by Ansible"
  async: 1
  poll: 0
  become: True
  listen: reboot_host

- name: Pause until host is shutting down
  local_action:
    module: wait_for
    timeout: 3
  become: False
  listen: reboot_host

- name: Wait until host is reachable
  wait_for_connection:
  become: False
  listen: reboot_host

References

---
- hosts: all
gather_facts: no
tasks:
- name: Get uptime before reboot
command: uptime
register: uptime
- debug: var=uptime.stdout_lines
- debug: msg="Rebooting {{ inventory_hostname }}"
changed_when: True
notify: reboot_host
- meta: flush_handlers
- name: Get uptime after reboot
command: uptime
register: uptime
- debug: var=uptime.stdout_lines
handlers:
- name: Reboot host
shell: sleep 2 && shutdown -r now "Reboot initiated by Ansible"
async: 1
poll: 0
become: True
listen: reboot_host
- name: Pause until host is shutting down
local_action:
module: wait_for
timeout: 3
become: False
listen: reboot_host
- name: Wait until host is reachable
wait_for_connection:
become: False
listen: reboot_host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment