Ansible playbook rebooting the server , wait till reboot complete

us flag

I'm facing a issue while executing shell script with ansible playbook.
Issue: I'm losing a connection with remote server because shell script is rebooting the server.

My Ansible playbook

- name: Handle reboot
  hosts: all
  become: yes
    - name: Execute the script 
      shell: bash
        chdir: /home/ubuntu
        - Wait for server to restart
    - name: Wait for server to restart
        module: wait_for
          host={{ inventory_hostname }}
        become: false

My shell script :

echo "Performing some tasks"
echo "rebooting now"
echo "reboot completed"
echo "Performing some more tasks"

The error I'm getting while the remote server reboot

fatal: [my-ip-address]: UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Shared connection to <my-ip-address> closed.",
    "unreachable": true

Is it possible to handle a reboot which is done by shell script and wait for connection until the remote server is up again ?


in flag
Is there a reason not to use the [reboot module](
in flag
You realize that the commands in your script after `reboot` are never going to be executed?
vik2595 avatar
us flag
@GeraldSchneider , I'm going to install a specific tools which includes this steps. More details about script, It's renaming network interface and assigning specific IP. FYI: We can not change anything in shell script. So i have to think other way to make it work.
vik2595 avatar
us flag
@GeraldSchneider apparently it's executing , Please check this script for more detail.
in flag

You should use wait_for_connection instead.

  - name: Execute the script 
    shell: bash
      chdir: /home/ubuntu
  - name: wait
      delay: 10

I'd advise to do this in a task, not a handler. The handler is only executed after all tasks have been finished, so if you have tasks following the task that executes the reboot they will be tried before the playbook even starts to wait.

Alternatively, use the reboot module, which does this automatically.

vik2595 avatar
us flag
Definitely , let me try this method.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.