# Parsing a csv file

**URL:** <https://forum.ansible.com/t/parsing-a-csv-file/31628>\
**Category:** Ansible Project\
**Created:** [November 8, 2019, 9:44pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628 "2019-11-08T21:44:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 8, 2019, 9:44pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628/1 "2019-11-08T21:44:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 8, 2019, 9:53pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628/2 "2019-11-08T21:53:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 8, 2019, 10:24pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628/3 "2019-11-08T22:24:40Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 8, 2019, 10:45pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628/4 "2019-11-08T22:45:51Z")

</div>

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’) }}”

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 13, 2019, 10:46pm UTC](https://forum.ansible.com/t/parsing-a-csv-file/31628/5 "2019-11-13T22:46:26Z")

</div>

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!
