Reboot Servers, Check Last Reboot and Email Report Anomaly

twkl
Fri, 21 Nov 2025 05:25:43 +0000
Get Help

Hi,

I’ve got an ansible role with a task to reboot a bunch of servers, check last reboot and then email last reboot output:

# tasks file for reboot_status
- name: reboot
  ansible.builtin.reboot:
  become: true
  when: inventory_hostname != 'localhost'

- name: last reboot
  ansible.builtin.command: last reboot | head -1
  register: last_reboot_result

- name: email reboot_result
  community.general.mail:
    host: smtp.com.local
    port: 25
    from: Ansible Alert <ansible@com.local>
    to:
      - systemalerts@com.local
    subject: Reboot Status for Linux Servers
    body: "{{ lookup('template', 'templates/email_body.j2') }}"
    subtype: html
  run_once: true
  delegate_to: localhost

The email template looks like this:


    {% if 'wed4' in group_names %}
Reboot Status for 4th Wednesday Collection
    {% elif 'wed3' in group_names %}
Reboot Status for 3rd Wednesday Collection
    {% else %}
Reboot Status for Ansible Server
    {% endif %}
Verify reboot status for the following servers:



Server
Status



            {% if 'wed4' in group_names %}
            {% for host in groups['wed4'] %}

{{ host}}
{{ last_reboot_result.stdout_lines[0] }}

            {% endfor %}
            {% elif 'wed3' in group_names %}
            {% for host in groups['wed3'] %}

{{ host}}
{{ last_reboot_result.stdout_lines[0] }}

            {% endfor %}
            {% else %}

ansibleserver
{{ last_reboot_result.stdout_lines[0] }}

            {% endif %}




However, the Status column in the report is not accurate. I think its using the …

last_reboot_result.stdout_lines[0]

… from the first server its processing and repeating the result to all servers in the Status column.

The Status column looks like this:

reboot system boot 5.15.0-161-gener Wed Nov 19 18:00 still running

Which is wrong for some of the servers which are on a different kernel version. The logic in the role’s task isn’t quite right.

The cron job runs this command:

/usr/bin/ansible-playbook /etc/ansible/playbooks/playbook_reboot_status.yml --limit wed3

Any advice would be most appreciated.

Thanks.

7 posts - 3 participants