# theory filters troubles (with\_items expects a list)

**URL:** <https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350>\
**Category:** Ansible Project\
**Created:** [May 28, 2014, 7:41am UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350 "2014-05-28T07:41:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjdeelman](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pjdeelman](https://forum.ansible.com/u/pjdeelman)\
**Post date:** [May 28, 2014, 7:41am UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/1 "2014-05-28T07:41:42Z")

</div>

Trying to figure out theory filters, but no luck yet. I’m pulling a list of available tables from postgres and exclude the tables contained in the list supertables.  
Am i using this functionality in the correct way? Or do i need to put it in a variable first? If so, how do i do that?  
I’m using ansible 1.4.4+dfsg-1~ubuntu12.04.1

My playbook:

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 28, 2014, 12:05pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/2 "2014-05-28T12:05:05Z")

</div>

Jinja2 filters return strings, not arrays, so use a “when” statement to skip lines you do not want to hit:

tasks:

- name: dump data into csv  
sudo: yes  
sudo\_user: postgres

command: psql -d cmdbuild -c "COPY "{{ item }}" to ‘/tmp/{{ item }}.csv’ $  
when: item not in supertables  
with\_items: almatables.stdout\_lines

This should do what you want.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 12:13pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/3 "2014-05-28T12:13:29Z")

</div>

I think you might have been skimming this one a bit fast, James 🙂

I don’t believe it’s true that filters always return strings.

A simple example other than this is “fileglob” which returns a list.

I also don’t understand why a when statement, when executed inside the loop, would have anything to do with the “with\_items expects a list error”.

It shouldn’t.

I think we should dig into this one a little bit more.

It may be something else still in the above example vs a code problem, though those explanations don’t seem to be it to me.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 28, 2014, 12:13pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/4 "2014-05-28T12:13:46Z")

</div>

have you tried?:  
with\_items: almatables | difference(supertables)| list  
​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 12:15pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/5 "2014-05-28T12:15:35Z")

</div>

So implication is it didn’t take a _set_ ?

(If so, that’s fixable)

---

<div class="post-metadata">

**Author:** ![pjdeelman](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pjdeelman](https://forum.ansible.com/u/pjdeelman)\
**Post date:** [May 28, 2014, 2:41pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/6 "2014-05-28T14:41:24Z")

</div>

Oops, sorry Brian, i replied to you personally 🙂

Yes it did the trick!  
PLAY [pdetest2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [pdetest2]

TASK: [dump data into csv] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [pdetest2] =\> (item=ALMA\_VirtualComputer)

But i agree with Michael, this should be fixable. At least in the documentation.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 6:00pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/7 "2014-05-28T18:00:34Z")

</div>

I’d rather have the code just make it work – less to remember for everyone.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 28, 2014, 9:50pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/8 "2014-05-28T21:50:29Z")

</div>

The issue is the {{ }} tends to convert output to strings so “| list” might be a requirement when no more ‘templetizing’ occurs​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 29, 2014, 6:05pm UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/9 "2014-05-29T18:05:41Z")

</div>

There’s been a lot of work to make variables keep their types.

I’m not sure that’s true, especially as “{{” is left off here.

In either case, there needs to be a github ticket filed so it can be investigated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 3, 2014, 1:45am UTC](https://forum.ansible.com/t/theory-filters-troubles-with-items-expects-a-list/15350/10 "2014-06-03T01:45:05Z")

</div>

My last patch makes sure the functions always return a set or a list.  
​
