-1

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: ''
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user63898
  • 29,839
  • 85
  • 272
  • 514
  • Did you try with `msg: "{{ info_data | json_query(query) | lower }}"` ? That would lower the resulting string of the JSON query, which I think is that you want. – Val Berthe Aug 19 '21 at 14:07
  • yes i did its empty array result ok: [10.0.8.218] => msg: [] – user63898 Aug 19 '21 at 14:16
  • What does `msg: "{{ info_data | json_query(query) }}"` give ? – Val Berthe Aug 19 '21 at 14:17
  • is i leave as in the example the json to be with upper case APP1 it returns nothing , but if i change the search query in the yml playbook to : "*[].data_info[?contains(source,'\\${APP1\\.type')].destination|[]" returns: ok: [10.0.8.218] => msg: - home_app – user63898 Aug 19 '21 at 14:24

1 Answers1

1

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"
    ]
}