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

11,148 views
Skip to first unread message

ishan jain

unread,
Jul 21, 2016, 2:28:22 AM7/21/16
to Ansible Project
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 ?

Kai Stian Olstad

unread,
Jul 21, 2016, 3:14:07 AM7/21/16
to ansible...@googlegroups.com
On 21.07.2016 08:28, ishan jain wrote:
> 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

--
Kai Stian Olstad

ishan jain

unread,
Jul 21, 2016, 3:32:36 AM7/21/16
to Ansible Project, ansible-pr...@olstad.com
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

Kai

unread,
Jul 21, 2016, 3:44:05 AM7/21/16
to ansible...@googlegroups.com, ansible-pr...@olstad.com
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
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Kai Stian Olstad

unread,
Jul 21, 2016, 4:11:49 AM7/21/16
to ansible...@googlegroups.com
On 21.07.2016 09:32, ishan jain wrote:
> On Thursday, 21 July 2016 12:44:07 UTC+5:30, Kai Stian Olstad wrote:
>>
>> On 21.07.2016 08:28, ishan jain wrote:
>> > 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.

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Jul 21, 2016, 5:57:47 AM7/21/16
to ansible...@googlegroups.com
Had some spare time at lunch and wrote a mockup to show one way to
accomplish this.

*Playbook:*
---
- hosts: all
tasks:
- name: Check if reboot is needed
stat: path=/var/run/reboot-required
register: need_reboot

- name: Generate report
template:
src=report.j2
dest=report.txt
deletate_to: localhost
run_once: true


*report.j2:*
Check 1 need reboot
{% for i in play_hosts | sort %}
{{ i }}: {{ hostvars[i]['need_reboot']['stat']['exists'] |
ternary('Yes', 'No') }}
{% endfor %}

Check 2 swap used
{% for i in play_hosts | sort %}
{{ i }}: {{ hostvars[i]['ansible_memory_mb']['swap']['used'] }} MB
{% endfor %}


Not tested, so bug can exist.

--
Kai Stian Olstad

ishan jain

unread,
Aug 3, 2016, 5:49:10 AM8/3/16
to Ansible Project, ansible-pr...@olstad.com
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 %} 
Reply all
Reply to author
Forward
0 new messages