how to loop inventory groups in ansible

38 views
Skip to first unread message

Chetan Kumar Sriram

unread,
Mar 20, 2021, 12:41:55 PM3/20/21
to Ansible Project

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

Brian Coca

unread,
Mar 23, 2021, 11:56:18 AM3/23/21
to Ansible Project
You can modify the mail task to send per group:

- name: send mail to chetan
mail:
host: relay.services
port: 25
from: ad...@company.com
to: "{{web_email}}"
subject: uptime for {{item}}
body: |-
{% for server in group_names[item] %}
{% if hostvars[server]['up_time.stdout'] | default (0)
|int >= 5 %}
{{ server }} - {{"has"}}
{{hostvars[server]['up_time.stdout'] }} {{"days uptime. Consider
rebooting for better performance"}}
{% endif %}
{% endfor %}
become: no
loop: ['lab', 'web', 'db']
delegate_to: localhost
run_once: true

Another way, in you existing play change:

hosts: '{{target|default("all")}}'

And create new play:
inorder.yml

- import_playbook: send_mail.yml
vars:
target: lab

- import_playbook: send_mail.yml
vars:
target: web

- import_playbook: send_mail.yml
vars:
target: db


--
----------
Brian Coca

Chetan Kumar Sriram

unread,
Mar 25, 2021, 9:57:25 PM3/25/21
to ansible...@googlegroups.com, bc...@redhat.com
Thanks Brian for the reply,

I will try the same and get back to you.

--
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.
Reply all
Reply to author
Forward
0 new messages