Search code examples
listansiblejinja2

Ansible: add many attachments to email only if a respective condition for each attachment is met


In this thread it is explained how to add a single attachment to a mail sent via community.general.mail module only if a condition is met.

Now I would like to add as attachment many files from a list of attachments if some (="their") respective condition is met.

I have tried to do so by defining the list of files to attach is

list_of_attachments_paths: 
  "{% set attachments_list = [] %}
  {% if NOL_in_file1.stdout|int > 0  %}
  {{ attachments_list.append( file1_path ) }}
  {% endif %}
  {% if NOL_in_file2.stdout|int > 0  %}
  {{ attachments_list.append( file2_path ) }}
  {% endif %}
  {{ attachments_list }}"

a debug task

    - name: show list_of_attachments_paths
      debug:
        msg: 
          - "{{ list_of_attachments_paths }}"

and this the task to send the mail

- name: Send the analysis via e-mail 
  community.general.mail:

    subtype: html  
    host: smtp.myserver.it
    port: 25
    sender: automated-sender@myserver.it
    to:
      "{{ mail_to_recipients }}"
    cc:
      "{{ mail_cc_recipients }}"


    subject: "[automated-sender] my analysis"
    attach:
      - "{{ list_of_attachments_paths }}"

    body: "{{ body_analysis }}"

the debug task returns:

show list_of_attachments_paths...   
  localhost ok: {    
    "changed": false,    
    "msg": [    
        "       ['/home/myuser/mypath/file1.csv', '/home/myuser/mypath/file2.csv']"

    ]

}

but I get this error and I don't understand why

Send the report via e-mail...

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: FileNotFoundError: [Errno 2] No such file or directory: "       ['/home/myuser/mypath/file1.csv'"

  localhost failed | msg: Failed to send community.general.mail: can't attach file        ['/home/myuser/mypath/file1.csv': [Errno 2] No such file or directory: "       ['/home/myuser/mypath/file1.csv'"

on ansible documentation for task key attach it is clearly stated that the input must be a list of path of attachments, so what is the problem here?


Solution

  • I have found a (very bad) way to solve the problem:

        - name: Generate void list of attachment paths
          set_fact:
            list_of_attachments_paths: "{{ [] }}"
    
    
        - name: Update list of attachment paths - file1
          when: NOL_in_file1.stdout|int > 0
          set_fact:
            list_of_attachments_paths: "{{ list_of_attachments_paths + [file1_path]  }}"
    
    
        - name: Update list of attachment paths - file2
          when: NOL_in_file2.stdout|int > 0
          set_fact:
            list_of_attachments_paths: "{{ list_of_attachments_paths + [file2_path]  }}"
    

    and then

    - name: Send the analysis via e-mail 
      community.general.mail:
    
        subtype: html  
        host: smtp.myserver.it
        port: 25
        sender: automated-sender@myserver.it
        to:
          "{{ mail_to_recipients }}"
        cc:
          "{{ mail_cc_recipients }}"
    
    
        subject: "[automated-sender] my analysis"
        attach:
          "{{ list_of_attachments_paths | select('defined') | list }}"  # note the select('defined) filter
    
        body: "{{ body_analysis }}"