Parsing a csv file

40 views
Skip to first unread message

John Harmon

unread,
Nov 8, 2019, 4:44:02 PM11/8/19
to Ansible Project
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.

John Harmon

unread,
Nov 8, 2019, 4:53:34 PM11/8/19
to Ansible Project
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

John Harmon

unread,
Nov 8, 2019, 5:24:40 PM11/8/19
to Ansible Project
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


Matt Martz

unread,
Nov 8, 2019, 5:46:40 PM11/8/19
to ansible...@googlegroups.com
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') }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/44dc83d9-3dd2-4714-96a0-18b8dbe46f5d%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

John Harmon

unread,
Nov 13, 2019, 5:46:26 PM11/13/19
to Ansible Project
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!
Reply all
Reply to author
Forward
0 new messages