# Loop with a filter applied to a list

**URL:** <https://forum.ansible.com/t/loop-with-a-filter-applied-to-a-list/27972>\
**Category:** Ansible Project\
**Created:** [July 6, 2018, 10:30am UTC](https://forum.ansible.com/t/loop-with-a-filter-applied-to-a-list/27972 "2018-07-06T10:30:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [July 6, 2018, 10:30am UTC](https://forum.ansible.com/t/loop-with-a-filter-applied-to-a-list/27972/1 "2018-07-06T10:30:53Z")

</div>

Hi all,

I'm having trouble creating a list of file names to loop over. I've tried  
this, but it doesn't work:

- name: Debug  
debug:  
msg: "{{ item }}"  
loop: "{{ query('fileglob', 'dir/\*') | basename) }}"

I get an error:

An exception occurred during task execution. To see the full traceback, use  
-vvv. The error was: AttributeError: 'list' object has no attribute 'rfind'

I'm using Ansible 2.5.5

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 6, 2018, 11:21am UTC](https://forum.ansible.com/t/loop-with-a-filter-applied-to-a-list/27972/2 "2018-07-06T11:21:43Z")

</div>

query return a list, basename can't run on a list only string, so move you basename to the item in msg.

---

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [July 6, 2018, 12:41pm UTC](https://forum.ansible.com/t/loop-with-a-filter-applied-to-a-list/27972/3 "2018-07-06T12:41:27Z")

</div>

Got it: loop: "{{ query('fileglob', 'dir/\*') | map('basename') | list) }}"
