# Lookup in a csv file with a multiple field key

**URL:** <https://forum.ansible.com/t/lookup-in-a-csv-file-with-a-multiple-field-key/21296>\
**Category:** Ansible Project\
**Created:** [April 28, 2016, 10:46am UTC](https://forum.ansible.com/t/lookup-in-a-csv-file-with-a-multiple-field-key/21296 "2016-04-28T10:46:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 28, 2016, 10:46am UTC](https://forum.ansible.com/t/lookup-in-a-csv-file-with-a-multiple-field-key/21296/1 "2016-04-28T10:46:32Z")

</div>

Hello,

my csv file has the “,” delimiter and is this way:

buddy,dv19,toto  
eric,dv05,toto  
buddy,dv05,toto

I can easily use the lookup command on a single key like buddy or eric or dv19 or dv05.  
How could I proceed to select on a multiple key like “buddy,dv19” ?

I tried to set a variable to buddy,dv1ç and use it this way, it cannot be found:

…  
doublekey: buddy,dv19

…

- debug: msg=“the value of doublekey is {{ lookup(‘csvfile’, ‘{} file=/home/fanvalt/parameter\_playbook/param.csv delimiter=, col=0’.format(doublekey)) }}”

any clue ?

Regards

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 28, 2016, 11:57am UTC](https://forum.ansible.com/t/lookup-in-a-csv-file-with-a-multiple-field-key/21296/2 "2016-04-28T11:57:27Z")

</div>

Not sure what the csvfile lookup plugin returns, but since you are inside {{ }} I’m guessing you can using jinja filters, so you might be able to use ‘selectattr’ to pick only those results which have the secondary key.

See the filters documentation here: [http://jinja.pocoo.org/docs/dev/templates/#builtin-filters](http://jinja.pocoo.org/docs/dev/templates/#builtin-filters)

Hope this helps

Jon

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [April 28, 2016, 2:36pm UTC](https://forum.ansible.com/t/lookup-in-a-csv-file-with-a-multiple-field-key/21296/3 "2016-04-28T14:36:49Z")

</div>

Thank you but lookup returns a field that is not the key in my case, I didn’t find out how to use selectattr.

Because I cannot find out how to select on a multiple key, I tried to figure how to read all records from the file, beczuse lookup only select the 1st line found on the key value.  
If I try to use the grep unix command, how to store the records in an array or by any way ?

Regards
