简体   繁体   中英

Is it possible to send one email with multiple attachments on Ansible using Jinja2 templates

My yml playbook generates a file for each host it runs on and then sends out an email containing the file. Right now, it sends out one email per host per file, I was wondering if it was possible to attach multiple files from multiple hosts to the same email and I think Jinja2 templating is the way to go.

This is my Jinja template, I know I'm not using the hosts loop correctly because it doesn't loop through the hosts, it does one host and then ends.

{% for host in ansible_play_hosts %}
{% for item in files_to_email.files -%}
{{ item.path }}
{%- endfor %}
{% endfor% }

Here is the relevant part of the yml playbook

- name: get the file
  find:
    paths: /mypath
    patterns: '.*yumlist.*'
    use_regex: yes
  register: file_to_email

- name: email files
  mail: 
    from: noreply@test.com
    subject: test
    to:
      -me@myemailaddress
    body: test
    attach:
    - "{{ lookup('template', 'mail_body.j2') }}"
  run_once: true

If my filenames were file1 and file2 on host 1 and host 2 respectively, I get an error saying "fatal: [host1]: FAILED: can't attach file /mypath/file1/mypath/file1\n: [Errno 20] Not a directory, /mypath/file1/mypath/file1\n: rc: 1

Doesn't even bother with looking at the second host so I'm at a loss.

Q: "Send one email with multiple attachments."

A: Let's have a group of hosts

group2:
  hosts:
    test_01:
    test_02:
    test_03:
  1. Create a temporary directory at localhost
  2. Fetch all files to this directory
  3. Create a list of the files to be attached
  4. Send the email and attach the list of files
  5. Remove the temporary directory

For example:

- hosts: group2
  tasks:
    - tempfile:
        state: directory
      register: attach_dir
      delegate_to: localhost
      run_once: true

    - fetch:
        dest: "{{ attach_dir.path }}"
        src: /etc/passwd

- hosts: localhost
  tasks:
    - set_fact:
        attach_dir: "{{ hostvars[groups.group2.0]['attach_dir'] }}"
    - find:
        paths: "{{ attach_dir.path }}"
        recurse: true
      register: files_found

    - set_fact:
        files_to_email: "{{ files_found.files|json_query('[].path') }}"

    - mail: 
        from: noreply@localhost
        subject: Ansible test
        to:
          - root@localhost
        body: |
          List of attached files
          {% for file in files_to_email %}
          {{ file }}
          {% endfor %}
        attach: "{{ files_to_email }}"

    - file:
        state: absent
        path: "{{ attach_dir.path }}"

The email should look similar to this example

From: noreply@localhost
To: root@localhost
Cc: 
Subject: Ansible test
Date: Fri, 13 Dec 2019 02:35:19 +0100
X-Mailer: Ansible mail module

List of attached files
/tmp/ansible.kf2wHf/test_01/etc/passwd
/tmp/ansible.kf2wHf/test_02/etc/passwd
/tmp/ansible.kf2wHf/test_03/etc/passwd

[passwd  application/octet-stream (2294 bytes)] 
[passwd  application/octet-stream (2294 bytes)] 
[passwd  application/octet-stream (2294 bytes)] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM