Skip to content

Instantly share code, notes, and snippets.

@edrzmr
Last active October 11, 2019 21:26
Show Gist options
  • Save edrzmr/2968d61e6494f035c5d3aac3151ba922 to your computer and use it in GitHub Desktop.
Save edrzmr/2968d61e6494f035c5d3aac3151ba922 to your computer and use it in GitHub Desktop.
ansible waiting for reboot, really really works!
- name: 'check if reboot is required'
shell: if [ $(readlink -f /vmlinuz) != /boot/vmlinuz-$(uname -r) ]; then echo 'yes'; else echo 'no'; fi
ignore_errors: true
register: reboot
- name: 'reboot...'
shell: nohup bash -c 'sleep 2 && shutdown -r now "Ansible kernel update applied"' &
async: 0
poll: 0
ignore_errors: true
when: reboot.stdout.find('yes') != -1
register: rebooting
- name: 'waiting...'
local_action: wait_for host={{ ansible_host | default(inventory_hostname,boolean=True) }} search_regex='OpenSSH' port=22 state='started' delay=30 timeout=300
become: no
when: rebooting|changed
@edrzmr
Copy link
Author

edrzmr commented Aug 19, 2016

nohup bash -c 'sleep 2 && shutdown -r now "Ansible kernel update applied"' &

This is the magic, background reboot in the future, give 2 seconds to ansible/ssh disconnect from remote host

@gmzabos
Copy link

gmzabos commented Nov 9, 2016

Very nice!

Works for me in 2.2.0

@razvlix
Copy link

razvlix commented Dec 20, 2016

Hello,

Good work, but unfortunatelly I get this:

"bash: shutdown: command not found", "stdout": "", "stdout_lines": [], "warnings": []}

And the playbook looks like this:

`---

  • hosts: web1
    remote_user: razvan
    become: True
    become_method: su
    tasks:
    • name: install nmap
      yum: pkg=nmap state=latest

    • name: 'reboot...'
      shell: nohup bash -c 'sleep 2 && shutdown -r now "Instalat NMAP"'
      async: 0
      poll: 0
      register: rebooting

    • name: 'waiting...'
      local_action: wait_for host=web1 search_regex='OpenSSH' port=22 state='started' delay=30 timeout=300
      become: True
      become_method: su
      when: rebooting|changed`

@my2ter
Copy link

my2ter commented Jan 3, 2017

Nice work!

Saved me a massive headache because the solution given by Ansible here doesn't work... You should let them know of yours!

Thanks

@gmaghera
Copy link

Thank you @drrzmr!!! I was trying to get something functional put together from three examples, none of which worked on their own. This write-up is doing a favor for the entire Ansible community. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment