Hi I am struck with looping for below requirement.
The mail section has to loop over first group in inventory, check the uptime with the condition of more than 200 days and send mail to only the lab_email with lab servers only
Next go to 2nd web group check condition and send mail to web_email with web servers only.
- name: send mail
hosts: all
gather_facts: no
become: yes
tasks:
- name: check uptime
ignore_errors: yes
shell: uptime |awk '{print $3}'
register: up_time
- debug:
msg: "{{inventory_hostname}} has {{ up_time.stdout }} days uptime"
- name: send mail to chetan
mail:
host: relay.services
port: 25
from: ad...@company.com
to: "{{web_email}}"
subject: uptime
body: |-
{% for server, servervars in hostvars.items() %}
{% if servervars.up_time.stdout | default (0) |int >= 5 %}
{{ server }} - {{"has"}} {{ servervars.up_time.stdout }} {{"days uptime. Consider rebooting for better performance"}}
{% endif %}
{% endfor %}
become: no
delegate_to: localhost
run_once: true
INVENTORY FILE
[lab:vars]
lab_email="<labg...@company.com>"
[lab]
linuxlab-01
linuxlab-02
[web:vars]
web_email="<webg...@company.com>"
[web]
webserver-01
webserver-02
[db:vars]
db_email="<dbg...@company.com>"
dbserver-03
dbserver-04
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/0E7YoyU2LoM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7fK7eaF5gdqZJxG6gVqZa92rC3Pju5n4x70AaQJeAuYpQ%40mail.gmail.com.