# Ansible error during server reboot

**URL:** <https://forum.ansible.com/t/ansible-error-during-server-reboot/27683>\
**Category:** Ansible Project\
**Created:** [June 1, 2018, 5:20am UTC](https://forum.ansible.com/t/ansible-error-during-server-reboot/27683 "2018-06-01T05:20:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnab\_sen](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@arnab\_sen](https://forum.ansible.com/u/arnab_sen)\
**Post date:** [June 1, 2018, 5:20am UTC](https://forum.ansible.com/t/ansible-error-during-server-reboot/27683/1 "2018-06-01T05:20:12Z")

</div>

Hi I’m getting below error during sw upgrade of a server using Ansible. Remote machine is linux server. Note that the reboot is not initiated by Ansible. At the end of the sw upgrade the server reboot automatically (application initiated reboot). Can anybody help?

fatal: [192.168.1.131]: UNREACHABLE! =\> {“changed”: false, “msg”: “Failed to connect to the host via ssh: Shared connection to \*\*\*\*(server IP) closed.\r\n”, “unreachable”: true}

Script.

```auto
- hosts: remote_server 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  vars:
     ansible_ssh_host: **** (host IP) 
     ansible_ssh_port: 22 
  tasks:
  - name: Install SW version xxx
    ....
    ....

  - name: waiting for server to reboot
    wait_for: host="{{ ansible_ssh_host | default(inventory_hostname) }}" port={{ ansible_ssh_port | default(22) }} search_regex=OpenSSH delay=60 timeout=300 
    connection: local
    sudo: false  

  - shell: uptime
    register: output
  - debug: msg=" {{ output.stdout }} "

```

---

<div class="post-metadata">

**Author:** ![Umesh\_Gowda](https://avatars.discourse-cdn.com/v4/letter/u/8edcca/32.png) [@Umesh\_Gowda](https://forum.ansible.com/u/Umesh_Gowda)\
**Post date:** [June 1, 2018, 5:46am UTC](https://forum.ansible.com/t/ansible-error-during-server-reboot/27683/2 "2018-06-01T05:46:53Z")

</div>

not seen any reboot command after

```auto
- name: Install SW version xxx

```

---

<div class="post-metadata">

**Author:** ![arnab\_sen](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@arnab\_sen](https://forum.ansible.com/u/arnab_sen)\
**Post date:** [June 1, 2018, 5:51am UTC](https://forum.ansible.com/t/ansible-error-during-server-reboot/27683/3 "2018-06-01T05:51:02Z")

</div>

The host reboots as part of “Install SW version xxx”

Thanks  
Arnab

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 1, 2018, 8:56am UTC](https://forum.ansible.com/t/ansible-error-during-server-reboot/27683/4 "2018-06-01T08:56:26Z")

</div>

> Hi I'm getting below error during sw upgrade of a server using Ansible.  
> Remote machine is linux server. Note that the reboot is not initiated by  
> Ansible. At the end of the sw upgrade the server reboot automatically  
> (application initiated reboot). Can anybody help?

The best option is to check if it's possible to turn off automatic reboot and then do the reboot in Ansible.  
If that's not possible this might be a gussing game.

> fatal: [192.168.1.131]: UNREACHABLE! =\> {"changed": false, "msg": "Failed  
> to connect to the host via ssh: Shared connection to \*\*\*\*(server IP)  
> closed.\r\n", "unreachable": true}
> 
> Script.
> 
> - hosts: remote\_server  
> &nbsp;&nbsp;remote\_user: ansible  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;become\_method: sudo  
> &nbsp;&nbsp;connection: ssh  
> &nbsp;&nbsp;gather\_facts: yes  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_ssh\_host: \*\*\*\* (host IP)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_ssh\_port: 22  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: Install SW version xxx  
> &nbsp;&nbsp;&nbsp;&nbsp;....

Check out fire and forget  
[http://docs.ansible.com/ansible/latest/user\_guide/playbooks\_async.html](http://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html)

poll must be 0 and async must be a value higher than the time it take to execute task "Install SW version xxx"

> &nbsp;&nbsp;- name: waiting for server to reboot  
> &nbsp;&nbsp;&nbsp;&nbsp;wait\_for: host="{{ ansible\_ssh\_host | default(inventory\_hostname)  
> }}" port={{ ansible\_ssh\_port | default(22) }} search\_regex=OpenSSH  
> delay=60 timeout=300  
> &nbsp;&nbsp;&nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;&nbsp;&nbsp;sudo: false

I recommend using wait\_for\_connection because this one check if Ansible is able to run on remote before it goes to the next task.  
But in either case the delay must be greater than the time to execute task "Install SW version xxx".

So timing is essential but if you get them right it should work.
