# using csvfile lookups inside a module

**URL:** <https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703>\
**Category:** Ansible Project\
**Created:** [October 3, 2016, 9:18am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703 "2016-10-03T09:18:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asil](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Asil](https://forum.ansible.com/u/Asil)\
**Post date:** [October 3, 2016, 9:18am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/1 "2016-10-03T09:18:14Z")

</div>

Hi,

I’m trying to use csvfile lookups to populate values in the groups module, but not having much luck. My csvfile:

`

# groups.csv

# name, gid [optional - leave blank], state [present|absent], system [yes|no]

accounts,502,present,no  
engineering,504,present,no

`

The playbook:

`

---

<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:** [October 3, 2016, 2:05pm UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/2 "2016-10-03T14:05:17Z")

</div>

Presumably you’ve got csv files coming from somewhere else that you want to use to drive things?

If not, I’d suggest to you see if you can use group\_vars [http://docs.ansible.com/ansible/intro\_inventory.html#group-variables](http://docs.ansible.com/ansible/intro_inventory.html#group-variables) to set variables for specific groups. Bear in mind you can have multiple groups and you can have groups which are specific to just one inventory file or are shared, so they can be pretty flexible.

I think using group\_vars would ‘go with the grain’ better than using the lookups, and give you more readable playbooks.

Probably not what you wanted to hear, but hope it helps,

Jon

---

<div class="post-metadata">

**Author:** ![Asil](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Asil](https://forum.ansible.com/u/Asil)\
**Post date:** [October 3, 2016, 3:49pm UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/3 "2016-10-03T15:49:48Z")

</div>

Thanks for replying.

At the moment no, there aren’t any csv files anywhere. Just trying out different approaches to config management using ansible. I’m comfortable with group\_vars, it’s the repetition in defining them that grates.

If you have a spare few minutes, take a look at Neil Watson’s blog, specifically [this post](http://evolvethinking.com/blog/2014/02/26/my-evolution-of-cfengine-policy/) explaining the evolution of his Cfengine policies. I’ve used this approach at a previous shop that had CFengine deployed and it worked well.  
It makes viewing and managing configuration much easier, the idea of data-driven policies appeals.It also simplifies operations view of managing the infrastructure, one line specifies multiple configuration parameters for an object.

So…not what I wanted to hear but, not the end of the world either.

Just noticed I hadn’t set the delimiter parameter, however still get the “…unable to convert to bool” message.

Asil

---

<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:** [October 3, 2016, 4:49pm UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/4 "2016-10-03T16:49:50Z")

</div>

Not read this yet but you can define cross group\_vars that are common across inventory as well as specific to inventory - it’s a trick I use to save having to copy and paste stuff which is shared across most, but not all inventory.

Thanks for the link though.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 3, 2016, 6:41pm UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/5 "2016-10-03T18:41:32Z")

</div>

> Hi,
> 
> I'm trying to use csvfile lookups to populate values in the groups module,  
> but not having much luck. My csvfile:
> 
> # groups.csv  
> # name, gid [optional - leave blank], state [present|absent], system  
> [yes|no]  
> accounts,502,present,no  
> engineering,504,present,no
> 
> The playbook:
> 
> ---
> 
> - hosts: localhost  
> &nbsp;&nbsp;become: True  
> &nbsp;&nbsp;become\_user: root
> 
> &nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;- name: get groups  
> &nbsp;&nbsp;&nbsp;&nbsp;command: /usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }' groups.csv  
> &nbsp;&nbsp;&nbsp;&nbsp;register: groups\_out
> 
> &nbsp;&nbsp;- name: Process groups  
> &nbsp;&nbsp;&nbsp;&nbsp;group: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name="{{ lookup('csvfile', 'item file=groups.csv col=0') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gid="{{ lookup('csvfile', 'item file=groups.csv col=1') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state="{{ lookup('csvfile', 'item file=groups.csv col=2') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system="{{ lookup('csvfile', 'item file=groups.csv col=3') }}"

As you mention in a later post you are missing the delimiter, TAB is the default.

And you key is literally "item" on all you lookup. To use the variable item you'll have to concatenate like this.

&nbsp;&nbsp;&nbsp;lookup('csvfile', item + ' file=groups.csv delimiter=, col=n')

> &nbsp;&nbsp;&nbsp;&nbsp;# with\_lines: "/usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }'  
> groups.csv"  
> &nbsp;&nbsp;&nbsp;&nbsp;# with\_items: "{{ groups\_out.stdout\_lines }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_lines: "{{ groups\_out.stdout\_lines }}"

You can't only have a variable in with\_items, it must be a command  
[https://docs.ansible.com/ansible/playbooks\_loops.html#iterating-over-the-results-of-a-program-execution](https://docs.ansible.com/ansible/playbooks_loops.html#iterating-over-the-results-of-a-program-execution)

> Using with\_lines and groups\_out.stdout\_lines gives me:
> 
> TASK [Process groups]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> /bin/sh: accounts: command not found

You don't have /bin/sh, that can be a problem, since default, Ansible is depending on it.

---

<div class="post-metadata">

**Author:** ![Asil](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Asil](https://forum.ansible.com/u/Asil)\
**Post date:** [October 4, 2016, 7:54am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/6 "2016-10-04T07:54:34Z")

</div>

Hi Kai,

Thanks for taking the time to reply. I did first try using with\_lines and the awk command and using “item +”, that throws up another error: This code

`

---

<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:** [October 4, 2016, 9:03am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/7 "2016-10-04T09:03:59Z")

</div>

I wonder if you might be better off using include\_vars to load all of your groups details in one pass, without multiple lookup calls.

[http://docs.ansible.com/ansible/include\_vars\_module.html](http://docs.ansible.com/ansible/include_vars_module.html)

There’s a little bit more syntax to make your csv into yaml. Instead of…  
accounts,502,present,no  
engineering,504,present,no

You’d write:

groups:

- {name: accounts, gid: 502, state: present, system: no}
- {name: engineering, gid: 504, state: present, system: no}

or

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 4, 2016, 9:12am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/8 "2016-10-04T09:12:06Z")

</div>

If you look carefully at my example I have a space in front of file.  
Without it your name would be like this on first iteration

&nbsp;&nbsp;&nbsp;lookup('csvfile', 'accountfile=groups.csv col=0 delimiter=,')

And this is not a valid lookup.

---

<div class="post-metadata">

**Author:** ![Asil](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Asil](https://forum.ansible.com/u/Asil)\
**Post date:** [October 4, 2016, 10:27am UTC](https://forum.ansible.com/t/using-csvfile-lookups-inside-a-module/22703/9 "2016-10-04T10:27:46Z")

</div>

Kai - Thank you, I missed the space! This now works as I would have hoped.

Jon - Thank you too! Using a include\_vars rather than a lookup achieves the same goal, the single-line format of each item was something I completely forgot you could do, for some reason I’ve adopted the multi-line format and never let go.

Kind regards,

Asil
