[find module][mail module] Report only file name list

Hi all,

I’m trying combine find module and mail module to report to my email only the list of files I need that were finded in specific folder. I try many ways but always receive full output. Do anyone can help?

My tasks


  • name: check if tar files exists
    find:
    paths: “{{ bkPath }}”
    patterns: ‘*.tar’
    register: backup_existed

  • name: results output
    debug:
    msg: “{{ item.path }}”
    with_items: “{{ backup_existed.files }}”
    register: backup_output

  • name: Report through email
    mail:
    host: smtp.gmail.com
    port: 587
    username: my_gmail@gmail.com
    password: SecretPass
    to: Khang Tran my_working@email
    subject: Ansible-Backup-Report
    body: >
    Backup files list:

  • {{ backup_output }}
    charset: utf-8
    delegate_to: localhost

Actually Email report:

Backup files list: - {‘msg’: u’/20190625/AS/mcpAcct.2019_06_25.16_46_45.tar’, ‘_ansible_verbose_always’: True, ‘_ansible_ignore_errors’: True, ‘_ansible_item_label’: {u’uid’: 1003, u’woth’: False, u’mtime’: 1561456341.9453063, u’inode’: 596799269, u’isgid’: False, u’size’: 3205120, u’roth’: True, u’isuid’: False, u’isreg’: True, u’pw_name’: u’labbackup’, u’gid’: 1003, u’ischr’: False, u’wusr’: True, u’xoth’: False, u’rusr’: True, u’nlink’: 1, u’issock’: False, u’rgrp’: True, u’gr_name’: u’labbackup’, u’path’: u’/20190625/AS/mcpAcct.2019_06_25.16_46_45.tar’, u’xusr’: False, u’atime’: 1561456341.6703176, u’isdir’: False, u’ctime’: 1561456341.9453063, u’isblk’: False, u’xgrp’: False, u’dev’: 64770, u’wgrp’: True, u’isfifo’: False, u’mode’: u’0664’, u’islnk’: False}}, {‘changed’: False, ‘_ansible_no_log’: False, ‘item’: {u’uid’: 1003, u’woth’: False, u’mtime’: 1561456341.951306, u’inode’: 596799270, u’isgid’: False, u’size’: 40960, u’roth’: True, u’isuid’: False, u’isreg’: True, u’pw_name’: u’labbackup’, u’gid’: 1003, u’ischr’: False, u’wusr’: True, u’xoth’: False, u’rusr’: True, u’nlink’: 1, u’issock’: False, u’rgrp’: True, u’gr_name’: u’labbackup’, u’path’: u’/20190625/AS/mcpAcct.2019_06_25.16_51_33.tar’, u’xusr’: False, u’atime’: 1561456341.9463062, u’isdir’: False, u’ctime’: 1561456341.951306, u’wgrp’: True, u’xgrp’: False, u’dev’: 64770, u’isblk’: False, u’isfifo’: False, u’mode’: u’0664’, u’islnk’: False}, ‘_ansible_item_result’: True, ‘failed’: False }

Expected Email report contain:

Skip the debug task, and use backup_existed in your mail task.
You do have to filter that down to a list of files, and then iterate
over that in jinja in the body parameter.

Hi Visser,

I’m an ansible newbie, could you please help me in detail how to filter down and iterate in jinja in body parameter?

Regards,

Khang Tran

Remove the debug task.
And use this in the mail task:

        body: >
          Backup list
          {% for file in backup_existed.files %}
          - {{ file.path }}
          {% endfor %}

You might need to tweak the newlines and formatting a bit.

Dick

Thanks Visser a lots, it works exactly as expected.