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

6,193 views
Skip to first unread message

codfather

unread,
Mar 4, 2015, 12:02:46 PM3/4/15
to ansible...@googlegroups.com
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 <hostname-to-reboot> -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:


So any pointers and tips gratefully received.

Nick

Serge van Ginderachter

unread,
Mar 4, 2015, 12:10:23 PM3/4/15
to ansible...@googlegroups.com

On 4 March 2015 at 12:33, codfather <swcod...@gmail.com> wrote:
ERROR: command is not a legal parameter at this level in an Ansible Playbook


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

You need to wrap them in a playbook declaration.

codfather

unread,
Mar 26, 2015, 5:57:01 AM3/26/15
to ansible...@googlegroups.com
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.

---
- hosts: all
  sudo: yes
  tasks:
    - name: restart server
      command: /sbin/reboot
      async: 0
      poll: 0
      ignore_errors: true
    - name: wait for the server to restart
      local_action: wait_for host={{ inventory_hostname }}
                    port=22
                    delay=1
                    timeout=300
                    state=started
      sudo: false 

Cheers

Giovanni Tirloni

unread,
Apr 12, 2015, 7:57:19 AM4/12/15
to ansible...@googlegroups.com
On Thursday, March 26, 2015 at 6:57:01 AM UTC-3, codfather wrote:
Here is the working one.

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

Mikhail Koshelev

unread,
Apr 13, 2015, 12:20:49 PM4/13/15
to ansible...@googlegroups.com
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

Konstantin Šuvakin

unread,
Jun 8, 2015, 10:20:23 AM6/8/15
to ansible...@googlegroups.com
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):

benno joy

unread,
Jun 8, 2015, 12:22:11 PM6/8/15
to ansible...@googlegroups.com
try this:

- name: Restart server
      shell: " sleep 3; shutdown -r now"
      async: 1
      poll: 0
      ignore_errors: true

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/43e640c8-c723-4b11-b1aa-d63e3839b83b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mike

unread,
Mar 8, 2016, 12:52:45 PM3/8/16
to Ansible Project


On Monday, June 8, 2015 at 11:22:11 AM UTC-5, benno joy wrote:
try this:

- name: Restart server
      shell: " sleep 3; shutdown -r now"
      async: 1
      poll: 0
      ignore_errors: true

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!
Reply all
Reply to author
Forward
0 new messages