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

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

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') }}"

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

- hosts: all
   tasks:
     - user:
         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 }}"
   with_lines: cat user.csv

Hey Dan,

your current csv file doesn’t really looks like a csv file :smiley: 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