"Dirty" find printout question

Hello,

We have a simple/working playbook that looks for log files based on the server’s name and prints the file(s) it could find:

vars:
log_files: “/path/to/directory”
tasks:

  • name: Collect log files
    ansible.builtin.find:
    path: “{{ log_files }}/”
    patterns: “{{ inventory_hostname }}
    register: log_search

  • name: print collection result
    ansible.builtin.debug:
    msg: “{{ item.path }}”
    with_items: “{{ log_search.files }}”

For each file found, the the printout looks like this:
ok: [SERVER_NAME] => (item={‘path’: ‘/path/to/directory/logs/monitor-<SERVER_NAME>-02-2024.03.18.log’, ‘mode’: ‘0644’,
‘isdir’: False, ‘ischr’: False, ‘isblk’: False, ‘isreg’: True, ‘isfifo’: False, ‘islnk’: False, ‘issock’: False, ‘uid’: 999, ‘gid’: 999,
‘size’: 272091, ‘inode’: 12956486, ‘dev’: 45, ‘nlink’: 1, ‘atime’: 1710905864.848738, ‘mtime’: 1710827942.815553, ‘ctime’: 1710827942.815553,
‘gr_name’: ‘xxxxx’, ‘pw_name’: ‘xxxxx’, ‘wusr’: True, ‘rusr’: True, ‘xusr’: False, ‘wgrp’: False, ‘rgrp’: True, ‘xgrp’: False, ‘woth’: False,
‘roth’: True, ‘xoth’: False, ‘isuid’: False, ‘isgid’: False}) => {
“msg”: “/path/to/directory/logs/monitor-<SERVER_NAME>-02-2024.03.18.log”
}

How could I, if even possible, format the printout so it would only show the whole path to the file, omitting/discarding all the other file attributes information?
(And please forgive my ignorance…)

Thanks a lot,

Alex

The functionality you are looking for is the label option of loop_control: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#limiting-loop-output-with-label

Thanks for the quick reply, Matt…!

I’ll take a look at that…

Alex

Yes, it worked like a charm by going with:

  • name: print collection result
    ansible.builtin.debug:
    msg: “{{ item.path }}”
    loop: “{{ log_search.files }}”
    loop_control:
    label: “{{ item.path }}”

Again, thanks a lot!

Alex

It may be easier and less noisy to pick the path attribute and drop the loop altogether:

  • name: print collection result
    ansible.builtin.debug:
    msg: “{{ log_search.files | map(attribute=‘path’) }}”

Yup, a more concise way and worked the same…!

Thanks for that, Dick!