Search code examples
ansiblelowercasejmespath

Ansible json_query fail to search when json string convert to lower


i try to search string in json , all working great is the search term is used with exact case but i like to make the search case insensitive where i don't know in which case the given json will be : so this is my playbook see the "lower" command:

---
- hosts: test_host
  tasks:
    - name: get json
      set_fact:
        info_data: "{{ info_config }}"
      when: info_config is defined

    - name: print data
      debug:
        msg: "{{ info_data | lower |json_query(query) }}"
      vars:
        query: "*[].data_info[?contains(source,'\\${app1\\.type')].destination|[]"

here is my playbook command see the APP1 is upper case:

  ansible-playbook remote7.yaml -i ./hosts -e '{"info_config":{ "info_json": [{ "data_info": [{ "source": "\\${APP1\\.type}", "destination": "home_app" }, { "source": "\\${APP2\\.ip}", "destination": "localhost" } ] }, { "data_info": [{ "source": "\\${APP3\\.type}", "destination": "factory_app" }, { "source": "\\${APP4\\.ip}", "destination": "1.1.1.1" } ] } ] }}' 

and the result where msg is empty:

TASK [print data] ***************************************************************************************************************************************************************************************************************************
Thursday 19 August 2021  13:59:46 +0000 (0:00:00.046)       0:00:01.573 *******
ok: [10.0.8.218] =>
  msg: ''

Solution

  • if you first convert the JSON to lowercase and set it to a variable, it works:

    ---
    - hosts: localhost
    
      tasks:
    
        - name: print data
          debug:
            msg: "{{ info_data | json_query(query) }}"
          vars:
            info_data: "{{ info_config | lower }}"
            query: "*[].data_info[?contains(source,'\\${app1\\.type')].destination|[]"
          when: info_config is defined
    

    and the output:

    TASK [print data] ********************************************************************
    ok: [localhost] => {
        "msg": [
            "home_app"
        ]
    }