theory filters troubles (with_items expects a list)

200 views
Skip to first unread message

pjde...@gmail.com

unread,
May 28, 2014, 3:41:42 AM5/28/14
to ansible...@googlegroups.com
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:
---
- hosts: pdetest2
  vars:
    supertables:
    - ALMA_asset
    - ALMA_computer
  tasks:
  - name: get list of ALMA tables
    sudo: yes
    sudo_user: postgres
    command: psql cmdbuild -t -c "select tablename from pg_catalog.pg_tables where tablename like '%ALMA_%' ORDER BY tablename;"
    register: almatables
 
  - name: debug
    debug: var=supertables
 
  - name: debug
    debug: var=almatables.stdout_lines
 
  - name: dump data into csv
    sudo: yes
    sudo_user: postgres
    command: psql -d cmdbuild -c "COPY \"{{ item }}\" to '/tmp/{{ item }}.csv' CSV;"
    with_items: almatables.stdout_lines | difference(supertables)

Ansible fails with:
TASK: [debug] *****************************************************************
ok: [pdetest2] => {
    "supertables": [
        "ALMA_asset",
        "ALMA_computer"
    ]
}
 
TASK: [debug] *****************************************************************
ok: [pdetest2] => {
    "almatables.stdout_lines": [
        " ALMA_Asset",
        " ALMA_Building",
        " ALMA_Building_history",
        " ALMA_Cabinet",
        " ALMA_Cabinet_history",
        " ALMA_Certificate",
<snip>
        " Map_ALMA_Software_InstalledSoftware_history"
    ]
}
 
TASK: [dump data into csv] ****************************************************
fatal: [pdetest2] => with_items expects a list
 
FATAL: all hosts have already failed -- aborting

So i tried to dumb it down:
- hosts: pdetest2
  vars:
    almatables:
    - ALMA_asset
    - ALMA_VirtualComputer
    supertables:
    - ALMA_asset
    - ALMA_computer
  tasks:
  - name: dump data into csv
    sudo: yes
    sudo_user: postgres
    command: psql -d cmdbuild -c "COPY \"{{ item }}\" to '/tmp/{{ item }}.csv' $
    with_items: almatables | difference(supertables)

Fails with the same error:
TASK: [dump data into csv] ****************************************************
fatal: [pdetest2] => with_items expects a list

James Cammarata

unread,
May 28, 2014, 8:05:10 AM5/28/14
to ansible...@googlegroups.com
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.



--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/62d0e894-4f00-4900-8cf1-c47fbd0bdbe2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
May 28, 2014, 8:13:32 AM5/28/14
to ansible...@googlegroups.com
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.




Brian Coca

unread,
May 28, 2014, 8:13:49 AM5/28/14
to ansible...@googlegroups.com
have you tried?:
 with_items: almatables | difference(supertables)| list

Michael DeHaan

unread,
May 28, 2014, 8:15:37 AM5/28/14
to ansible...@googlegroups.com
So implication is it didn't take a *set* ?

(If so, that's fixable)




On Wed, May 28, 2014 at 8:13 AM, Brian Coca <bria...@gmail.com> wrote:
have you tried?:
 with_items: almatables | difference(supertables)| list

--
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 post to this group, send email to ansible...@googlegroups.com.

pjde...@gmail.com

unread,
May 28, 2014, 10:41:24 AM5/28/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
May 28, 2014, 2:00:37 PM5/28/14
to ansible...@googlegroups.com
I'd rather have the code just make it work -- less to remember for everyone.




--
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 post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
May 28, 2014, 5:50:32 PM5/28/14
to ansible...@googlegroups.com
The issue is the {{ }} tends to convert output to strings so  "| list" might be a requirement when no more 'templetizing' occurs​

Michael DeHaan

unread,
May 29, 2014, 2:05:46 PM5/29/14
to ansible...@googlegroups.com
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!


On Wed, May 28, 2014 at 5:50 PM, Brian Coca <bria...@gmail.com> wrote:
The issue is the {{ }} tends to convert output to strings so  "| list" might be a requirement when no more 'templetizing' occurs​

--
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 post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
Jun 2, 2014, 9:45:10 PM6/2/14
to ansible...@googlegroups.com
My last patch makes sure the functions always return a set or a list.
Reply all
Reply to author
Forward
0 new messages