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

10 views
Skip to first unread message

Tran Khang

unread,
Sep 5, 2019, 3:21:19 AM9/5/19
to Ansible Project
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_g...@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:

Backup files list:
- /20190625/AS/mcpAcct.2019_06_25.16_46_45.tar
- /20190625/AS/mcpAcct.2019_06_25.16_51_33.tar

Dick Visser

unread,
Sep 5, 2019, 3:59:54 AM9/5/19
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e54b4129-e863-4757-a16e-16dbf8f60dcf%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Tran Khang

unread,
Sep 5, 2019, 4:27:10 AM9/5/19
to Ansible Project
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

Dick Visser

unread,
Sep 5, 2019, 5:12:53 AM9/5/19
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/aa8c88ee-953b-418b-8d03-c7a4c60de12f%40googlegroups.com.

Tran Khang

unread,
Sep 5, 2019, 5:22:17 AM9/5/19
to Ansible Project
Thanks Visser a lots, it works exactly as expected.


On Thursday, September 5, 2019 at 4:12:53 PM UTC+7, Dick Visser wrote:
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

On Thu, 5 Sep 2019 at 10:27, Tran Khang <trank...@gmail.com> wrote:
>
> 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
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages