当 json 字符串转换为较低时,Ansible json_query 无法搜索
Ansible json_query fail to search when json string convert to lower
我尝试在 json 中搜索字符串,一切都很好,就是搜索词与精确大小写一起使用
但我喜欢让搜索不区分大小写,因为我不知道在哪种情况下给定的 json 将是:
所以这是我的剧本,请参阅“较低”命令:
---
- 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|[]"
这是我的剧本命令,看到 APP1 是大写的:
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" } ] } ] }}'
以及 msg 为空的结果:
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: ''
如果您先将 JSON 转换为小写并将其设置为一个变量,它会起作用:
---
- 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
和输出:
TASK [print data] ********************************************************************
ok: [localhost] => {
"msg": [
"home_app"
]
}
我尝试在 json 中搜索字符串,一切都很好,就是搜索词与精确大小写一起使用 但我喜欢让搜索不区分大小写,因为我不知道在哪种情况下给定的 json 将是: 所以这是我的剧本,请参阅“较低”命令:
---
- 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|[]"
这是我的剧本命令,看到 APP1 是大写的:
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" } ] } ] }}'
以及 msg 为空的结果:
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: ''
如果您先将 JSON 转换为小写并将其设置为一个变量,它会起作用:
---
- 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
和输出:
TASK [print data] ********************************************************************
ok: [localhost] => {
"msg": [
"home_app"
]
}