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:
`
`
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
`
sivel
(sivel)
November 8, 2019, 10:45pm
4
You can do something like one of the following options:
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!