how to loop inventory groups in ansible

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

You can modify the mail task to send per group:

        - name: send mail to chetan
          mail:
            host: relay.services
            port: 25

Thanks Brian for the reply,

I will try the same and get back to you.