Reboot and wait with Ansible

Reboot and wait with Ansible

Sometimes it is required to reboot a Linux server after certain tasks. If you’re using Ansible as the tool to automate your server management, you can write a small role in Ansible to trigger a quick reboot, wait for the server to come back to life and continue with other tasks.

The following script works in Ansible 2.1; tested in Ubuntu 14.04 and RHEL 6

- name: Restart server
  become: yes
  shell: sleep 2 && /sbin/shutdown -r now "System package upgraded"

- name: waiting 30 secs for server to come back
  local_action: wait_for host={{ ansible_default_ipv4.address }} port=22 state=started delay=30 timeout=60
  become: false

 

Sayantan Das

Sayantan is a DevOps Consultant by the day and works mainly with Ansible and Linux . He is a AWS - SA and RedHat Certified Engineer . He loves to tinker with Linux systems .

Leave a Reply

tuxtrixmod
Close Menu
%d bloggers like this: