# Build a file based on list only when another part of list matches ansible\_hostname

**URL:** <https://forum.ansible.com/t/build-a-file-based-on-list-only-when-another-part-of-list-matches-ansible-hostname/32397>\
**Category:** Ansible Project\
**Created:** [March 5, 2020, 8:24pm UTC](https://forum.ansible.com/t/build-a-file-based-on-list-only-when-another-part-of-list-matches-ansible-hostname/32397 "2020-03-05T20:24:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@John1](https://forum.ansible.com/u/John1)\
**Post date:** [March 5, 2020, 8:24pm UTC](https://forum.ansible.com/t/build-a-file-based-on-list-only-when-another-part-of-list-matches-ansible-hostname/32397/1 "2020-03-05T20:24:30Z")

</div>

I need to create a file based on the output of the csv\_content variable. Specifically I want to extract the ErrorMessage only, but the caveat is that I want only the errors associated with ansible\_hostname. That way on each server I am extracting only the ErrorMessages that are relevant to that server

Sample output from list:

`

ok: [3.220.248.61] =\> {  
“csv\_content”: {  
“changed”: false,  
“dict”: {},  
“failed”: false,  
“list”: [  
{  
“Customer”: “MyCustomerName”,  
“ErrorCount”: “7563”,  
“ErrorMessage”: “consider creating an index or changing the query”,  
“Host”: “MyCustomerName-prod-author1”,  
“InstanceId”: “i-0d0a56f41f9042c4f”,  
“PossibleReason”: “Missing search index that causes large repository traversal causing performance issues due to high disk I/O”,  
“RecommendedActions”: "Contact customizer to create required search index - ",  
“Severity”: “MAJOR”,  
“Source”: “/mnt/crx/author/crx-quickstart/logs/error.log”  
},  
{  
“Customer”: “MyCustomerName”,  
“ErrorCount”: “27882”,  
“ErrorMessage”: “consider creating an index or changing the query”,  
“Host”: “MyCustomerName-prod-httpd1”,  
“InstanceId”: “i-087c38748d150c4fa”,  
“PossibleReason”: “Missing search index that causes large repository traversal causing performance issues due to high disk I/O”,  
“RecommendedActions”: "Contact customizer to create required search index - ",  
“Severity”: “MAJOR”,  
“Source”: “/mnt/crx/publish/crx-quickstart/logs/error.log”  
},  
]  
}  
}

`

My attempt:

`

- name: Extract Errors  
lineinfile:  
line: “{{ item.ErrorMessage }}”  
path: “{{ error\_log }}”  
insertafter: EOF  
create: yes  
state: present  
loop: “{{ csv\_content.list }}”  
when: csv\_content.list.item.Host is search(ansible\_hostname) \<------------ Doesn’t work and I don’t even know if I can do something similar to this in a loop

`

---

<div class="post-metadata">

**Author:** ![John1](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@John1](https://forum.ansible.com/u/John1)\
**Post date:** [March 5, 2020, 8:35pm UTC](https://forum.ansible.com/t/build-a-file-based-on-list-only-when-another-part-of-list-matches-ansible-hostname/32397/2 "2020-03-05T20:35:17Z")

</div>

I was able to figure it out. I just had to think about it a bit more:

`

- name: Extract Errors  
lineinfile:  
line: “{{ item.ErrorMessage }}”  
path: “{{ error\_log }}”  
insertafter: EOF  
create: yes  
state: present  
loop: “{{ csv\_content.list }}”  
when: item.Host is search(ansible\_hostname)

`
