Boot Process
There are no specific modules for managing a linux system’s boot process with Ansible. However, you can use your pre-existing knowledge of the boot process to manage related files. You can use the file module to manage systemd boot targets. Or lineinfile to manage the GRUB configuration.
The reboot module does let you to reboot a host though. And it will pick up after the reboot at the exact same location in a playbook.
Managing Systemd Targets (symlink method)
To manage the default systemd target, the /etc/systemd/system/default.target file must exist as a symbolic link to the desired default target:
Let’s use the file module to change the symlink, targeting graphical mode instead:
Managing Systemd Targets (systemctl method)
This method isn’t great because you have to run around with your head cut off trying to figure out a way to make it indempotent.
Rebooting Managed Hosts
As mentioned, the reboot module is used to restart managed nodes. And the test_command argument verifies that the host is available after the reboot. test_command specifies a command that Ansible should run on the managed hosts after the reboot. If the command is successful, then Ansible knows to continue.
There are also arguments related to timeouts:
| Argument | Purpose |
|---|---|
| connect_timeout | Maximum seconds to wait for a successful connection before trying again. |
| post_reboot_delay | Seconds to wait after the reboot command before trying to check if a host is available. |
| pre_reboot_delay | Seconds to wait before issuing the reboot. |
| reboot_timeout | Maximum seconds to wait for the rebooted machine to respond to the test command. |
Let’s change default target and reboot the system:
You can test that the reboot was issued successfully by using:
Finding help
You may need to search man pages to find more info:
The relevant man pages here may be:
I didn’t find exact instructions on how to change the default target. But there is information on symlinking in the systemd.unit. And also mentions using the systemctl command to make the symlink:
If you can’t remember the search path, the paths are listed at the top of the systemd-unit man page:
If you can remember that you need to symlink a unit to /etc/systemd/system/default.target then I think you’ll be good here.
You can also check options for the systemctl command:
You’ll also want to remember where systemd keeps all of the default unit files. List is listed at the top of the systemd man page:
The file module documentation has a symlink example at the bottom:
Just use similar methods (managing settings file) to change GRUB settings and other boot options. Feel free to reach out for any clarifications needed. :)
