# Trying to create ansible playbook to reboot servers and getting an error

**URL:** <https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661>\
**Category:** Ansible Project\
**Created:** [March 4, 2015, 11:33am UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661 "2015-03-04T11:33:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![codfather](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@codfather](https://forum.ansible.com/u/codfather)\
**Post date:** [March 4, 2015, 11:33am UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/1 "2015-03-04T11:33:10Z")

</div>

After much googling I haven’t found a solution to this. I want to reboot a single server from a group of servers in my hosts file. This is being done on OSX, with ansible 1.8.

Playbook:

- name: restart server  
command: /sbin/reboot

- name: wait for the server to restart  
local\_action:  
module: wait\_for  
host={{ inventory\_hostname }}  
port=22  
delay=1  
timeout=300  
sudo: false

The command being run:

ansible-playbook -l -i ~/ansible\_hosts ~/ans-reboot.yml --ask-sudo-pass

Here is the result:

ERROR: command is not a legal parameter at this level in an Ansible Playbook

I have tried substiting the command: with shell: with no improvement.

I have also looked at the following site which seems to suggestion I’m on the right track:

[https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back](https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back)

So any pointers and tips gratefully received.

Nick

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [March 4, 2015, 5:09pm UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/2 "2015-03-04T17:09:58Z")

</div>

​You are writing only \*tasks\* here and you put them on playbook level.​

You need to wrap them in a playbook declaration.

---

<div class="post-metadata">

**Author:** ![codfather](https://avatars.discourse-cdn.com/v4/letter/c/bcef8e/32.png) [@codfather](https://forum.ansible.com/u/codfather)\
**Post date:** [March 26, 2015, 9:57am UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/3 "2015-03-26T09:57:01Z")

</div>

Thanks for the hint Serge - you put me on the right track to the solution.

As you rightly said there was more needed in the yml file

Here is the working one.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [April 12, 2015, 11:57am UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/4 "2015-04-12T11:57:19Z")

</div>

You say it’s working but do you continue playbook execution after the restart?

I’m also trying to use a task to restart my servers but I would like to wait\_for them to come back and continue executing other tasks.

I’ve tried all variations of the above task (changing async, poll, ignore\_errors, switch command/shell modules, etc) but I always get the following error:

fatal: [server] =\> SSH Error: Shared connection to 1.2.3.4 closed.  
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

It seems that the connection is closed too fast and Ansible doesn’t understand what is happening.

Can anyone confirm this is the right way to reboot a server and continue playbook execution in Ansible 1.9.0.1?

- name: Restart server  
shell: "shutdown -r now  
async: 0  
poll: 0  
ignore\_errors: true

It doesn’t even reach the local\_action tasks with the wait\_for module. This is on CentOS 7.1 x86\_64 running on a Digital Ocean VM.

Thanks,  
Giovanni

---

<div class="post-metadata">

**Author:** ![mhael](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mhael](https://forum.ansible.com/u/mhael)\
**Post date:** [April 13, 2015, 4:20pm UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/5 "2015-04-13T16:20:48Z")

</div>

That is because systemd in CentOS 7 is usually quite fast to stop the system. In my experience modules don’t get a chance to complete and return the results (even with async) in this case. As couple of minutes delay wasn’t a big deal in the particular scenario, I ended up with added delay in shutdown for CentOS 7, like this (replace ‘osid’ with whatever proper Ansible variables):

`

# note - centos7 shutdown do scheduling and returns immediately, centos5/6 shutdown blocks till the actual reboot time

# use 1 min delay for centos7 or else systemd stops the host before task returns

- name: Reboot system because of kernel update  
raw: /sbin/shutdown -r “{{ ‘1’ if osid == ‘centos7’ else ‘now’ }}”  
changed\_when: True

- name: Wait for system to complete reboot (5 min max / 90 sec delay)  
wait\_for: host={{ ansible\_default\_ipv4.address }} port=22 timeout={{ 5 \* 60 }} delay=90 state=started  
delegate\_to: 127.0.0.1

`

Regards,  
Mikhail

---

<div class="post-metadata">

**Author:** ![Konstantin\_Suvakin](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@Konstantin\_Suvakin](https://forum.ansible.com/u/Konstantin_Suvakin)\
**Post date:** [June 8, 2015, 7:44am UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/6 "2015-06-08T07:44:45Z")

</div>

Thank you Mikhail, working on Debian 8 too ( same issue with reboot - playbook stale on server reboot ).

Dňa pondelok, 13. apríla 2015 18:20:49 UTC+2 Mikhail Koshelev napísal(-a):

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [June 8, 2015, 4:22pm UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/7 "2015-06-08T16:22:06Z")

</div>

try this:

- name: Restart server  
shell: " sleep 3; shutdown -r now"

async: 1  
poll: 0  
ignore\_errors: true

---

<div class="post-metadata">

**Author:** ![Mike3](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Mike3](https://forum.ansible.com/u/Mike3)\
**Post date:** [March 8, 2016, 5:51pm UTC](https://forum.ansible.com/t/trying-to-create-ansible-playbook-to-reboot-servers-and-getting-an-error/17661/8 "2016-03-08T17:51:53Z")

</div>

Is there a simple way to use ‘shutdown -r 22:00’ in a playbook to schedule a reboot in the future? Or is it necessary to wrap the command in nohup and &?

Thanks!
