What approach should I take with this loop?

The following fails for sure, but you get the idea of what I am trying to do. How might I be able to loop this? Problem is with ‘searchme’ and probably trying to do something you can’t do

`

  • name: Set fact based on host
    set_fact:
    log_source: “{{ item.src }}”
    when: “ansible_hostname is search({{item.searchme}})”
    with_items:
  • { src: “/mnt/host1/error.log”, searchme: host1 }
  • { src: “/mnt/var/log/httpd/other.log”, searchme: host2 }
  • { src: “/mnt/host3/error.log”, searchme: host3 }

`

It's possible to use "JSON Query Filter"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter

For example the play below

    - hosts: my_hosts
      gather_facts: no
      vars:
        my_list:
          - {src: "/mnt/host1/error.log", searchme: "host1"}
          - {src: "/mnt/var/log/httpd/other.log", searchme: "host2"}
          - {src: "/mnt/host3/error.log", searchme: "host3"}
      tasks:
        - set_fact:
            log_source: "{{ my_list|json_query(query)|first }}"
          vars:
            query: "[?searchme=='{{ inventory_hostname }}'].src"
        - debug:
            var: log_source

gives

    ok: [host1] => {
        "log_source": "/mnt/host1/error.log"
    }
    ok: [host2] => {
        "log_source": "/mnt/var/log/httpd/other.log"
    }
    ok: [host3] => {
        "log_source": "/mnt/host3/error.log"
    }

Cheers,

  -vlado

Thank you!

Just change your when from:
`
when: “ansible_hostname is search({{item.searchme}})”

`

to:

when: inventory_hostname in item.searchme