Build a file based on list only when another part of list matches ansible_hostname

12 views
Skip to first unread message

John

unread,
Mar 5, 2020, 3:24:30 PM3/5/20
to Ansible Project
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


John

unread,
Mar 5, 2020, 3:35:17 PM3/5/20
to Ansible Project
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)


Reply all
Reply to author
Forward
0 new messages