theory filters troubles (with_items expects a list)

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:

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.

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

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.

have you tried?:
with_items: almatables | difference(supertables)| list

So implication is it didn’t take a set ?

(If so, that’s fixable)

Oops, sorry Brian, i replied to you personally :slight_smile:

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.

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

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

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!

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