# lookup csvfile with\_items issue

**URL:** <https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705>\
**Category:** Ansible Project\
**Created:** [July 7, 2015, 5:56pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705 "2015-07-07T17:56:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_Pit](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Bart\_Pit](https://forum.ansible.com/u/Bart_Pit)\
**Post date:** [July 7, 2015, 5:56pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705/1 "2015-07-07T17:56:29Z")

</div>

Hi! I’m struggling to get ‘lookup’ with ‘csvfile’ in combination with ‘with\_items’ to work.

I have this csv file (shortened snippet):

`"col1","col2" 1,"Mike" 2,"Sally"`

Than I also have this var:

`  
test\_ids:

- 1
- 2  
`

Now I want to lookup (docs: [https://docs.ansible.com/playbooks\_lookups.html#the-csv-file-lookup](https://docs.ansible.com/playbooks_lookups.html#the-csv-file-lookup)) the values in the second column of the csv file:

`

- name: read from csv file  
debug: msg=“{{ lookup(‘csvfile’, ‘item file=/path/to/file.csv delimiter=, col=1’) }}”  
with\_items: test\_ids  
`

Where ‘item’ before file=/… should output 1 and 2 right??

This doesn’t work, output is:

`TASK: [client-setup | read from csv file] ******** ok: [localhost] => (item=1) => { "item": 1, "msg": "[]" } ok: [localhost] => (item=2) => { "item": 2, "msg": "[]" }`

As you can see it recognizes 1 and 2, but I don’t get the values of the second column in the csv.

If I hardcode the number in like so:

`

- name: read from csv file  
debug: msg=“{{ lookup(‘csvfile’, ‘1 file=test.csv delimiter=, col=1’) }}”  
with\_items: test\_ids

`

It does give me the correct result:

`TASK: [client-setup | read from csv file] ******** ok: [localhost] => (item=1) => { "item": 1, "msg": "Mike" } ok: [localhost] => (item=2) => { "item": 2, "msg": "Mike" }`

I tried quoting the var with ‘1’ and ‘2’, but that doesn’t work either.

Is this a bug or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 7, 2015, 8:23pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705/2 "2015-07-07T20:23:15Z")

</div>

you are forcing item to be a static string, it needs to be like this:

- name: read from csv file  
&nbsp;&nbsp;debug: msg="{{ lookup('csvfile', item + ' file=/path/to/file.csv  
delimiter=, col=1') }}"  
&nbsp;&nbsp;with\_items: test\_ids

---

<div class="post-metadata">

**Author:** ![Bart\_Pit](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Bart\_Pit](https://forum.ansible.com/u/Bart_Pit)\
**Post date:** [July 7, 2015, 8:57pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705/3 "2015-07-07T20:57:47Z")

</div>

Thanks Brian! It doing something more now, that’s good 🙂

However, I now get this error:

`fatal: [localhost] => Failed to template msg="{{ lookup('csvfile', item + ' file=test.csv delimiter="," col=2') }}": csvfile: "delimiter" must be string, not unicode`

I tried quoting the comma, double and single. And tried the approach in this thread, both single and double: [https://groups.google.com/forum/#!topic/ansible-project/r2L3UONwaN0](https://groups.google.com/forum/#!topic/ansible-project/r2L3UONwaN0) like so:

`debug: msg="{{ lookup('csvfile', item + ' file=test.csv delimiter='","' col=2') }}"`

But also doesn’t work.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Bart\_Pit](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Bart\_Pit](https://forum.ansible.com/u/Bart_Pit)\
**Post date:** [July 7, 2015, 9:55pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705/4 "2015-07-07T21:55:33Z")

</div>

For extra clarification: I started getting this error without the quotes around the comma. So like this, produces the same error:

`debug: msg="{{ lookup('csvfile', item + ' file=test.csv delimiter=, col=2') }}"`

---

<div class="post-metadata">

**Author:** ![Bart\_Pit](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Bart\_Pit](https://forum.ansible.com/u/Bart_Pit)\
**Post date:** [July 8, 2015, 7:02pm UTC](https://forum.ansible.com/t/lookup-csvfile-with-items-issue/18705/5 "2015-07-08T19:02:46Z")

</div>

Any ideas on the

`"delimiter" must be string, not unicode`

error?
