3

How do I go through all hosts in an Ansible inventory in serial with an Ansible ad-hoc and reboot servers with a specified number of seconds in between each reboot?

I am looking for something similar to: ansible -i inventory nodes -a "/sbin/reboot"

It should have a pause and run in serial.

2 Answers 2

5

@Argyle explained the --fork 1 trick to achieve a serial run.

I suggest you have a look at the reboot module that might be better suited than running a command directly.

A quick example that will reboot all your nodes one by one waiting for each to be fully booted before moving to the next one.

ansible -i inventory nodes --forks 1 -m reboot 

Pass needed parameters to the module depending on your requirements with the -a option.

1
  • 2
    +1 for the reboot module. I wasn't aware it waited until the machine came back online. Good to know!
    – Argyle
    Jan 22, 2020 at 18:02
2

I think this might be as easy as adding a sleep with the value of seconds you'd like to wait before rebooting. Also, since Ansible runs in parallel by default, we'll have to specify that we only want it to run as one process (what Ansible calls forks) instead of the default of 5. So, like this:

ansible -i inventory nodes -a "sleep 30 && /sbin/reboot" -f 1
2
  • I also really like this one because you can tune how long it waits. Which answers my original question. I did pick @Zeitounator's answer because I am all about that module though. Great answers!
    – David West
    Jan 22, 2020 at 20:12
  • @DavidWest Note that the module has options to tune different delays, and that ansible allows you to async and poll as well. Jan 23, 2020 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.