Ansible reboot Linux machine or server with playbooks

In Ansible, you can use the reboot module to reboot a Linux machine or server. The reboot module allows you to reboot a machine and wait for it to come back online before continuing with the rest of the playbook tasks.

Here is an example of how you can use the reboot module in an Ansible playbook:

- name: Reboot the server
reboot:
timeout: 300
connect_timeout: 5

- name: Wait for the server to come back online
wait_for:
host: "{{ ansible_host }}"
port: 22
timeout: 300

- name: Verify the server is back online
command: uptime
register: uptime

- name: Debug uptime information
debug:
var: uptime

The above playbook will reboot the server, wait for it to come back online, and then check the uptime to confirm that it is back online.

The timeout option specifies the amount of time to wait for the machine to reboot and come back online. The connect_timeout option specifies the amount of time to wait for the connection to the machine to be re-established.

The wait_for task is used to wait for the server to come back online after the reboot. It uses the host and port options to specify the server to connect to and the timeout option to specify how long to wait for the connection to be established.

Please be aware that this command will reboot the target machine and might disrupt any running services or active sessions.

Leave a Comment