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 %}
On Thursday, 21 July 2016 15:27:47 UTC+5:30, Kai Stian Olstad wrote:
>
> On 21.07.2016 10:11, Kai Stian Olstad wrote:
> > On 21.07.2016 09:32, ishan jain wrote:
> >
> >> - 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:*
> ---
> - 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
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/6652a07f-8d15-446f-878d-fdc1bf777b8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.