-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathjob_report_simple.yaml
80 lines (70 loc) · 2.75 KB
/
job_report_simple.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
# This is a simpler, self-contained version of job_report.yml.
- name: Run jobs and generate simple report
hosts: all
gather_facts: no
order: sorted
collections:
- ansible.builtin
- community.general
vars:
# Location for job reports
outdir: /var/tmp/reports
pre_tasks:
- name: Create output folder
run_once: yes
delegate_to: localhost
file:
path: "{{ outdir }}"
state: directory
tasks:
# Add your own tasks here! #
- name: Run a command and save the results
command:
cmd: echo "This is output from command 1."
register: cmd1_results
post_tasks:
- name: Save some useful facts under Ansible controller
delegate_to: localhost
delegate_facts: True
run_once: yes
set_fact:
date_str: "{{ date }}_{{ hms }}"
date_str_pretty: "{{ date }} {{ hms_pretty }}"
success_list: "{{ ansible_play_hosts }}"
failed_list: "{{ ansible_play_hosts_all | difference(ansible_play_hosts) }}"
vars:
date: "{{ '%Y-%m-%d' | strftime }}"
hms_pretty: "{{ '%H:%M:%S' | strftime }}"
hms: "{{ hms_pretty | replace(':','-') }}"
# The template module is a better alternative for larger or more complex reports.
- name: Save simple job report to {{ outdir }} on Ansible server
delegate_to: localhost
delegate_facts: True
run_once: yes
copy:
content: |
<html>
<body>
<h1 id="top">Job Report for {{ hostvars['localhost']['date_str_pretty'] }}</h1>
<hr>
<p><strong>Host Totals:</strong></p>
<ul>
<li>Successful: {{ hostvars['localhost']['success_list']|length }} </li>
<li>Failed: {{ hostvars['localhost']['failed_list']|length }}</li>
<li>Total: {{ ansible_play_hosts_all|length }}</li>
</ul>
{%- if hostvars['localhost']['failed_list']|length > 0 %}
<p style="color:fuchsia;"><strong>Failed hosts:</strong> {{ hostvars['localhost']['failed_list']|join(',') }}</p>
{%- endif %}
{% for h in ansible_play_hosts_all | sort %}
<hr>
<h3>{{ h }}: {{ 'success' if h in ansible_play_hosts else 'failed' }}</h3>
<p>Output: </p><pre>{{ hostvars[h]['cmd1_results']['stdout']|d('no output')|trim }}</pre>
<p>Errors: </p><pre>{{ hostvars[h]['cmd1_results']['sterr']|d('no errors')|trim }}</pre>
{% endfor %}
<hr>
<p><i>Generated by Ansible playbook <strong>{{ playbook_dir }}/job_report_simple.yaml.</strong></i></p>
</body>
</html>
dest: "{{ outdir }}/job_report_simple_{{ hostvars['localhost']['date_str'] }}.html"