wait_for on reboot doesn't seem to be waiting.

Hi all,

I’m trying to write a playbook that patches and reboots a server if necessary to apply kernel updates, etc. When I run it, it reboots and loses the connection instead of applying the wait_for piece.

Any ideas?

Here’s my playbook:

  • name: Updating Servers
    hosts: stig-test-lx
    gather_facts: true

tasks:

  • name: Update to latest of all pkgs
    yum:
    name: “*”
    state: latest

  • name: See if needs to reboot to apply kernel
    shell: “LAST_KERNEL=$(rpm -q --last kernel | awk ‘NR==1{sub(/kernel-/,""); print $1}’); CURRENT_KERNEL=$(uname -r); if [ $LAST_KERNEL != $CURRENT_KERNEL ]; then echo ‘reboot’; else echo ‘no’; fi”
    ignore_errors: true
    register: reboot_hint

  • name: Rebooting …
    command: shutdown -r +1 “Rebooting due to kernel updates”
    async: 30
    poll: 0
    ignore_errors: true
    when: reboot_hint.stdout.find(“reboot”) != -1

  • name: Waiting for system to come back up
    wait_for:
    host: “{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}”
    port: 22
    search_regex: OpenSSH
    delay: 10
    timeout: 180

when: reboot_hint.stdout.find(“reboot”) != -1

vars:
ansible_connection: local

Note: I commented out the “when” to test the reboot regardless.

Hi all,

I'm trying to write a playbook that patches and reboots a server if
necessary to apply kernel updates, etc. When I run it, it reboots and
loses the connection instead of applying the *wait_for* piece.

Any ideas?

Here's my playbook:

- name: Updating Servers
  hosts: stig-test-lx
  gather_facts: true

  tasks:
  - name: Update to latest of all pkgs
    yum:
      name: "*"
      state: latest

  - name: See if needs to reboot to apply kernel
    shell: "LAST_KERNEL=$(rpm -q --last kernel | awk
'NR==1{sub(/kernel-/,\"\"); print $1}'); CURRENT_KERNEL=$(uname -r); if [
$LAST_KERNEL != $CURRENT_KERNEL ]; then echo 'reboot'; else echo 'no'; fi"
    ignore_errors: true
    register: reboot_hint

  - name: Rebooting ...
    command: shutdown -r +1 "Rebooting due to kernel updates"
    async: 30
    poll: 0
    ignore_errors: true
    when: reboot_hint.stdout.find("reboot") != -1

+1 in shutdown means wait 1 minute before rebooting.

  - name: Waiting for system to come back up
    wait_for:
      host: "{{
(ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
      port: 22
      search_regex: OpenSSH
      delay: 10
      timeout: 180
# when: reboot_hint.stdout.find("reboot") != -1
    vars:
      ansible_connection: local

Your delay is only 10 seconds, so by the time it checks(after 10s) the machine is still on and port 22 is very much alive.
So your delay must be at least 60s + the time it takes for the machine to stop ssh.

If you want to do this a little faster you can use this task

- name: Reboot
   shell: sleep 2 && shutdown -r now
   async: 1
   poll: 0

And if you are on Ansible 2.3 or newer I recommend using wait_for_connection instead of wait_for.
wait_for_connection waits until the system is capable/ready to run Ansible code and not just that ssh has started.

Awesome! Thanks for that little tidbit regarding the wait_for_connection! That’s huge!

I think you need to increase the poll