简体   繁体   English

ansible、jinja2、json - 在 Jinja 中使用 json_query?

[英]ansible, jinja2, json - Using json_query in Jinja?

Hello Developer Community!你好开发者社区!

I'm currently working on developing some Ansible playbooks to manage Citrix NetScaler configuration and would like to get some help about the following.我目前正在开发一些 Ansible 剧本来管理 Citrix NetScaler 配置,并希望获得有关以下方面的一些帮助。 I have the following data structure defined in a variable file:我在变量文件中定义了以下数据结构:

nsadc_config_file_textfsm_nsapp_lb_service_parsed nsadc_config_file_textfsm_nsapp_lb_service_parsed

    {
    "bind_lbvserver_NotParsed": "",
    "bind_lbvserver_analyticsprofile": "",
    "bind_lbvserver_gotopriorityexpression": "",
    "bind_lbvserver_name": "VSRV-CS-Client1",
    "bind_lbvserver_policyname": "",
    "bind_lbvserver_priority": "",
    "bind_lbvserver_service_group_name": "SVC_Client1_App1_Server1_Port1",
    "bind_lbvserver_type": "",
    "bind_lbvserver_weight": ""
},
{
    "bind_lbvserver_NotParsed": "",
    "bind_lbvserver_analyticsprofile": "",
    "bind_lbvserver_gotopriorityexpression": "NEXT",
    "bind_lbvserver_name": "VSRV_Client2_App2",
    "bind_lbvserver_policyname": "policy_Client2_Rewrite",
    "bind_lbvserver_priority": "80",
    "bind_lbvserver_service_group_name": "",
    "bind_lbvserver_type": "REQUEST",
    "bind_lbvserver_weight": ""
},

I would like to query this JSON structure in a Jinja file , tried the followings but unfortunately it does not work.我想在 Jinja 文件中查询这个 JSON 结构,尝试了以下方法但不幸的是它不起作用。 I'm not 100% sure if it is possible to use "json_query" in a Jinja file.我不是 100% 确定是否可以在 Jinja 文件中使用“json_query”。

                                servicebindings:
{% for item_1 in nsadc_config_file_textfsm_nsapp_lb_vserver_binding_parsed %}
{% if (item_0.add_lbvserver_name == item_1.bind_lbvserver_name) and (item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) %}

                                    - servicename:         "{{ item_1.bind_lbvserver_service_group_name }}"
{% if item_1.bind_lbvserver_weight is defined and item_1.bind_lbvserver_weight %}
                                      weight:              "{{ item_1.bind_lbvserver_weight }}"
{% endif %}
{% endif %}
{% endfor %}

Instead of using (item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) above, I have also tried to include a json_query statement directly in Jinja, but with no luck.上面没有使用(item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) ,我还尝试在 Jinja 中直接包含一个 json_query 语句,但没有运气。 I'm afraid, there might be an issue with using quotes and escape chars.恐怕,使用引号和转义字符可能会出现问题。

{% if (item_0.add_lbvserver_name == item_1.bind_lbvserver_name) and (nsadc_config_file_textfsm_nsapp_lb_service_parsed | json_query(\"[?add_svc_name == '\" + item_1.bind_lbvserver_service_group_name + \"']\")) %}

Could anybody please advise if it is possible to query JSON structure directly in Jinja?有人可以建议是否可以直接在 Jinja 中查询 JSON 结构?

Many thanks in advance!提前谢谢了!

json_query is a Jinja filter, so of course you can use it in a template...or anywhere else that Jinja expressions are valid. json_query是一个 Jinja 过滤器,所以当然你可以在模板中使用它......或其他任何 Jinja 表达式有效的地方。

For example, if I have data in a playbook like this (and a template task):例如,如果我在这样的剧本(和模板任务)中有数据:

- hosts: localhost
  gather_facts: false
  vars:
    var1:
      - name: thing1
        color: red
      - name: thing2
        color: blue

    var2:
      - name: bob
        likes: blue
      - name: alice
        likes: green

  tasks:
    - template:
        src: template.in
        dest: output.txt

I can write a template that looks like this:我可以编写一个如下所示的模板:

{% for person in var2 %}
{% for thing in var1|json_query('[?color == `{}`]'.format(person.likes)) %}
- {{ person.name }} likes {{ thing.name }}
{% endfor %}
{% endfor %}

Which will produce the following output in output.txt :这将在output.txt产生以下输出:

- bob likes thing2

Note two things about the json_query expression in the template:请注意模板中有关json_query表达式的两件事:

  • Because we're looking for a literal string, we need to use backticks to quote the value.因为我们正在寻找一个文字字符串,所以我们需要使用反引号来引用该值。
  • We're using Python string formatting to include our target value in the query string.我们使用 Python 字符串格式将我们的目标值包含在查询字符串中。

In lieu of the string formatting syntax we could instead have written:代替字符串格式化语法,我们可以写成:

json_query('[?color == `' + person.likes + '`]')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM