# Small Example for Reading from CSV-File for the creation of new user

**URL:** <https://forum.ansible.com/t/small-example-for-reading-from-csv-file-for-the-creation-of-new-user/25097>\
**Category:** Ansible Project\
**Created:** [July 26, 2017, 4:04pm UTC](https://forum.ansible.com/t/small-example-for-reading-from-csv-file-for-the-creation-of-new-user/25097 "2017-07-26T16:04:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Steffen](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@Dan\_Steffen](https://forum.ansible.com/u/Dan_Steffen)\
**Post date:** [July 26, 2017, 4:04pm UTC](https://forum.ansible.com/t/small-example-for-reading-from-csv-file-for-the-creation-of-new-user/25097/1 "2017-07-26T16:04:45Z")

</div>

Hello,

maybe I am just blind but I spend today 2 hours to found out how to read from a CSV to create User at the moment I have something like

`

`

- hosts: all  
user:  
name=“{{ lookup(‘csvfile’, ‘item file=user.csv delimiter=, col=0’) }}”

`

`

and a CSV-File that looks like

`  
jdoe  
jkirk  
bwillis

`

I already know that I have to declare the “item” Variable so I can iterate through the user.csv but I don’t can figured out how, maybe someone here can give me a little how to define the item-Variable I would very thanksful  
best regards  
Dan

---

<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:** [July 26, 2017, 6:40pm UTC](https://forum.ansible.com/t/small-example-for-reading-from-csv-file-for-the-creation-of-new-user/25097/2 "2017-07-26T18:40:02Z")

</div>

> maybe I am just blind but I spend today 2 hours to found out how to read  
> from a CSV to create User at the moment I have something like
> 
> - hosts: all  
> &nbsp;&nbsp;user:  
> &nbsp;&nbsp;&nbsp;&nbsp;name="{{ lookup('csvfile', 'item file=user.csv delimiter=, col=0') }}"

This is not a correct start of a play, you are missing tasks: and a dash to indicate a list.

- hosts: all  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name=...

> and a CSV-File that looks like
> 
> jdoe  
> jkirk  
> bwillis

This is not a Comma Separated Value file.  
File like this one can be read by with\_lines.

- debug: msg="The user is {{ item }}"  
&nbsp;&nbsp;&nbsp;with\_lines: cat user.csv

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 26, 2017, 6:49pm UTC](https://forum.ansible.com/t/small-example-for-reading-from-csv-file-for-the-creation-of-new-user/25097/3 "2017-07-26T18:49:45Z")

</div>

Hey Dan,

your current csv file doesn’t really looks like a csv file 😃 The item variable which you are refering to is the key which jinja will use to give you the right csv- row. I am guessing that there will be some more values than the name in the future, so you could iterate over the first column with “with\_lines” and use the lookup feature to get the other values.  
Like this

tasks:

- name: Add- User Task  
user:  
name: “{{ item.split()[0] }}”  
uid: " {{ lookup(‘csvfile’, item.split(‘,’)[0] + ‘file=user.csv delimeter=, col=1’) }}" # Btw. col=1 is the default  
home: "{{ lookup(‘csvfile’, item.split(‘,’)[0] + ‘file=user.csv delimeter=, col=2’) }}  
with\_lines: cat user.csv

And your csvfile could look like this:

user.csv:  
jdoe,1000,/home/jdoe  
jkirk,1001,/home/jkirk  
bwillis,1002,/home/bwillis
