Reboot Servers, Check Last Reboot and Email Report Anomaly

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:

<body>
    {% if 'wed4' in group_names %}
        <h3>Reboot Status for 4th Wednesday Collection</h3>
    {% elif 'wed3' in group_names %}
        <h3>Reboot Status for 3rd Wednesday Collection</h3>
    {% else %}
        <h3>Reboot Status for Ansible Server</h3>
    {% endif %}
    <p>Verify reboot status for the following servers:</p>
    <table>
        <thead>
            <tr>
                <th>Server</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            {% if 'wed4' in group_names %}
            {% for host in groups['wed4'] %}
            <tr>
                <td>{{ host}}</td>
                <td>{{ last_reboot_result.stdout_lines[0] }}</td>
            </tr>
            {% endfor %}
            {% elif 'wed3' in group_names %}
            {% for host in groups['wed3'] %}
            <tr>
                <td>{{ host}}</td>
                <td>{{ last_reboot_result.stdout_lines[0] }}</td>
            </tr>
            {% endfor %}
            {% else %}
            <tr>
                <td>ansibleserver</td>
                <td>{{ last_reboot_result.stdout_lines[0] }}</td>
            </tr>
            {% endif %}
        </tbody>
    </table>
</body>

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.

Think I figured it out. At least in terms of the solution by replacing the email body’s code to:

<tbody>
	{% for host in ansible_play_hosts_all %}
	<tr>
		<td>{{ host }}</td>
		<td>{{ hostvars[host].last_reboot_result.stdout_lines[0] }}</td>
	</tr>
	{% endfor %}
</tbody>

Previous convoluted code was probably just rehashing the variable “last_reboot_result.stdout_lines[0]” from just one host.

Cheers.