Looping over email addresses in Mail module

182 views
Skip to first unread message

david....@bmt.ky

unread,
Nov 4, 2021, 11:43:51 AM11/4/21
to Ansible Project
Hey everyone,

I'm still a bit new to Ansible, so sorry if this has an obvious answer

Basically, I've got a list of email addresses that I want to send an email to one at a time and I'm using the Community.General Mail module to send the emails. I can get it working with a static list of emails that I manually enter into the recipient list.

However when I try to loop over a list of emails, it looks like Ansible just skips over the task and doesn't even try to send an email. I've tried using garbage in the list in an attempt to throw an error, but nothing. 

Here's the code I'm using

[...]
  mail:
    host: "{{ smtp_host_var }}"
    port: "{{ smtp_port_var }}"
    username: "{{ smtp_username_var }}"
    password: "{{ smtp_password_var }}"
    to: "{{ item.email }}"
    from: he...@example.com
    subject: "Welcome" Email
    body: "{{ New_User_Email_Body }}"
  with_items: "{{ users_to_add.objects }}"
  delegate_to: localhost
[...]


And the results I get

[...]

TASK [Send Welcome Email] **********************************************************************
task path: /.../tasks/Welcome_Email.yml:16
META: ran handlers
META: ran handlers

PLAY RECAP ***********************************************************
1.1.1.1              : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

Any help would be much appreciated!

Thanks
Dave

Todd Lewis

unread,
Nov 4, 2021, 11:59:05 AM11/4/21
to Ansible Project
I'm afraid the problem is in the first "[...]". I.e., we'll need to see more context. First thing I'd do if I were you is stick a debug task right before this (unnamed?) task to dump the value of users_to_add.objects. Guessing it isn't what you think it is. (And we have no clue.)

david....@bmt.ky

unread,
Nov 4, 2021, 4:45:50 PM11/4/21
to Ansible Project
Thanks for the feedback

Turns out you were exactly right - the list I was trying to iterate over was empty, hence the output being blank and obviously no email sending.

Appreciate the help!
Reply all
Reply to author
Forward
0 new messages