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: [email protected]
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="<[email protected]>"
[lab]
linuxlab-01
linuxlab-02
[web:vars]
web_email="<[email protected]>"
[web]
webserver-01
webserver-02
[db:vars]
db_email="<[email protected]>"
dbserver-03
dbserver-04
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/5cdf5cd9-aeeb-4584-b936-f4235d179aebn%40googlegroups.com.