"Dirty" find printout question

40 views
Skip to first unread message

Alex Wanderley

unread,
Mar 26, 2024, 6:36:08 PM3/26/24
to ansible...@googlegroups.com
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
--

 

Edmonton_sig_RGB_S.jpg

Alex Wanderley

Application and Infrastructure Analyst II
Server Solutions & Automation

Financial and Corporate Services | Open City and Technology  

 

780-496-4156  Office

780-819-0273  Mobile

 

City of Edmonton

Century Place, 19th Floor

9803 102A Avenue NW

Edmonton AB, T5J 3A3

 

All information contained in this email post is proprietary to the City of Edmonton, confidential and intended only for the addressed recipient. If you have received this post in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination or distribution of this email, if misdirected, is strictly prohibited.


The contents of this message and any attachment(s) are confidential, proprietary to the City of Edmonton, and are intended only for the addressed recipient. If you have received this in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination, or distribution of this message, if misdirected, is strictly prohibited.

Matt Martz

unread,
Mar 26, 2024, 6:38:04 PM3/26/24
to ansible...@googlegroups.com
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

--
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/CADp8UUQpNfDY02hCi_emzz_M6%2BB8-xE%3DaotbdVdSNsVPKHc7aw%40mail.gmail.com.


--
Matt Martz
@sivel
sivel.net

Alex Wanderley

unread,
Mar 26, 2024, 6:43:56 PM3/26/24
to ansible...@googlegroups.com
Thanks for the quick reply, Matt...!

I'll take a look at that...

Alex

Alex Wanderley

unread,
Mar 26, 2024, 7:17:11 PM3/26/24
to ansible...@googlegroups.com
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      


Dick Visser

unread,
Mar 27, 2024, 3:53:13 AM3/27/24
to ansible...@googlegroups.com
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') }}"



Alex Wanderley

unread,
Mar 27, 2024, 11:38:31 AM3/27/24
to ansible...@googlegroups.com
Yup, a more concise way and worked the same...!

Thanks for that, Dick!

Reply all
Reply to author
Forward
0 new messages