Create file who is in my csv file

Hey all !! :slight_smile:

I have csv file with this content :

filename,state,note,temps
file1,0,my fisrt note,52
file2,0,my second note,34

file3,0,my thirst note,71

i have this task :

  • set_fact:
    csvfile: “{{ lookup(‘csvfile’, ‘file=/home/ansible/mssql_query/roles/files/csv_file.csv delimiter=,’) }}”

but not works, my goal is that create all file sucessively (file1, file2, file3, …) in some repertory

Someone have any idea please ?

Thanks very much community ansible !! :wink:

Regards,

Karther

You might be better off with the 'read_csv' module
https://docs.ansible.com/ansible/latest/modules/read_csv_module.html#read-csv-read-a-csv-file

For example

    - hosts: localhost
      tasks:
        - read_csv:
            path: csv_file.csv
          register: my_files
        - debug:
            var: my_files.list

gives

    "my_files.list": [
        {
            "filename": "file1",
            "note": "my fisrt note",
            "state": "0",
            "temps": "52"
        },
        {
            "filename": "file2",
            "note": "my second note",
            "state": "0",
            "temps": "34"
        },
        {
            "filename": "file3",
            "note": "my thirst note",
            "state": "0",
            "temps": "71"
        }
    ]

The loop the list and create files. For example

    - copy:
        dest: "{{ item.filename }}"
        content: "{{ item.note }}"
      loop: "{{ my_files.list }}"

HTH,

  -vlado

Hey Vladimir !! :slight_smile:

Thanks for your answer,

My problem is that there is problem of syntax with module read_csv:

My task is :

- name: “Read users from CSV file and return a list”
read_csv:
path: /home/ansible/mssql_query/roles/files/csv_file.csv
delimiter: ‘,’
register: file_csv
delegate_to: localhost

Best Regards,

Karther

The syntax seems to be OK. Could you disclose the details of the error
message?

Hey Vladimir,

The error is :

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in ‘/home/ansible/mssql_query/roles/tasks/main.yml’: line 36, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: “Read users from CSV file and return a list”
^ here

Thanks for your help !! :slight_smile:

Best regards,

Karther

And,

I have this problem only with module read_csv it’s very strange …
all parameters are well indented

Can you help me please …

Best regards,

Karther

Ansible too old?
read_csv is available from 2.8

Hello Visser,

My version is :

ansible 2.8.0.dev0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/usr/share/ansible’]
ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
executable location = /var/ansible/bin/ansible
python version = 2.7.9 (default, Sep 14 2019, 20:00:08) [GCC 4.9.2]

Maybe 2.8.0.dev0 is not good version ? It’s not same that version 2.8.0 ?

Thanks for your answers !! :slight_smile:

Best regards,

Karther

This looks suspicious. Upgrade to the latest stable version in your distro.
Probably 2.9.0 ? See "Release and maintenance"
https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html