我想在 json 查询过滤器中使用 ansible 变量。这是我的代码:
剧本执行:
ansible-playbook debug.yml -e "project_environment=live"
- debug:
msg: "{{ project_environment }}"
- debug:
msg: "{{ check_objects | json_query('`{{project_environment}}`.current') }}"
这是我的字典:
check_objects:
live:
current:
- example.com
next:
- next.example.com
这就是我得到的:
TASK [debug : debug]
ok: [sample-hostname] => {
"msg": "live"
}
TASK [debug : debug]
ok: [sample-hostname] => {
"msg": ""
}
当我使用预期值替换变量时,它的输出工作正常:
- debug:
msg: "{{ check_objects | json_query('live.current') }}"
TASK [typo3-deployment/check : debug]
ok: [sbk-test-ntly01] => {
"msg": [
"example.com"
]
}
我认为它在插入变量时遇到了麻烦。
我已经尝试过这个解决方案,但它也不起作用:Ansible : pass a variable in a json_query filter