Ansible mail module: how to attach a file with a relative path?

1,730 views
Skip to first unread message

Sabrina Lautier

unread,
Sep 17, 2018, 6:13:48 AM9/17/18
to Ansible Project
Hi everyone,

Is it possible to provide a relative path to a file as attach parameter of the ansible mail module?
From my tests and internet searches, it doesn't work: in my example, the playbook calls the task that is in directory structure ./roles/notify/tasks/send_email.yml and the file I need to attach is in ./roles/notify/files.

Directory structure
./roles/notify/
 
- files: contains the file to send
 
- tasks: send_email.yml

Task "Send email":
name: Send email
  mail
:
    host
: "{{ hostvars[groups['smtp'][0]]['ansible_host'] }}"
   
from: "{{ from_address }}"
    to
: "{{ to_recipients }}"
    cc
: "{{ cc_recipients }}"
    attach
: "./files/{{ attached_file }}"
    subject
: "{{ subject }}"
    body
: "{{ body }}"
    subtype
: "html"

I also tried with those combinations but none works:
attach: "{{ attached_file }}"
attach: "./roles/notify/files/{{ attached_file }}"

Haven't I got any other option but copying the file to a temporary directory and provide an absolute path to it?

Many thanks for your help.

Best regards,
Sabrina

Sabrina Lautier

unread,
Sep 17, 2018, 10:38:21 AM9/17/18
to Ansible Project
[I forgot to mention that the attached file path can change from one execution to another, that's why I cannot provide an absolute path]

It works fine if I copy the attached file to a non-changing directory (e.g. /tmp), but the task fails if no attached file is provided.

- name: Copy attached file to /tmp
  copy:
    src: "./files/{{ attached_file }}"
    dest: "/tmp"

name: Send email
  mail
:
    host
: "{{ hostvars[groups['smtp'][0]]['ansible_host'] }}"
   
from: "{{ from_address }}"
    to
: "{{ to_recipients }}"
    cc
: "{{ cc_recipients }}"

   
attach: "//tmp/{{ attached_file }}"

    subject
: "{{ subject }}"
    body
: "{{ body }}"
    subtype
: "html"

I don't like the idea of having 2 nearly identical blocks, one for the case there's an attachment and another when there's none.
If there's no cc (i.e. cc is not defined), the mail module doesn't fail. Isn't there a way to do the same with the attach parameter?

Sabrina Lautier

unread,
Sep 27, 2018, 7:43:55 AM9/27/18
to Ansible Project
When there's not attachment, setting "" works fine.

name: Send email
  mail
:
    host
: "{{ smtp_server }}"

   
from: "{{ from_address }}"
    to
: "{{ to_recipients }}"
    cc
: "{{ cc_recipients }}"
   
attach
: ""

    subject
: "{{ subject }}"
    body
: "{{ body }}"
    subtype
: "html"
..

So setting a variable to either "" if there's not attachment or the file to attach if there's one prevents the duplication of the block.

On Monday, September 17, 2018 at 12:13:48 PM UTC+2, Sabrina Lautier wrote:
Hi everyone,

Is it possible to provide a relative path to a file as attach parameter of the ansible mail module?
From my tests and internet searches, it doesn't work: in my example, the playbook calls the task that is in directory structure ./roles/notify/tasks/send_email.yml and the file I need to attach is in ./roles/notify/files.

Directory structure
./roles/notify/
 
- files: contains the file to send
 
- tasks: send_email.yml

Task "Send email":
name: Send email
  mail
:
    host
: "{{ smtp_server }}"

   
from: "{{ from_address }}"
    to
: "{{ to_recipients }}"
    cc
: "{{ cc_recipients }}"
    attach
: "./files/{{ attached_file }}"
    subject
: "{{ subject }}"
    body
: "{{ body }}"
    subtype
: "html"

I also tried with those combinations but none works:
Reply all
Reply to author
Forward
0 new messages