I have a playbook to list all the windows updates needed for multiple hosts and then uses a jinja template to output the results to a html file. The playbook runs ok however, multiple html files are created for each host. The html file name uses the current date and time to make it unique.
For example, I target 10 hosts which results in 10 html files being created and each file contains the list of windows updates for all the 10 hosts.
The playbook below is part of a role:
---
- name: Search for Windows Updates
ansible.windows.win_updates:
category_names: '*'
server_selection: "windows_update"
state: searched
register: check_win_updates_patchresult
- name: Create HTML report
ansible.windows.win_template:
src: report.j2
dest: \\server\\reports\Windows_Patch_Report.html
I would be grateful for any guidance on this. Thank you.
What I need is to run the playbook against multiple hosts and just create one single report. I have not been able to find any solutions for this so far.
A playbook cannot be part of a role, it works the other way - a playbook can import/include a role. Your example is a list of tasks.
It's quite simple.
But you did not specify where would you like to have the report - the implementation would depend on it. It's about templating and where do you run it.
For example, if you want to have one report with all the hosts data within, you just need to iterate over the list of your hosts and template the report on the localhost referencing the registered variable via
hostvars(post-processing of theupdatesreturn value is omitted for simplicity):Creation of the report in this case should be removed from your role, or delegated to
localhostwithrun_once: true(if it's a Windows machine, too):And a playbook would look like this:
Alternatively, if you want to have a report per machine located on that machine, the template will be even simpler, and no changes should be made to the playbook or the role: