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

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 ?

Template?
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
  • 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

Hi!
If you can figure out, how to create your report on the target machine, then you’re nearly done. :slight_smile: 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

> 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

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

- 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

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.

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

*Playbook:*

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 %}