Parsing a csv file

I would like to parse a CSV. I need the column with the header ‘ErrorMessage’. I am reading the variable and gathering the information, but I don’t know how to pull out the info I need form the results:

`

  • name: Parse LookingGlass Report for Errors
    read_csv:
    path: /etc/ansible/roles/LookingGlass/files/Pentair.csv
    register: errors

  • debug:
    var: errors.list

`

Results:

`

ok: [localhost] => {
“errors.list”: [
{
“Customer”: “Test”,
“ErrorCount”: “19”,
“ErrorMessage”: “an error”,
“Host”: “host1”,
“RecommendedActions”: “”,
“Severity”: “LOW”,
},
{
“Customer”: “Test”,
“ErrorCount”: “3057”,
“ErrorMessage”: “A different error”,
“Host”: “host1”,
“RecommendedActions”: “”,
“Severity”: “LOW”,
},
… etc

`

I am hoping to get a list out of it like:

an error
A different error

and so on.

If I run the following I can pull back the key value pair for the first item in the list:
`

  • debug:
    var: errors.list.1.ErrorMessage

`

I just need every item in the list, and hopefully just the values

This does it in bash, but I am trying to avoid scripting it:

`
awk -F “","” ‘{print $2}’ my.csv

`

Results:

`

resource has no content
Blocked until the other thread finished using this session
not found
not found
not found
resource has no content

`

You can do something like one of the following options:

  • debug:
    msg: “{{ errors.list|map(attribute=‘ErrorMessage’) }}”

  • debug:
    var: item.ErrorMessage
    loop: “{{ errors.list }}”

Since ansible outputs JSONified results, you won’t really get the result you see with awk.

You could enable the debug callback plugin, and change the first one to look like:

  • debug:
    msg: “{{ errors.list|map(attribute=‘ErrorMessage’)|join(‘\n’) }}”

Matt, thank you!

I took what you gave me and finished off with this, which gives me what I need.

`

  • name: Write error log
    lineinfile:
    line: “{{ item.ErrorMessage }}”
    path: /tmp/errors.txt
    insertafter: EOF
    delegate_to: localhost
    loop: “{{ errors.list }}”

`

This ends up writing each line of the CSV (ErrorMessage) to a file. I can work with that. Thanks again!