# Has anyone used Ansible to generate a kind of report about certain checks on remote hosts ?

**URL:** <https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [July 21, 2016, 6:28am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074 "2016-07-21T06:28:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [July 21, 2016, 6:28am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/1 "2016-07-21T06:28:21Z")

</div>

Hi All,

I am trying to prepare Ansible scripts to check remote hosts for certain things like - OS version, free disk space etc. I am able to create tasks for each of this check as Ansible already have a great support for that, but i am stuck at a place on how to create a final report kind of thing for all tasks. I have multiple ways to do this:

**Either, simple print the outcome of checking tasks**

- name: Make drive is present  
win\_stat: path=“{{path}}”  
register: drive

- debug: var=drive

Problem with this approach is that the actual reporting is lost in between all log data of Ansible task execution.

**Let a task fail at the point where the check fails**

- fail: msg=“OS version mismatch”  
when: ‘“Windows Server 2012 R2” not in ansible\_os\_name’

Problem in this approach is that it will not be a exact reporting of all checks.

So, does anyone has an idea how i can generate a report in the end after collecting output of multiple commands ?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 21, 2016, 7:13am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/2 "2016-07-21T07:13:55Z")

</div>

Template?  
[https://docs.ansible.com/ansible/template\_module.html](https://docs.ansible.com/ansible/template_module.html)

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [July 21, 2016, 7:32am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/3 "2016-07-21T07:32:35Z")

</div>

I can see a few problems with the templates:

- I want the report on Ansible host machine and not on target machines
- Seems like a difficult job to consolidate report within template for each target host.  
For eg this kind of report:  
Check number 1  
[host 1]: OK  
[host 2]: Failed
- I do not have extensive knowledhe of jinja2 templates and seems like i will have to learn a few things to create a report template

---

<div class="post-metadata">

**Author:** ![Kai1](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kai1](https://forum.ansible.com/u/Kai1)\
**Post date:** [July 21, 2016, 7:43am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/4 "2016-07-21T07:43:33Z")

</div>

Hi!  
If you can figure out, how to create your report on the target machine, then you’re nearly done. 🙂 Just do that and thereafter transfer it to the control machine (I don’t have a link at hand, but that’s possible).  
Cheers, Kai

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 21, 2016, 8:11am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/5 "2016-07-21T08:11:12Z")

</div>

> > \> So, does anyone has an idea how i can generate a report in the end  
> > \> after  
> > \> collecting output of multiple commands ?
> > 
> > Template?  
> > [https://docs.ansible.com/ansible/template\_module.html](https://docs.ansible.com/ansible/template_module.html)
> 
> I can see a few problems with the templates:
> 
> - I want the report on Ansible host machine and not on target machines

This is the easy part, add delegate\_to: localhost on the template.  
[https://docs.ansible.com/ansible/playbooks\_delegation.html#delegation](https://docs.ansible.com/ansible/playbooks_delegation.html#delegation)

> - Seems like a difficult job to consolidate report within template for each  
> target host.  
> &nbsp;&nbsp;For eg this kind of report:  
> &nbsp;&nbsp;Check number 1  
> &nbsp;&nbsp;[host 1]: OK  
> &nbsp;&nbsp;[host 2]: Failed

This would involve hostvars and for loops in the template.

> - I do not have extensive knowledhe of jinja2 templates and seems like i  
> will have to learn a few things to create a report template

It will be rewarding in the end, you can do a lot of things with jinja2 and templates.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 21, 2016, 9:57am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/6 "2016-07-21T09:57:36Z")

</div>

Had some spare time at lunch and wrote a mockup to show one way to accomplish this.

\*Playbook:\*

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [August 3, 2016, 9:49am UTC](https://forum.ansible.com/t/has-anyone-used-ansible-to-generate-a-kind-of-report-about-certain-checks-on-remote-hosts/22074/7 "2016-08-03T09:49:10Z")

</div>

That was brilliant Kai, it worked for me very well.

To add some more info, i did it like this:

Playbook:

- template: src=/opt/report.txt dest=/opt/reports/linux\_report.txt  
delegate\_to: 127.0.0.1  
run\_once: true

Report.txt

List of linux hosts checked:  
{% for i in play\_hosts %}  
{{ i }}: Username - {{ hostvars[i].ansible\_user }}, Hostname - {{ hostvars[i].ansible\_host }}  
{% endfor %}

Check if OS version is {{linuxDistribution}} version {{linuxDistributionVersion}}  
{% for i in play\_hosts %}  
{% if linuxDistribution == hostvars[i].ansible\_distribution and linuxDistributionVersion == hostvars[i].ansible\_distribution\_version %}  
{{ i }}: Success - OS version - {{linuxDistribution}} version {{linuxDistributionVersion}}  
{% else %}  
{{ i }}: Failed - Found OS version {{ hostvars[i].ansible\_distribution }} version {{hostvars[i].ansible\_distribution\_version }}against required {{linuxDistribution}} version {{linuxDistributionVersion}}  
{% endif %}  
{% endfor %}
