I want to use a ansible variable inside the json query filter. This is my Code:
Playbook execution:
ansible-playbook debug.yml -e "project_environment=live"
- debug:
msg: "{{ project_environment }}"
- debug:
msg: "{{ check_objects | json_query('`{{project_environment}}`.current') }}"
This is my dictionary:
check_objects:
live:
current:
- example.com
next:
- next.example.com
This is what i got:
TASK [debug : debug]
ok: [sample-hostname] => {
"msg": "live"
}
TASK [debug : debug]
ok: [sample-hostname] => {
"msg": ""
}
When i replace the variable by using the expected value, the output it is working fine:
- debug:
msg: "{{ check_objects | json_query('live.current') }}"
TASK [typo3-deployment/check : debug]
ok: [sample-hostname] => {
"msg": [
"example.com"
]
}
I think it runs in trouble while interpolate the variable.
I have tried this solution but it doesn't work too: Ansible : pass a variable in a json_query filter
The task with json_query below
vars:
project_environment: live
tasks:
- debug:
msg: "{{ check_objects|
dict2items|
json_query(query)|
flatten }}"
vars:
query: "[?key=='{{ project_environment }}'].value.current"
gives
"msg": [
"example.com"
]
The same result can be achieved also with the task
- debug:
var: check_objects[project_environment].current